Skip to content

Commit b4c39c7

Browse files
author
Alberto Scotto
committed
initial commit
0 parents  commit b4c39c7

14 files changed

+746
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
*.class
2+
3+
# Package Files #
4+
*.jar
5+
*.war
6+
*.ear
7+
8+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
9+
hs_err_pid*
10+
11+
# eclipse/mvn stuff
12+
.project
13+
.classpath
14+
.settings/
15+
target/
16+
17+
# idea stuff
18+
/.idea/
19+
*.iml

pom.xml

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.alb-i986.selenium</groupId>
8+
<artifactId>selenium-junit</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<description>A framework for writing maintainable poetry with Selenium WebDriver.</description>
11+
<url>http://alb-i986.github.io/selenium-tinafw/</url>
12+
13+
<licenses>
14+
<license>
15+
<name>MIT License</name>
16+
<url>http://www.opensource.org/licenses/mit-license</url>
17+
</license>
18+
</licenses>
19+
20+
<developers>
21+
<developer>
22+
<name>Alberto Scotto</name>
23+
<email>scotto.alberto.86@gmail.com</email>
24+
</developer>
25+
</developers>
26+
27+
<scm>
28+
<connection>scm:git:https://github.com/alb-i986/selenium-junit.git</connection>
29+
<developerConnection>scm:git:git@github.com:alb-i986/selenium-junit.git</developerConnection>
30+
<url>https://github.com/alb-i986/selenium-junit</url>
31+
</scm>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
36+
</properties>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<version>3.5.1</version>
44+
<configuration>
45+
<source>1.7</source>
46+
<target>1.7</target>
47+
</configuration>
48+
</plugin>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-source-plugin</artifactId>
52+
<version>3.0.0</version>
53+
<executions>
54+
<execution>
55+
<id>attach-sources</id>
56+
<goals>
57+
<goal>jar-no-fork</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-javadoc-plugin</artifactId>
65+
<version>2.10.3</version>
66+
<executions>
67+
<execution>
68+
<id>attach-javadocs</id>
69+
<goals>
70+
<goal>jar</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
<plugin>
76+
<groupId>org.sonatype.plugins</groupId>
77+
<artifactId>nexus-staging-maven-plugin</artifactId>
78+
<version>1.6.7</version>
79+
<extensions>true</extensions>
80+
<configuration>
81+
<serverId>ossrh</serverId>
82+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
83+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
84+
</configuration>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
<dependencies>
90+
<dependency>
91+
<groupId>junit</groupId>
92+
<artifactId>junit</artifactId>
93+
<version>4.12</version>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.hamcrest</groupId>
97+
<artifactId>hamcrest-library</artifactId>
98+
<version>1.3</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>log4j</groupId>
102+
<artifactId>log4j</artifactId>
103+
<version>1.2.17</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.seleniumhq.selenium</groupId>
107+
<artifactId>selenium-java</artifactId>
108+
<version>2.53.0</version>
109+
</dependency>
110+
111+
<!--test deps-->
112+
113+
<dependency>
114+
<groupId>org.mockito</groupId>
115+
<artifactId>mockito-core</artifactId>
116+
<version>1.10.19</version>
117+
<scope>test</scope>
118+
</dependency>
119+
</dependencies>
120+
121+
<reporting>
122+
<plugins>
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-javadoc-plugin</artifactId>
126+
<version>2.9.1</version>
127+
<configuration>
128+
<doclet>org.umlgraph.doclet.UmlGraphDoc</doclet>
129+
<docletArtifact>
130+
<groupId>org.umlgraph</groupId>
131+
<artifactId>umlgraph</artifactId>
132+
<version>5.6.6</version>
133+
</docletArtifact>
134+
<additionalparam>-views -all</additionalparam>
135+
<useStandardDocletOptions>true</useStandardDocletOptions>
136+
</configuration>
137+
</plugin>
138+
</plugins>
139+
</reporting>
140+
141+
<distributionManagement>
142+
<snapshotRepository>
143+
<id>ossrh</id>
144+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
145+
</snapshotRepository>
146+
<repository>
147+
<id>ossrh</id>
148+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
149+
</repository>
150+
</distributionManagement>
151+
152+
<profiles>
153+
<profile>
154+
<id>release-sign-artifacts</id>
155+
<activation>
156+
<property>
157+
<name>gpg.passphrase</name>
158+
</property>
159+
</activation>
160+
<build>
161+
<plugins>
162+
<plugin>
163+
<groupId>org.apache.maven.plugins</groupId>
164+
<artifactId>maven-gpg-plugin</artifactId>
165+
<version>1.5</version>
166+
<executions>
167+
<execution>
168+
<id>sign-artifacts</id>
169+
<phase>verify</phase>
170+
<goals>
171+
<goal>sign</goal>
172+
</goals>
173+
</execution>
174+
</executions>
175+
</plugin>
176+
</plugins>
177+
</build>
178+
</profile>
179+
</profiles>
180+
181+
182+
183+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package me.alb_i986.selenium;
2+
3+
import org.openqa.selenium.WebDriver;
4+
5+
/**
6+
* A factory creating instances of {@link WebDriver}.
7+
*/
8+
public interface WebDriverFactory {
9+
WebDriver create();
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.alb_i986.selenium.junit.rules;
2+
3+
import org.junit.rules.ExternalResource;
4+
import org.openqa.selenium.remote.service.DriverService;
5+
6+
public class DriverServiceResource extends ExternalResource {
7+
8+
private final DriverService service;
9+
10+
public DriverServiceResource(DriverService service) {
11+
if (service == null) {
12+
throw new IllegalArgumentException("service should not be null");
13+
}
14+
this.service = service;
15+
}
16+
17+
/**
18+
* Start the service
19+
*
20+
* @see DriverService#start()
21+
*/
22+
@Override
23+
protected void before() throws Throwable {
24+
service.start();
25+
}
26+
27+
/**
28+
* Stop the service
29+
*
30+
* @see DriverService#stop()
31+
*/
32+
@Override
33+
protected void after() {
34+
service.stop();
35+
}
36+
37+
public DriverService getService() {
38+
return service;
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.alb_i986.selenium.junit.rules;
2+
3+
import me.alb_i986.selenium.WebDriverFactory;
4+
import org.junit.rules.RuleChain;
5+
import org.openqa.selenium.OutputType;
6+
import org.openqa.selenium.TakesScreenshot;
7+
import org.openqa.selenium.WebDriver;
8+
9+
import java.util.logging.Logger;
10+
11+
/**
12+
* @author ascotto
13+
*/
14+
public class RuleChainBuilder {
15+
16+
private final WebDriverResource driverResource;
17+
private TakeScreenshotOnFailureRule takeScreenshotOnFailureRule;
18+
private TestLoggerRule testLogger;
19+
20+
public RuleChainBuilder(WebDriver driver) {
21+
this.driverResource = new WebDriverResource(driver);
22+
}
23+
24+
public RuleChainBuilder(WebDriverFactory factory) {
25+
this.driverResource = new WebDriverResource(factory);
26+
}
27+
28+
public RuleChainBuilder withTestLogger(Logger logger) {
29+
this.testLogger = new TestLoggerRule(logger);
30+
return this;
31+
}
32+
33+
public <X> RuleChainBuilder takeScreenshotOnFailure(OutputType<X> outputType) {
34+
WebDriver driver = driverResource.getDriver();
35+
if (!(driver instanceof TakesScreenshot)) {
36+
throw new IllegalStateException("the driver is not able to take screenshots");
37+
}
38+
this.takeScreenshotOnFailureRule = new TakeScreenshotOnFailureRule(((TakesScreenshot) driver), outputType);
39+
return this;
40+
}
41+
42+
public RuleChain build() {
43+
RuleChain ruleChain = RuleChain.outerRule(driverResource);
44+
if (takeScreenshotOnFailureRule != null) {
45+
ruleChain.around(takeScreenshotOnFailureRule);
46+
}
47+
if (testLogger != null) {
48+
ruleChain.around(testLogger);
49+
}
50+
return ruleChain;
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package me.alb_i986.selenium.junit.rules;
2+
3+
import org.junit.rules.TestWatcher;
4+
import org.junit.runner.Description;
5+
import org.openqa.selenium.OutputType;
6+
import org.openqa.selenium.TakesScreenshot;
7+
8+
/**
9+
* @author ascotto
10+
*/
11+
public class TakeScreenshotOnFailureRule<X> extends TestWatcher {
12+
13+
private final TakesScreenshot driver;
14+
private final OutputType<X> outputType;
15+
16+
public TakeScreenshotOnFailureRule(TakesScreenshot driver, OutputType<X> outputType) {
17+
if (driver == null) {
18+
throw new IllegalArgumentException("the driver should not be null");
19+
}
20+
this.driver = driver;
21+
this.outputType = outputType;
22+
}
23+
24+
/**
25+
* @throws org.openqa.selenium.WebDriverException if taking the screenshot fails
26+
*/
27+
@Override
28+
protected void failed(Throwable e, Description description) {
29+
X screenshot = driver.getScreenshotAs(outputType);
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.alb_i986.selenium.junit.rules;
2+
3+
import org.junit.AssumptionViolatedException;
4+
import org.junit.rules.TestWatcher;
5+
import org.junit.runner.Description;
6+
7+
import java.util.logging.Logger;
8+
9+
/**
10+
* @author ascotto
11+
*/
12+
public class TestLoggerRule extends TestWatcher {
13+
14+
protected final Logger logger;
15+
16+
public TestLoggerRule(Logger logger) {
17+
if (logger == null) {
18+
throw new IllegalArgumentException("logger should not be null");
19+
}
20+
this.logger = logger;
21+
}
22+
23+
@Override
24+
protected void starting(Description description) {
25+
logger.info("STARTING " + description.getDisplayName());
26+
}
27+
28+
@Override
29+
protected void skipped(AssumptionViolatedException e, Description description) {
30+
logger.info("SKIPPED " + description.getDisplayName());
31+
}
32+
33+
@Override
34+
protected void failed(Throwable e, Description description) {
35+
logger.warning("FAILED " + description.getDisplayName());
36+
}
37+
38+
@Override
39+
protected void succeeded(Description description) {
40+
logger.info("PASSED " + description.getDisplayName());
41+
}
42+
}

0 commit comments

Comments
 (0)