20
20
import org .junit .Before ;
21
21
import org .junit .BeforeClass ;
22
22
import org .junit .Rule ;
23
- import org .junit .rules .RuleChain ;
24
- import org .junit .rules .TestRule ;
25
- import org .junit .rules .TestWatcher ;
26
- import org .junit .runner .Description ;
27
23
import org .junit .runner .RunWith ;
28
- import org .junit .runners .model .Statement ;
29
24
import org .openqa .selenium .Capabilities ;
30
25
import org .openqa .selenium .WebDriver ;
31
26
import org .openqa .selenium .environment .GlobalTestEnvironment ;
32
27
import org .openqa .selenium .environment .InProcessTestEnvironment ;
33
28
import org .openqa .selenium .environment .TestEnvironment ;
34
29
import org .openqa .selenium .environment .webserver .AppServer ;
35
- import org .openqa .selenium .remote .RemoteWebDriver ;
36
30
import org .openqa .selenium .support .ui .Wait ;
37
- import org .openqa .selenium .support .ui .WebDriverWait ;
38
- import org .openqa .selenium .testing .drivers .Browser ;
39
- import org .openqa .selenium .testing .drivers .WebDriverBuilder ;
40
31
41
- import java .time .Duration ;
42
- import java .util .logging .Logger ;
43
- import java .util .stream .Stream ;
44
-
45
- import static org .assertj .core .api .Assertions .assertThat ;
46
32
import static org .assertj .core .api .Assumptions .assumeThat ;
47
33
48
34
@ RunWith (SeleniumTestRunner .class )
49
35
public abstract class JUnit4TestBase {
50
36
51
- private static final Logger logger = Logger . getLogger ( JUnit4TestBase . class . getName ());
52
- private static final ThreadLocal < WebDriver > storedDriver = new ThreadLocal <> ();
37
+ @ Rule
38
+ public SeleniumTestRule seleniumTestRule = new SeleniumTestRule ();
53
39
54
- private final Browser current = Browser .detect ();
55
40
protected TestEnvironment environment ;
56
41
protected AppServer appServer ;
57
42
protected Pages pages ;
@@ -71,187 +56,18 @@ public void prepareEnvironment() {
71
56
72
57
pages = new Pages (appServer );
73
58
74
- String hostName = environment .getAppServer ().getHostName ();
75
- String alternateHostName = environment .getAppServer ().getAlternateHostName ();
76
-
77
- assertThat (hostName ).isNotEqualTo (alternateHostName );
78
- }
79
-
80
- @ Rule
81
- public TestRule chain = RuleChain
82
- .outerRule (new TraceMethodNameRule ())
83
- .around (new ManageDriverRule ())
84
- .around (new SwitchToTopRule ())
85
- .around (new NotYetImplementedRule ());
86
-
87
- private static class TraceMethodNameRule extends TestWatcher {
88
- @ Override
89
- protected void starting (Description description ) {
90
- super .starting (description );
91
- logger .info (">>> Starting " + description );
92
- }
93
-
94
- @ Override
95
- protected void finished (Description description ) {
96
- super .finished (description );
97
- logger .info ("<<< Finished " + description );
98
- }
99
- }
100
-
101
- private class ManageDriverRule extends TestWatcher {
102
- @ Override
103
- protected void starting (Description description ) {
104
- super .starting (description );
105
- NoDriverBeforeTest killSharedDriver = description .getAnnotation (NoDriverBeforeTest .class );
106
- if (killSharedDriver != null && current .matches (killSharedDriver .value ())) {
107
- System .out .println ("Destroying driver before test " + description );
108
- removeDriver ();
109
- return ;
110
- }
111
- NeedsFreshDriver annotation = description .getAnnotation (NeedsFreshDriver .class );
112
- if (annotation != null && current .matches (annotation .value ())) {
113
- System .out .println ("Restarting driver before test " + description );
114
- removeDriver ();
115
- }
116
- try {
117
- createDriver ();
118
- } catch (Exception e ) {
119
- throw new RuntimeException ("Exception creating driver" , e );
120
- }
121
- }
122
-
123
- @ Override
124
- protected void succeeded (Description description ) {
125
- super .finished (description );
126
- NoDriverAfterTest annotation = description .getAnnotation (NoDriverAfterTest .class );
127
- if (annotation != null && !annotation .failedOnly () && current .matches (annotation .value ())) {
128
- System .out .println ("Restarting driver after succeeded test " + description );
129
- removeDriver ();
130
- }
131
- }
132
-
133
- @ Override
134
- protected void failed (Throwable e , Description description ) {
135
- super .finished (description );
136
- NoDriverAfterTest annotation = description .getAnnotation (NoDriverAfterTest .class );
137
- if (annotation != null && current .matches (annotation .value ())) {
138
- System .out .println ("Restarting driver after failed test " + description );
139
- removeDriver ();
140
- }
141
- }
142
- }
143
-
144
- private class SwitchToTopRule extends TestWatcher {
145
- @ Override
146
- protected void finished (Description description ) {
147
- super .finished (description );
148
- SwitchToTopAfterTest annotation = description .getAnnotation (SwitchToTopAfterTest .class );
149
- if (annotation != null ) {
150
- driver .switchTo ().defaultContent ();
151
- }
152
- }
153
- }
154
-
155
- private class NotYetImplementedRule implements TestRule {
156
-
157
- private boolean notImplemented (NotYetImplementedList list ) {
158
- return list != null && list .value ().length > 0 && notImplemented (Stream .of (list .value ()));
159
- }
160
-
161
- private boolean notImplemented (NotYetImplemented single ) {
162
- return single != null && notImplemented (Stream .of (single ));
163
- }
164
-
165
- private boolean notImplemented (Stream <NotYetImplemented > nyi ) {
166
- return nyi .anyMatch (driver -> current .matches (driver .value ()));
167
- }
168
-
169
- @ Override
170
- public Statement apply (final Statement base , final Description description ) {
171
- if (notImplemented (description .getAnnotation (NotYetImplementedList .class )) ||
172
- notImplemented (description .getAnnotation (NotYetImplemented .class ))) {
173
- return new Statement () {
174
- @ Override
175
- public void evaluate () throws Throwable {
176
- Exception toBeThrown = null ;
177
- try {
178
- base .evaluate ();
179
- toBeThrown = new Exception (String .format (
180
- "%s.%s is marked as not yet implemented with %s but already works!" ,
181
- description .getTestClass ().getSimpleName (), description .getMethodName (), current ));
182
- }
183
- catch (final Throwable e ) {
184
- // expected
185
- }
186
- if (toBeThrown != null ) {
187
- throw toBeThrown ;
188
- }
189
- }
190
- };
191
-
192
- } else {
193
- return base ;
194
- }
195
- }
196
- }
197
-
198
- private void createDriver () {
199
- logger .info ("CREATING DRIVER" );
200
- driver = actuallyCreateDriver ();
201
- logger .info ("CREATED " + driver );
202
- wait = new WebDriverWait (driver , Duration .ofSeconds (10 ));
203
- shortWait = new WebDriverWait (driver , Duration .ofSeconds (5 ));
59
+ driver = seleniumTestRule .getDriver ();
60
+ wait = seleniumTestRule ::waitUntil ;
61
+ shortWait = seleniumTestRule ::shortWaitUntil ;
204
62
}
205
63
206
64
public void createNewDriver (Capabilities capabilities ) {
207
- removeDriver ();
208
- driver = actuallyCreateDriver (capabilities );
209
- wait = new WebDriverWait (driver , Duration .ofSeconds (10 ));
210
- shortWait = new WebDriverWait (driver , Duration .ofSeconds (5 ));
65
+ driver = seleniumTestRule .createNewDriver (capabilities );
66
+ wait = seleniumTestRule ::waitUntil ;
67
+ shortWait = seleniumTestRule ::shortWaitUntil ;
211
68
}
212
69
213
- private static WebDriver actuallyCreateDriver () {
214
- WebDriver driver = storedDriver .get ();
215
-
216
- if (driver == null ||
217
- (driver instanceof RemoteWebDriver && ((RemoteWebDriver )driver ).getSessionId () == null )) {
218
- StaticResources .ensureAvailable ();
219
- driver = new WebDriverBuilder ().get ();
220
- storedDriver .set (driver );
221
- }
222
- return storedDriver .get ();
70
+ public void removeDriver () {
71
+ seleniumTestRule .removeDriver ();
223
72
}
224
-
225
- private static WebDriver actuallyCreateDriver (Capabilities capabilities ) {
226
- WebDriver driver = storedDriver .get ();
227
-
228
- if (driver == null ||
229
- (driver instanceof RemoteWebDriver && ((RemoteWebDriver )driver ).getSessionId () == null )) {
230
- StaticResources .ensureAvailable ();
231
- driver = new WebDriverBuilder ().get (capabilities );
232
- storedDriver .set (driver );
233
- }
234
- return storedDriver .get ();
235
- }
236
-
237
- public static void removeDriver () {
238
- if (Boolean .getBoolean ("webdriver.singletestsuite.leaverunning" )) {
239
- return ;
240
- }
241
-
242
- WebDriver current = storedDriver .get ();
243
-
244
- if (current == null ) {
245
- return ;
246
- }
247
-
248
- try {
249
- current .quit ();
250
- } catch (RuntimeException ignored ) {
251
- // fall through
252
- }
253
-
254
- storedDriver .remove ();
255
- }
256
-
257
73
}
0 commit comments