Skip to content

Commit 1444932

Browse files
committed
Update to latest selenium 2.53.0
New listeners for better logging Fix for creating subdirectories for reporting on Windows platform
1 parent 328830f commit 1444932

23 files changed

+785
-310
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ classes/
6363

6464
# Seb directories #
6565
################################
66-
seb-report/*
66+
seb-reports/*

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cz.etnetera</groupId>
55
<artifactId>seb</artifactId>
6-
<version>0.3.20</version>
6+
<version>0.3.21</version>
77
<packaging>jar</packaging>
88

99
<name>Seb</name>
@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>org.seleniumhq.selenium</groupId>
6464
<artifactId>selenium-java</artifactId>
65-
<version>2.52.0</version>
65+
<version>2.53.0</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>junit</groupId>

src/main/java/cz/etnetera/seb/Seb.java

+82-43
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.HashMap;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.logging.Level;
3031
import java.util.stream.Collectors;
3132

3233
import org.openqa.selenium.By;
@@ -53,13 +54,14 @@
5354
import cz.etnetera.seb.element.SebFieldDecorator;
5455
import cz.etnetera.seb.event.EventConstructException;
5556
import cz.etnetera.seb.event.SebEvent;
57+
import cz.etnetera.seb.event.impl.AfterDriverConstructEvent;
5658
import cz.etnetera.seb.event.impl.AfterSebQuitEvent;
5759
import cz.etnetera.seb.event.impl.BeforeDriverConstructEvent;
5860
import cz.etnetera.seb.event.impl.BeforeSebQuitEvent;
5961
import cz.etnetera.seb.event.impl.LogEvent;
60-
import cz.etnetera.seb.event.impl.LogEvent.Level;
6162
import cz.etnetera.seb.event.impl.OnFileSaveEvent;
6263
import cz.etnetera.seb.event.impl.OnReportEvent;
64+
import cz.etnetera.seb.event.impl.OnSebStartEvent;
6365
import cz.etnetera.seb.listener.EventFiringSebBridgeListener;
6466
import cz.etnetera.seb.listener.SebListener;
6567
import cz.etnetera.seb.logic.Logic;
@@ -112,6 +114,10 @@ public class Seb implements SebContext {
112114

113115
protected boolean lazyDriver;
114116

117+
protected Level logLevel;
118+
119+
protected boolean started;
120+
115121
protected Map<String, Object> dataHolder = new HashMap<String, Object>();
116122

117123
protected SebUtils utils = new SebUtils();
@@ -126,7 +132,6 @@ public class Seb implements SebContext {
126132
* and properties from resource named seb.properties.
127133
*/
128134
public Seb() {
129-
initDefault();
130135
}
131136

132137
/**
@@ -135,7 +140,7 @@ public Seb() {
135140
* constructor with no parameters.
136141
*/
137142
public <T extends SebConfiguration> Seb(Class<T> configCls) {
138-
init(configCls);
143+
withConfiguration(configCls);
139144
}
140145

141146
/**
@@ -145,39 +150,65 @@ public <T extends SebConfiguration> Seb(Class<T> configCls) {
145150
* The configuration
146151
*/
147152
public Seb(SebConfiguration configuration) {
148-
init(configuration);
153+
withConfiguration(configuration);
149154
}
150155

151-
protected void initDefault() {
152-
init(BasicSebConfiguration.class);
156+
public Seb withDefaultConfiguration() {
157+
return withConfiguration(BasicSebConfiguration.class);
153158
}
154159

155-
protected <T extends SebConfiguration> void init(Class<T> configCls) {
160+
public <T extends SebConfiguration> Seb withConfiguration(Class<T> configCls) {
156161
try {
157-
init(configCls.getConstructor().newInstance());
162+
return withConfiguration(configCls.getConstructor().newInstance());
158163
} catch (Exception e) {
159164
throw new SebConfigurationConstructException("Unable to construct Seb configuration " + configCls.getName(), e);
160165
}
161166
}
162167

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();
165195
initListeners();
196+
triggerEvent(constructEvent(OnSebStartEvent.class));
197+
started = true;
166198
if (!lazyDriver)
167-
driver = createDriver();
199+
initDriver();
168200
}
169201

170-
protected void applyConfiguration(SebConfiguration configuration) {
202+
protected void applyConfiguration() {
171203
configuration.init();
172-
this.configuration = configuration;
173204
baseUrl = configuration.getBaseUrl();
174205
baseUrlRegex = configuration.getBaseUrlRegex();
175206
urlVerification = configuration.isUrlVerification();
176207
waitTimeout = configuration.getWaitTimeout();
177208
waitRetryInterval = configuration.getWaitRetryInterval();
178209
reported = configuration.isReported();
179-
reportDir = configuration.getReportDir();
180210
if (reported) {
211+
reportDir = configuration.getReportDir();
181212
if (!reportDir.exists()) {
182213
try {
183214
Files.createDirectories(reportDir.toPath());
@@ -198,27 +229,27 @@ protected void applyConfiguration(SebConfiguration configuration) {
198229
listeners = new ArrayList<>();
199230
}
200231
lazyDriver = configuration.isLazyDriver();
232+
logLevel = configuration.getLogLevel();
201233
}
202234

203235
protected void initListeners() {
204236
if (listeners != null)
205237
listeners.forEach(l -> l.init(this));
206238
}
207239

208-
protected WebDriver createDriver() {
240+
protected void initDriver() {
209241
// collect capabilities
210242
DesiredCapabilities caps = configuration.getCapabilities();
211243
// notify listeners to allow its change
212244
BeforeDriverConstructEvent befDriverConstEvent = constructEvent(BeforeDriverConstructEvent.class).with(caps);
213245
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));
218250

219251
// set driver specific configurations
220252
alertSupported = configuration.isAlertSupported(drv);
221-
return drv;
222253
}
223254

224255
/**
@@ -241,23 +272,14 @@ public String getLabel() {
241272
}
242273

243274
/**
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}.
255277
*
256-
* @param labels
257-
* Seb labels
278+
* @param label Seb label
279+
* @param moreLabels More labels joined to first label
258280
*/
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));
261283
}
262284

263285
/**
@@ -385,6 +407,15 @@ public boolean isAlertSupported() {
385407
return alertSupported;
386408
}
387409

410+
/**
411+
* Basic log level.
412+
*
413+
* @return The log level
414+
*/
415+
public Level getLogLevel() {
416+
return logLevel;
417+
}
418+
388419
/**
389420
* Returns utils instance.
390421
*
@@ -437,10 +468,11 @@ public void quit() {
437468
/**
438469
* Sets label using enclosing method class name and method name.
439470
*/
440-
public void useEnclosingMethodLabel() {
471+
public Seb useEnclosingMethodLabel() {
441472
final StackTraceElement e = Thread.currentThread().getStackTrace()[2];
442473
final String s = e.getClassName();
443474
setLabel(s.substring(s.lastIndexOf('.') + 1, s.length()), e.getMethodName());
475+
return this;
444476
}
445477

446478
/**
@@ -529,8 +561,9 @@ public <T> T getConfiguration(Class<T> configuration) {
529561

530562
@Override
531563
public WebDriver getDriver() {
564+
if (!started) start();
532565
if (lazyDriver && driver == null) {
533-
driver = createDriver();
566+
initDriver();
534567
}
535568
return driver;
536569
}
@@ -574,6 +607,7 @@ public <T extends Page> T goToSafely(Class<T> page) {
574607
try {
575608
return goTo(page);
576609
} catch (WebDriverException e) {
610+
log(Level.INFO, "Unable to SAFELY go to page " + page, e);
577611
return null;
578612
}
579613
}
@@ -583,6 +617,7 @@ public <T extends Page> T goToSafely(T page) {
583617
try {
584618
return goTo(page);
585619
} catch (WebDriverException e) {
620+
log(Level.INFO, "Unable to SAFELY go to page " + page, e);
586621
return null;
587622
}
588623
}
@@ -592,6 +627,7 @@ public <T extends Page> T initPageSafely(Class<T> page) {
592627
try {
593628
return initPage(page);
594629
} catch (WebDriverException e) {
630+
log(Level.INFO, "Unable to SAFELY init page " + page, e);
595631
return null;
596632
}
597633
}
@@ -601,6 +637,7 @@ public <T extends Page> T initPageSafely(T page) {
601637
try {
602638
return initPage(page);
603639
} catch (WebDriverException e) {
640+
log(Level.INFO, "Unable to SAFELY init page " + page, e);
604641
return null;
605642
}
606643
}
@@ -610,6 +647,7 @@ public Page initOnePageSafely(Object... pages) {
610647
try {
611648
return initOnePage(pages);
612649
} 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);
613651
return null;
614652
}
615653
}
@@ -652,10 +690,7 @@ public Page initOnePage(Object... pages) {
652690
return verifiedPage;
653691
}
654692
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())));
659694
}
660695

661696
@Override
@@ -794,7 +829,9 @@ public Path saveFile(byte[] bytes, String name, String extension) {
794829
if (!reported)
795830
return null;
796831
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);
798835
triggerEvent(constructEvent(OnFileSaveEvent.class, this).with(path.toFile()));
799836
return path;
800837
} catch (IOException e) {
@@ -807,7 +844,9 @@ public Path saveFile(File file, String name, String extension) {
807844
if (!reported)
808845
return null;
809846
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);
811850
triggerEvent(constructEvent(OnFileSaveEvent.class, this).with(path.toFile()));
812851
return path;
813852
} catch (IOException e) {

src/main/java/cz/etnetera/seb/SebContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.Path;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.logging.Level;
2223

2324
import org.openqa.selenium.By;
2425
import org.openqa.selenium.NoSuchElementException;
@@ -35,7 +36,6 @@
3536
import cz.etnetera.seb.element.SebElement;
3637
import cz.etnetera.seb.event.SebEvent;
3738
import cz.etnetera.seb.event.impl.LogEvent;
38-
import cz.etnetera.seb.event.impl.LogEvent.Level;
3939
import cz.etnetera.seb.event.impl.OnReportEvent;
4040
import cz.etnetera.seb.logic.Logic;
4141
import cz.etnetera.seb.page.Page;

src/main/java/cz/etnetera/seb/SebUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.List;
2121
import java.util.Objects;
22+
import java.util.regex.Pattern;
2223
import java.util.stream.Collectors;
2324
import java.util.stream.Stream;
2425

@@ -55,7 +56,7 @@ public Path getUniqueFilePath(File root, String name, String extension) {
5556
}
5657

5758
public String escapeFileName(String name) {
58-
return name.replaceAll("[^a-zA-Z0-9_\\-\\." + File.separator + "]", "_");
59+
return name.replaceAll("[^a-zA-Z0-9_\\-\\." + Pattern.quote(File.separator) + "]", "_");
5960
}
6061

6162
}

0 commit comments

Comments
 (0)