22
22
import com .google .common .base .Suppliers ;
23
23
import com .google .common .collect .ImmutableMap ;
24
24
25
+ import com .thoughtworks .selenium .SeleneseTestBase ;
25
26
import com .thoughtworks .selenium .Selenium ;
26
27
import com .thoughtworks .selenium .SeleniumException ;
27
28
import com .thoughtworks .selenium .webdriven .ElementFinder ;
@@ -47,14 +48,16 @@ private static ImmutableMap<String, CoreStepFactory> build() {
47
48
ImmutableMap .Builder <String , CoreStepFactory > steps = ImmutableMap .builder ();
48
49
49
50
CoreStepFactory nextCommandFails = (locator , value ) ->
50
- (selenium , state ) -> new NextCommandFails ();
51
+ (selenium , state ) -> new NextCommandFails (state . expand ( locator ) );
51
52
steps .put ("assertErrorOnNext" , nextCommandFails );
52
53
steps .put ("assertFailureOnNext" , nextCommandFails );
53
54
54
- CoreStepFactory verifyNextCommandFails = (locator , value ) ->
55
- (selenium , state ) -> new VerifyNextCommandFails ();
56
- steps .put ("verifyErrorOnNext" , verifyNextCommandFails );
57
- steps .put ("verifyFailureOnNext" , verifyNextCommandFails );
55
+ steps .put (
56
+ "verifyErrorOnNext" ,
57
+ (locator , value ) -> (selenium , state ) -> new VerifyNextCommandFails (state .expand (locator )));
58
+ steps .put (
59
+ "verifyFailureOnNext" ,
60
+ (locator , value ) -> (selenium , state ) -> new VerifyNextCommandFails (state .expand (locator )));
58
61
59
62
class SelectedOption implements CoreStep {
60
63
@@ -127,16 +130,31 @@ public NextStepDecorator execute(Selenium selenium, TestState state) {
127
130
}
128
131
129
132
private static class NextCommandFails extends NextStepDecorator {
133
+ private final String assertion ;
134
+
135
+ public NextCommandFails (String assertion ) {
136
+ this .assertion = assertion ;
137
+ }
130
138
131
139
@ Override
132
140
public NextStepDecorator evaluate (CoreStep nextStep , Selenium selenium , TestState state ) {
133
141
NextStepDecorator actualResult = nextStep .execute (selenium , state );
134
142
135
- // This is kind of fragile. Oh well.
136
- if (actualResult . equals ( NextStepDecorator . IDENTITY ) ) {
143
+ Throwable cause = actualResult . getCause ();
144
+ if (cause == null ) {
137
145
return NextStepDecorator .ASSERTION_FAILED ("Expected command to fail" );
138
146
}
139
- return NextStepDecorator .IDENTITY ;
147
+
148
+ if (!(cause instanceof SeleniumException )) {
149
+ return actualResult ;
150
+ }
151
+
152
+ try {
153
+ SeleneseTestBase .assertEquals (assertion , cause .getMessage ());
154
+ return NextStepDecorator .IDENTITY ;
155
+ } catch (AssertionError e ) {
156
+ return NextStepDecorator .ASSERTION_FAILED (e .getMessage ());
157
+ }
140
158
}
141
159
142
160
@ Override
@@ -146,16 +164,31 @@ public boolean isOkayToContinueTest() {
146
164
}
147
165
148
166
private static class VerifyNextCommandFails extends NextStepDecorator {
167
+ private final String assertion ;
168
+
169
+ public VerifyNextCommandFails (String assertion ) {
170
+ this .assertion = assertion ;
171
+ }
149
172
150
173
@ Override
151
174
public NextStepDecorator evaluate (CoreStep nextStep , Selenium selenium , TestState state ) {
152
175
NextStepDecorator actualResult = nextStep .execute (selenium , state );
153
176
154
- // This is kind of fragile. Oh well.
155
- if (actualResult . equals ( NextStepDecorator . IDENTITY ) ) {
177
+ Throwable cause = actualResult . getCause ();
178
+ if (cause == null ) {
156
179
return NextStepDecorator .VERIFICATION_FAILED ("Expected command to fail" );
157
180
}
158
- return NextStepDecorator .IDENTITY ;
181
+
182
+ if (!(cause instanceof SeleniumException )) {
183
+ return actualResult ;
184
+ }
185
+
186
+ try {
187
+ SeleneseTestBase .assertEquals (assertion , cause .getMessage ());
188
+ return NextStepDecorator .IDENTITY ;
189
+ } catch (AssertionError e ) {
190
+ return NextStepDecorator .VERIFICATION_FAILED (e .getMessage ());
191
+ }
159
192
}
160
193
161
194
@ Override
0 commit comments