Skip to content

Commit ea2fdb6

Browse files
Abhi Singhfrancisf
authored andcommitted
Added TestNG Runner
1 parent cbbaf02 commit ea2fdb6

File tree

11 files changed

+360
-176
lines changed

11 files changed

+360
-176
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ target/
1717
*.iml
1818

1919
local.log
20+
reports

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* Update `*.conf.js` files inside the `src/test/resources/conf/` directory to update desired capabilities.
1111

1212
## Running your tests
13-
* To run a single test, run `mvn compile exec:java -P single`
14-
* To run parallel tests, run `mvn compile exec:java -P parallel`
15-
* To run local tests, run `mvn compile exec:java -P local`
13+
* To run a single test, run `mvn test -P single`
14+
* To run parallel tests, run `mvn test -P parallel`
15+
* To run local tests, run `mvn test -P local`
1616

1717

1818

pom.xml

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<maven.compiler.source>1.8</maven.compiler.source>
1616
<maven.compiler.target>1.8</maven.compiler.target>
1717
<cucumber.version>6.9.1</cucumber.version>
18+
<testng.version>7.4.0</testng.version>
1819
<selenium-java.version>3.141.59</selenium-java.version>
1920
<browserstack-local-java.version>1.0.6</browserstack-local-java.version>
2021
<json-simple.version>1.1.1</json-simple.version>
@@ -34,12 +35,24 @@
3435
<version>${slf4j.version}</version>
3536
</dependency>
3637

38+
<dependency>
39+
<groupId>org.testng</groupId>
40+
<artifactId>testng</artifactId>
41+
<version>${testng.version}</version>
42+
<scope>test</scope>
43+
</dependency>
3744
<dependency>
3845
<groupId>io.cucumber</groupId>
3946
<artifactId>cucumber-java</artifactId>
4047
<version>${cucumber.version}</version>
4148
<scope>test</scope>
4249
</dependency>
50+
<dependency>
51+
<groupId>io.cucumber</groupId>
52+
<artifactId>cucumber-testng</artifactId>
53+
<version>${cucumber.version}</version>
54+
<scope>test</scope>
55+
</dependency>
4356
<dependency>
4457
<groupId>org.seleniumhq.selenium</groupId>
4558
<artifactId>selenium-java</artifactId>
@@ -87,31 +100,21 @@
87100
<build>
88101
<plugins>
89102
<plugin>
90-
<groupId>org.codehaus.mojo</groupId>
91-
<artifactId>exec-maven-plugin</artifactId>
92-
<version>3.0.0</version>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-surefire-plugin</artifactId>
105+
<version>3.0.0-M5</version>
93106
<configuration>
94-
<classpathScope>test</classpathScope>
95-
<mainClass>com.browserstack.TestRunner</mainClass>
96-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
97-
<systemProperties>
98-
<property>
99-
<key>cucumber.publish.quiet</key>
100-
<value>true</value>
101-
</property>
102-
<property>
103-
<key>cucumber.plugin</key>
104-
<value>html:target/cucumber-html-reports.html</value>
105-
</property>
106-
<property>
107-
<key>caps</key>
108-
<value>src/test/resources/conf/single.conf.json</value>
109-
</property>
110-
<property>
111-
<key>features</key>
112-
<value>src/test/resources/features/test</value>
113-
</property>
114-
</systemProperties>
107+
<suiteXmlFiles>
108+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
109+
</suiteXmlFiles>
110+
<argLine>
111+
-Ddataproviderthreadcount=1
112+
</argLine>
113+
<systemPropertyVariables>
114+
<caps>src/test/resources/conf/single.conf.json</caps>
115+
<cucumber.publish.quiet>true</cucumber.publish.quiet>
116+
<cucumber.features>src/test/resources/features/test</cucumber.features>
117+
</systemPropertyVariables>
115118
</configuration>
116119
</plugin>
117120
</plugins>
@@ -122,35 +125,27 @@
122125
<build>
123126
<plugins>
124127
<plugin>
125-
<groupId>org.codehaus.mojo</groupId>
126-
<artifactId>exec-maven-plugin</artifactId>
127-
<version>3.0.0</version>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-surefire-plugin</artifactId>
130+
<version>3.0.0-M5</version>
128131
<configuration>
129-
<classpathScope>test</classpathScope>
130-
<mainClass>com.browserstack.TestRunner</mainClass>
131-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
132-
<systemProperties>
132+
<suiteXmlFiles>
133+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
134+
</suiteXmlFiles>
135+
<argLine>
136+
-Ddataproviderthreadcount=4
137+
</argLine>
138+
<systemPropertyVariables>
139+
<caps>src/test/resources/conf/parallel.conf.json</caps>
140+
<cucumber.publish.quiet>true</cucumber.publish.quiet>
141+
<cucumber.features>src/test/resources/features/test</cucumber.features>
142+
</systemPropertyVariables>
143+
<properties>
133144
<property>
134-
<key>cucumber.publish.quiet</key>
135-
<value>true</value>
145+
<name>parallel</name>
146+
<value>methods</value>
136147
</property>
137-
<property>
138-
<key>cucumber.plugin</key>
139-
<value>html:target/cucumber-html-reports.html</value>
140-
</property>
141-
<property>
142-
<key>caps</key>
143-
<value>src/test/resources/conf/parallel.conf.json</value>
144-
</property>
145-
<property>
146-
<key>parallel-count</key>
147-
<value>3</value>
148-
</property>
149-
<property>
150-
<key>features</key>
151-
<value>src/test/resources/features/test</value>
152-
</property>
153-
</systemProperties>
148+
</properties>
154149
</configuration>
155150
</plugin>
156151
</plugins>
@@ -161,31 +156,21 @@
161156
<build>
162157
<plugins>
163158
<plugin>
164-
<groupId>org.codehaus.mojo</groupId>
165-
<artifactId>exec-maven-plugin</artifactId>
166-
<version>3.0.0</version>
159+
<groupId>org.apache.maven.plugins</groupId>
160+
<artifactId>maven-surefire-plugin</artifactId>
161+
<version>3.0.0-M5</version>
167162
<configuration>
168-
<classpathScope>test</classpathScope>
169-
<mainClass>com.browserstack.TestRunner</mainClass>
170-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
171-
<systemProperties>
172-
<property>
173-
<key>cucumber.publish.quiet</key>
174-
<value>true</value>
175-
</property>
176-
<property>
177-
<key>cucumber.plugin</key>
178-
<value>html:target/cucumber-html-reports.html</value>
179-
</property>
180-
<property>
181-
<key>caps</key>
182-
<value>src/test/resources/conf/local.conf.json</value>
183-
</property>
184-
<property>
185-
<key>features</key>
186-
<value>src/test/resources/features/localtest</value>
187-
</property>
188-
</systemProperties>
163+
<suiteXmlFiles>
164+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
165+
</suiteXmlFiles>
166+
<argLine>
167+
-Ddataproviderthreadcount=1
168+
</argLine>
169+
<systemPropertyVariables>
170+
<caps>src/test/resources/conf/local.conf.json</caps>
171+
<cucumber.publish.quiet>true</cucumber.publish.quiet>
172+
<cucumber.features>src/test/resources/features/localtest</cucumber.features>
173+
</systemPropertyVariables>
189174
</configuration>
190175
</plugin>
191176
</plugins>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.browserstack;
2+
3+
import java.util.Iterator;
4+
5+
import com.browserstack.local.Local;
6+
import com.browserstack.util.Utility;
7+
import org.testng.annotations.AfterClass;
8+
import org.testng.annotations.BeforeClass;
9+
import org.testng.annotations.DataProvider;
10+
import org.testng.annotations.Test;
11+
12+
import com.browserstack.webdriver.LazyInitWebDriverIterator;
13+
import com.browserstack.webdriver.ManagedWebDriver;
14+
import io.cucumber.testng.CucumberOptions;
15+
import io.cucumber.testng.FeatureWrapper;
16+
import io.cucumber.testng.PickleWrapper;
17+
import io.cucumber.testng.TestNGCucumberRunner;
18+
19+
@CucumberOptions(
20+
glue = "com.browserstack.stepdefs",
21+
plugin = {
22+
"pretty",
23+
"html:reports/tests/cucumber/html",
24+
"timeline:reports/tests/cucumber/timeline",
25+
"junit:reports/tests/cucumber/junit/cucumber.xml",
26+
"testng:reports/tests/cucumber/testng/cucumber.xml",
27+
"json:reports/tests/cucumber/json/cucumber.json"
28+
}
29+
)
30+
public class RunWebDriverCucumberTests {
31+
32+
private TestNGCucumberRunner testNGCucumberRunner;
33+
private Local local;
34+
private static final ThreadLocal<ManagedWebDriver> threadLocalWebDriver = new ThreadLocal<>();
35+
36+
@BeforeClass(alwaysRun = true)
37+
public void setUpClass() {
38+
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
39+
}
40+
41+
private synchronized static void setThreadLocalWebDriver(ManagedWebDriver managedWebDriver) {
42+
threadLocalWebDriver.set(managedWebDriver);
43+
}
44+
45+
public synchronized static ManagedWebDriver getManagedWebDriver() {
46+
return threadLocalWebDriver.get();
47+
}
48+
49+
@Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
50+
public void feature(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper, ManagedWebDriver managedWebDriver) {
51+
if(Utility.isLocal(managedWebDriver) && local==null){
52+
local = new Local();
53+
Utility.startLocal(local, managedWebDriver);
54+
}
55+
managedWebDriver.setTestName(pickleWrapper.getPickle().getName());
56+
setThreadLocalWebDriver(managedWebDriver);
57+
testNGCucumberRunner.runScenario(pickleWrapper.getPickle());
58+
}
59+
60+
@DataProvider(name = "scenarios", parallel = true)
61+
public Iterator<Object[]> scenarios() {
62+
Object[][] scenarios = testNGCucumberRunner.provideScenarios();
63+
return new LazyInitWebDriverIterator(true, scenarios);
64+
}
65+
66+
@AfterClass(alwaysRun = true)
67+
public void tearDownClass() {
68+
if(local != null){
69+
try {
70+
local.stop();
71+
} catch (Exception e) {
72+
throw new Error("Unable to stop BrowserStack Local.");
73+
}
74+
}
75+
if (testNGCucumberRunner == null) {
76+
return;
77+
}
78+
testNGCucumberRunner.finish();
79+
}
80+
81+
}

src/test/java/com/browserstack/TestRunner.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)