27
27
import java .util .HashMap ;
28
28
import java .util .List ;
29
29
import java .util .Map ;
30
+ import java .util .logging .Level ;
30
31
import java .util .stream .Collectors ;
31
32
32
33
import org .openqa .selenium .By ;
53
54
import cz .etnetera .seb .element .SebFieldDecorator ;
54
55
import cz .etnetera .seb .event .EventConstructException ;
55
56
import cz .etnetera .seb .event .SebEvent ;
57
+ import cz .etnetera .seb .event .impl .AfterDriverConstructEvent ;
56
58
import cz .etnetera .seb .event .impl .AfterSebQuitEvent ;
57
59
import cz .etnetera .seb .event .impl .BeforeDriverConstructEvent ;
58
60
import cz .etnetera .seb .event .impl .BeforeSebQuitEvent ;
59
61
import cz .etnetera .seb .event .impl .LogEvent ;
60
- import cz .etnetera .seb .event .impl .LogEvent .Level ;
61
62
import cz .etnetera .seb .event .impl .OnFileSaveEvent ;
62
63
import cz .etnetera .seb .event .impl .OnReportEvent ;
64
+ import cz .etnetera .seb .event .impl .OnSebStartEvent ;
63
65
import cz .etnetera .seb .listener .EventFiringSebBridgeListener ;
64
66
import cz .etnetera .seb .listener .SebListener ;
65
67
import cz .etnetera .seb .logic .Logic ;
@@ -112,6 +114,10 @@ public class Seb implements SebContext {
112
114
113
115
protected boolean lazyDriver ;
114
116
117
+ protected Level logLevel ;
118
+
119
+ protected boolean started ;
120
+
115
121
protected Map <String , Object > dataHolder = new HashMap <String , Object >();
116
122
117
123
protected SebUtils utils = new SebUtils ();
@@ -126,7 +132,6 @@ public class Seb implements SebContext {
126
132
* and properties from resource named seb.properties.
127
133
*/
128
134
public Seb () {
129
- initDefault ();
130
135
}
131
136
132
137
/**
@@ -135,7 +140,7 @@ public Seb() {
135
140
* constructor with no parameters.
136
141
*/
137
142
public <T extends SebConfiguration > Seb (Class <T > configCls ) {
138
- init (configCls );
143
+ withConfiguration (configCls );
139
144
}
140
145
141
146
/**
@@ -145,39 +150,65 @@ public <T extends SebConfiguration> Seb(Class<T> configCls) {
145
150
* The configuration
146
151
*/
147
152
public Seb (SebConfiguration configuration ) {
148
- init (configuration );
153
+ withConfiguration (configuration );
149
154
}
150
155
151
- protected void initDefault () {
152
- init (BasicSebConfiguration .class );
156
+ public Seb withDefaultConfiguration () {
157
+ return withConfiguration (BasicSebConfiguration .class );
153
158
}
154
159
155
- protected <T extends SebConfiguration > void init (Class <T > configCls ) {
160
+ public <T extends SebConfiguration > Seb withConfiguration (Class <T > configCls ) {
156
161
try {
157
- init (configCls .getConstructor ().newInstance ());
162
+ return withConfiguration (configCls .getConstructor ().newInstance ());
158
163
} catch (Exception e ) {
159
164
throw new SebConfigurationConstructException ("Unable to construct Seb configuration " + configCls .getName (), e );
160
165
}
161
166
}
162
167
163
- protected void init (SebConfiguration configuration ) {
164
- applyConfiguration (configuration );
168
+ public Seb withConfiguration (SebConfiguration configuration ) {
169
+ this .configuration = configuration ;
170
+ return this ;
171
+ }
172
+
173
+ /**
174
+ * Modify Seb label. Pass more labels
175
+ * to join them using {@link Seb#LABEL_DELIMITER}.
176
+ *
177
+ * @param label Seb label
178
+ * @param moreLabels More labels joined to first label
179
+ * @return Seb instance
180
+ */
181
+ public Seb withLabel (String label , String ... moreLabels ) {
182
+ this .label = utils .join (LABEL_DELIMITER , label , utils .join (LABEL_DELIMITER , (Object []) moreLabels ));
183
+ return this ;
184
+ }
185
+
186
+ public Seb start () {
187
+ init ();
188
+ return this ;
189
+ }
190
+
191
+ protected void init () {
192
+ if (configuration == null )
193
+ withDefaultConfiguration ();
194
+ applyConfiguration ();
165
195
initListeners ();
196
+ triggerEvent (constructEvent (OnSebStartEvent .class ));
197
+ started = true ;
166
198
if (!lazyDriver )
167
- driver = createDriver ();
199
+ initDriver ();
168
200
}
169
201
170
- protected void applyConfiguration (SebConfiguration configuration ) {
202
+ protected void applyConfiguration () {
171
203
configuration .init ();
172
- this .configuration = configuration ;
173
204
baseUrl = configuration .getBaseUrl ();
174
205
baseUrlRegex = configuration .getBaseUrlRegex ();
175
206
urlVerification = configuration .isUrlVerification ();
176
207
waitTimeout = configuration .getWaitTimeout ();
177
208
waitRetryInterval = configuration .getWaitRetryInterval ();
178
209
reported = configuration .isReported ();
179
- reportDir = configuration .getReportDir ();
180
210
if (reported ) {
211
+ reportDir = configuration .getReportDir ();
181
212
if (!reportDir .exists ()) {
182
213
try {
183
214
Files .createDirectories (reportDir .toPath ());
@@ -198,27 +229,27 @@ protected void applyConfiguration(SebConfiguration configuration) {
198
229
listeners = new ArrayList <>();
199
230
}
200
231
lazyDriver = configuration .isLazyDriver ();
232
+ logLevel = configuration .getLogLevel ();
201
233
}
202
234
203
235
protected void initListeners () {
204
236
if (listeners != null )
205
237
listeners .forEach (l -> l .init (this ));
206
238
}
207
239
208
- protected WebDriver createDriver () {
240
+ protected void initDriver () {
209
241
// collect capabilities
210
242
DesiredCapabilities caps = configuration .getCapabilities ();
211
243
// notify listeners to allow its change
212
244
BeforeDriverConstructEvent befDriverConstEvent = constructEvent (BeforeDriverConstructEvent .class ).with (caps );
213
245
triggerEvent (befDriverConstEvent );
214
-
215
- EventFiringWebDriver drv = new EventFiringWebDriver (
216
- configuration . getDriver ( befDriverConstEvent . getCapabilities () ));
217
- drv . register ( new EventFiringSebBridgeListener ( this ));
246
+ WebDriver drv = configuration . getDriver ( befDriverConstEvent . getCapabilities ());
247
+
248
+ driver = new EventFiringWebDriver ( drv ). register ( new EventFiringSebBridgeListener ( this ));
249
+ triggerEvent ( constructEvent ( AfterDriverConstructEvent . class ));
218
250
219
251
// set driver specific configurations
220
252
alertSupported = configuration .isAlertSupported (drv );
221
- return drv ;
222
253
}
223
254
224
255
/**
@@ -241,23 +272,14 @@ public String getLabel() {
241
272
}
242
273
243
274
/**
244
- * Modify Seb label
245
- *
246
- * @param label
247
- * Seb label
248
- */
249
- public void setLabel (String label ) {
250
- this .label = label ;
251
- }
252
-
253
- /**
254
- * Modify Seb label joining given labels.
275
+ * Modify Seb label. Pass more labels
276
+ * to join them using {@link Seb#LABEL_DELIMITER}.
255
277
*
256
- * @param labels
257
- * Seb labels
278
+ * @param label Seb label
279
+ * @param moreLabels More labels joined to first label
258
280
*/
259
- public void setLabel (String ... labels ) {
260
- this .label = utils .join (LABEL_DELIMITER , ( Object []) labels );
281
+ public void setLabel (String label , String ... moreLabels ) {
282
+ this .label = utils .join (LABEL_DELIMITER , label , utils . join ( LABEL_DELIMITER , ( Object []) moreLabels ) );
261
283
}
262
284
263
285
/**
@@ -385,6 +407,15 @@ public boolean isAlertSupported() {
385
407
return alertSupported ;
386
408
}
387
409
410
+ /**
411
+ * Basic log level.
412
+ *
413
+ * @return The log level
414
+ */
415
+ public Level getLogLevel () {
416
+ return logLevel ;
417
+ }
418
+
388
419
/**
389
420
* Returns utils instance.
390
421
*
@@ -437,10 +468,11 @@ public void quit() {
437
468
/**
438
469
* Sets label using enclosing method class name and method name.
439
470
*/
440
- public void useEnclosingMethodLabel () {
471
+ public Seb useEnclosingMethodLabel () {
441
472
final StackTraceElement e = Thread .currentThread ().getStackTrace ()[2 ];
442
473
final String s = e .getClassName ();
443
474
setLabel (s .substring (s .lastIndexOf ('.' ) + 1 , s .length ()), e .getMethodName ());
475
+ return this ;
444
476
}
445
477
446
478
/**
@@ -529,8 +561,9 @@ public <T> T getConfiguration(Class<T> configuration) {
529
561
530
562
@ Override
531
563
public WebDriver getDriver () {
564
+ if (!started ) start ();
532
565
if (lazyDriver && driver == null ) {
533
- driver = createDriver ();
566
+ initDriver ();
534
567
}
535
568
return driver ;
536
569
}
@@ -574,6 +607,7 @@ public <T extends Page> T goToSafely(Class<T> page) {
574
607
try {
575
608
return goTo (page );
576
609
} catch (WebDriverException e ) {
610
+ log (Level .INFO , "Unable to SAFELY go to page " + page , e );
577
611
return null ;
578
612
}
579
613
}
@@ -583,6 +617,7 @@ public <T extends Page> T goToSafely(T page) {
583
617
try {
584
618
return goTo (page );
585
619
} catch (WebDriverException e ) {
620
+ log (Level .INFO , "Unable to SAFELY go to page " + page , e );
586
621
return null ;
587
622
}
588
623
}
@@ -592,6 +627,7 @@ public <T extends Page> T initPageSafely(Class<T> page) {
592
627
try {
593
628
return initPage (page );
594
629
} catch (WebDriverException e ) {
630
+ log (Level .INFO , "Unable to SAFELY init page " + page , e );
595
631
return null ;
596
632
}
597
633
}
@@ -601,6 +637,7 @@ public <T extends Page> T initPageSafely(T page) {
601
637
try {
602
638
return initPage (page );
603
639
} catch (WebDriverException e ) {
640
+ log (Level .INFO , "Unable to SAFELY init page " + page , e );
604
641
return null ;
605
642
}
606
643
}
@@ -610,6 +647,7 @@ public Page initOnePageSafely(Object... pages) {
610
647
try {
611
648
return initOnePage (pages );
612
649
} catch (WebDriverException e ) {
650
+ log (Level .INFO , "Unable to SAFELY init any of given pages " + String .join (", " , Arrays .asList (pages ).stream ().map (p -> p .toString ()).collect (Collectors .toList ())), e );
613
651
return null ;
614
652
}
615
653
}
@@ -652,10 +690,7 @@ public Page initOnePage(Object... pages) {
652
690
return verifiedPage ;
653
691
}
654
692
throw new VerificationException (
655
- "Unable to init any of given pages " + String .join (", " , Arrays .asList (pages ).stream ().map (p -> {
656
- return (String ) ((p instanceof Page ) ? ((Page ) p ).getClass ().getName ()
657
- : ((Class <? extends Page >) p ).getName ());
658
- }).collect (Collectors .toList ())));
693
+ "Unable to init any of given pages " + String .join (", " , Arrays .asList (pages ).stream ().map (p -> p .toString ()).collect (Collectors .toList ())));
659
694
}
660
695
661
696
@ Override
@@ -794,7 +829,9 @@ public Path saveFile(byte[] bytes, String name, String extension) {
794
829
if (!reported )
795
830
return null ;
796
831
try {
797
- Path path = Files .write (getUniqueFilePath (name , extension ), bytes );
832
+ Path uniquePath = getUniqueFilePath (name , extension );
833
+ Files .createDirectories (uniquePath .getParent ());
834
+ Path path = Files .write (uniquePath , bytes );
798
835
triggerEvent (constructEvent (OnFileSaveEvent .class , this ).with (path .toFile ()));
799
836
return path ;
800
837
} catch (IOException e ) {
@@ -807,7 +844,9 @@ public Path saveFile(File file, String name, String extension) {
807
844
if (!reported )
808
845
return null ;
809
846
try {
810
- Path path = Files .copy (file .toPath (), getUniqueFilePath (name , extension ));
847
+ Path uniquePath = getUniqueFilePath (name , extension );
848
+ Files .createDirectories (uniquePath .getParent ());
849
+ Path path = Files .copy (file .toPath (), uniquePath );
811
850
triggerEvent (constructEvent (OnFileSaveEvent .class , this ).with (path .toFile ()));
812
851
return path ;
813
852
} catch (IOException e ) {
0 commit comments