Skip to content

Commit 0880a51

Browse files
committed
added test sample for cucumber-testng
1 parent d89ed8d commit 0880a51

File tree

12 files changed

+123
-439
lines changed

12 files changed

+123
-439
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1414
hs_err_pid*
1515
target/
16+
logs/
1617
/.idea/
1718
*.iml
1819

browserstack.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and acccessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: YOUR_USERNAME
7+
accessKey: YOUR_ACCESS_KEY
8+
9+
# ======================
10+
# Organizing your tests
11+
# ======================
12+
# Use `projectName`, `buildName`, `name` capabilities to organise your tests
13+
# `name` is the name of your test sessions and is automatically picked from your
14+
# test name and doesn't need to be set manually when using BrowserStack SDK
15+
# `buildName` is used to name your CI/CD job or the execution of your test suite.
16+
# Ensure you add a dynamic identifier, like an incremental build number from your
17+
# CI/CD or timestamp at the end of every build; otherwise tests from different
18+
# executions will be grouped together on BrowserStack
19+
buildName: browserstack-build-124
20+
# Use `projectName` to set the name of your project. Example, Marketing Website
21+
projectName: BrowserStack Samples
22+
23+
# Use `framework` to set the framework of your project. Example, testng, cucumber, cucumber-testng
24+
framework: cucumber-testng
25+
26+
# =======================================
27+
# Platforms (Browsers / Devices to test)
28+
# =======================================
29+
# Platforms object contains all the browser / device combinations you want to test on.
30+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
31+
platforms:
32+
- os: OS X
33+
osVersion: Big Sur
34+
browser: Chrome
35+
browserVersion: latest
36+
- os: Windows
37+
osVersion: 10
38+
browser: Edge
39+
browserVersion: latest
40+
- device: Samsung Galaxy S22 Ultra
41+
browserName: chrome # Try 'samsung' for Samsung browser
42+
osVersion: 12.0
43+
44+
# =======================
45+
# Parallels per Platform
46+
# =======================
47+
# The number of parallel threads to be used for each platform set.
48+
# BrowserStack's SDK runner will select the best strategy based on the configured value
49+
#
50+
# Example 1 - If you have configured 3 platforms and set `parallelsPerPlatform` as 2, a total of 6 (2 * 3) parallel threads will be used on BrowserStack
51+
#
52+
# Example 2 - If you have configured 1 platform and set `parallelsPerPlatform` as 5, a total of 5 (1 * 5) parallel threads will be used on BrowserStack
53+
parallelsPerPlatform: 1
54+
55+
# ==========================================
56+
# BrowserStack Local
57+
# (For localhost, staging/private websites)
58+
# ==========================================
59+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
60+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
61+
browserstackLocal: false # <boolean> (Default false)
62+
# browserStackLocalOptions:
63+
# Options to be passed to BrowserStack local in-case of advanced configurations
64+
# - localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
65+
# - forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
66+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
67+
68+
# ===================
69+
# Debugging features
70+
# ===================
71+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
72+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
73+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
74+
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)

pom.xml

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,10 @@
1919
<selenium-java.version>3.141.59</selenium-java.version>
2020
<browserstack-local-java.version>1.0.6</browserstack-local-java.version>
2121
<json-simple.version>1.1.1</json-simple.version>
22-
<slf4j.version>1.7.30</slf4j.version>
22+
<browserstack-java-sdk.version>LATEST</browserstack-java-sdk.version>
2323
</properties>
2424

2525
<dependencies>
26-
27-
<dependency>
28-
<groupId>org.slf4j</groupId>
29-
<artifactId>slf4j-api</artifactId>
30-
<version>${slf4j.version}</version>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.slf4j</groupId>
34-
<artifactId>slf4j-simple</artifactId>
35-
<version>${slf4j.version}</version>
36-
</dependency>
37-
3826
<dependency>
3927
<groupId>org.testng</groupId>
4028
<artifactId>testng</artifactId>
@@ -64,7 +52,12 @@
6452
<artifactId>browserstack-local-java</artifactId>
6553
<version>${browserstack-local-java.version}</version>
6654
</dependency>
67-
55+
<dependency>
56+
<groupId>com.browserstack</groupId>
57+
<artifactId>browserstack-java-sdk</artifactId>
58+
<version>${browserstack-java-sdk.version}</version>
59+
<scope>compile</scope>
60+
</dependency>
6861
<dependency>
6962
<groupId>com.googlecode.json-simple</groupId>
7063
<artifactId>json-simple</artifactId>
@@ -93,37 +86,23 @@
9386
</build>
9487
<profiles>
9588
<profile>
96-
<id>single</id>
89+
<id>sample-test</id>
9790
<activation>
9891
<activeByDefault>true</activeByDefault>
9992
</activation>
10093
<build>
10194
<plugins>
10295
<plugin>
103-
<groupId>org.apache.maven.plugins</groupId>
104-
<artifactId>maven-surefire-plugin</artifactId>
105-
<version>3.0.0-M5</version>
106-
<configuration>
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>
118-
</configuration>
96+
<artifactId>maven-dependency-plugin</artifactId>
97+
<executions>
98+
<execution>
99+
<id>getClasspathFilenames</id>
100+
<goals>
101+
<goal>properties</goal>
102+
</goals>
103+
</execution>
104+
</executions>
119105
</plugin>
120-
</plugins>
121-
</build>
122-
</profile>
123-
<profile>
124-
<id>parallel</id>
125-
<build>
126-
<plugins>
127106
<plugin>
128107
<groupId>org.apache.maven.plugins</groupId>
129108
<artifactId>maven-surefire-plugin</artifactId>
@@ -133,28 +112,32 @@
133112
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
134113
</suiteXmlFiles>
135114
<argLine>
136-
-Ddataproviderthreadcount=4
115+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
137116
</argLine>
138117
<systemPropertyVariables>
139-
<caps>src/test/resources/conf/parallel.conf.json</caps>
140118
<cucumber.publish.quiet>true</cucumber.publish.quiet>
141119
<cucumber.features>src/test/resources/features/test</cucumber.features>
142120
</systemPropertyVariables>
143-
<properties>
144-
<property>
145-
<name>parallel</name>
146-
<value>methods</value>
147-
</property>
148-
</properties>
149121
</configuration>
150122
</plugin>
151123
</plugins>
152124
</build>
153125
</profile>
154126
<profile>
155-
<id>local</id>
127+
<id>sample-local-test</id>
156128
<build>
157129
<plugins>
130+
<plugin>
131+
<artifactId>maven-dependency-plugin</artifactId>
132+
<executions>
133+
<execution>
134+
<id>getClasspathFilenames</id>
135+
<goals>
136+
<goal>properties</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
158141
<plugin>
159142
<groupId>org.apache.maven.plugins</groupId>
160143
<artifactId>maven-surefire-plugin</artifactId>
@@ -164,10 +147,9 @@
164147
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
165148
</suiteXmlFiles>
166149
<argLine>
167-
-Ddataproviderthreadcount=1
150+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
168151
</argLine>
169152
<systemPropertyVariables>
170-
<caps>src/test/resources/conf/local.conf.json</caps>
171153
<cucumber.publish.quiet>true</cucumber.publish.quiet>
172154
<cucumber.features>src/test/resources/features/localtest</cucumber.features>
173155
</systemPropertyVariables>
Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,15 @@
11
package com.browserstack;
22

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;
3+
import io.cucumber.testng.*;
184

195
@CucumberOptions(
206
glue = "com.browserstack.stepdefs",
217
plugin = {
228
"pretty",
23-
"html:reports/tests/cucumber/html",
24-
"timeline:reports/tests/cucumber/timeline",
25-
"junit:reports/tests/cucumber/junit/cucumber.xml",
9+
"html:reports/tests/cucumber/cucumber-pretty.html",
2610
"testng:reports/tests/cucumber/testng/cucumber.xml",
27-
"json:reports/tests/cucumber/json/cucumber.json"
11+
"json:reports/tests/cucumber/json/cucumberTestReport.json",
12+
"message:reports/tests/cucumber/message.txt"
2813
}
2914
)
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-
//Get Iterator of Object arrays consisting PickleWrapper, FeatureWrapper and ManagedWebDriver
64-
return new LazyInitWebDriverIterator(scenarios);
65-
}
66-
67-
@AfterClass(alwaysRun = true)
68-
public void tearDownClass() {
69-
if(local != null){
70-
try {
71-
local.stop();
72-
} catch (Exception e) {
73-
throw new Error("Unable to stop BrowserStack Local.");
74-
}
75-
}
76-
if (testNGCucumberRunner == null) {
77-
return;
78-
}
79-
testNGCucumberRunner.finish();
80-
}
81-
82-
}
15+
public class RunWebDriverCucumberTests extends AbstractTestNGCucumberTests {}

src/test/java/com/browserstack/stepdefs/StackDemoSteps.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
package com.browserstack.stepdefs;
22

3-
import com.browserstack.RunWebDriverCucumberTests;
43
import com.browserstack.pageobjects.HomePage;
5-
import com.browserstack.util.Utility;
64
import io.cucumber.java.After;
75
import io.cucumber.java.Before;
86
import io.cucumber.java.Scenario;
97
import io.cucumber.java.en.Given;
108
import io.cucumber.java.en.Then;
119
import io.cucumber.java.en.When;
1210
import org.openqa.selenium.WebDriver;
11+
import org.openqa.selenium.remote.DesiredCapabilities;
12+
import org.openqa.selenium.remote.RemoteWebDriver;
1313

14-
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.assertTrue;
14+
import java.net.MalformedURLException;
15+
import java.net.URL;
16+
17+
import org.testng.Assert;
1618

1719
public class StackDemoSteps {
1820
private WebDriver driver;
1921
private HomePage homePage;
2022

2123
@Before
22-
public void setUp() {
23-
driver = RunWebDriverCucumberTests.getManagedWebDriver().getWebDriver();
24+
public void setUp() throws MalformedURLException {
25+
DesiredCapabilities capabilities = new DesiredCapabilities();
26+
driver = new RemoteWebDriver(
27+
new URL("https://hub.browserstack.com/wd/hub"), capabilities);
2428
homePage = new HomePage(driver);
2529
}
2630

@@ -40,21 +44,16 @@ public void I_select_a_product_and_add_to_cart() throws Throwable {
4044
@Then("the product should be added to cart")
4145
public void product_should_be_added_to_cart() {
4246
homePage.waitForCartToOpen();
43-
assertEquals(homePage.getSelectedProductName(), homePage.getProductCartText());
47+
Assert.assertEquals(homePage.getSelectedProductName(), homePage.getProductCartText());
4448
}
4549

4650
@Then("the page should contain '(.+)'$")
4751
public void page_should_contain(String expectedTitle) {
48-
assertTrue(driver.getPageSource().contains(expectedTitle));
52+
Assert.assertTrue(driver.getPageSource().contains(expectedTitle));
4953
}
5054

5155
@After
5256
public void teardown(Scenario scenario) throws Exception {
53-
if (scenario.isFailed()) {
54-
Utility.setSessionStatus(driver, "failed", String.format("%s failed.", scenario.getName()));
55-
} else {
56-
Utility.setSessionStatus(driver, "passed", String.format("%s passed.", scenario.getName()));
57-
}
5857
Thread.sleep(2000);
5958
driver.quit();
6059
}

0 commit comments

Comments
 (0)