Skip to content

Commit 15a2e9b

Browse files
agarneha1331francisf
authored andcommitted
Made same as testng
1 parent 8e1aa69 commit 15a2e9b

7 files changed

Lines changed: 208 additions & 0 deletions

File tree

browserstack.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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-1
20+
# Use `projectName` to set the name of your project. Example, Marketing Website
21+
projectName: BrowserStack Samples
22+
# Use `framework` to set the framework of your project. Example, testng, cucumber, cucumber-testng
23+
framework: testng
24+
25+
# =======================================
26+
# Platforms (Browsers / Devices to test)
27+
# =======================================
28+
# Platforms object contains all the browser / device combinations you want to test on.
29+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
30+
platforms:
31+
- os: OS X
32+
osVersion: Big Sur
33+
browserName: Chrome
34+
browserVersion: latest
35+
- os: Windows
36+
osVersion: 10
37+
browserName: Edge
38+
browserVersion: latest
39+
- device: Samsung Galaxy S22 Ultra
40+
browserName: chrome # Try 'samsung' for Samsung browser
41+
osVersion: 12.0
42+
43+
# =======================
44+
# Parallels per Platform
45+
# =======================
46+
# The number of parallel threads to be used for each platform set.
47+
# BrowserStack's SDK runner will select the best strategy based on the configured value
48+
#
49+
# 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
50+
#
51+
# 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
52+
parallelsPerPlatform: 1
53+
54+
# ==========================================
55+
# BrowserStack Local
56+
# (For localhost, staging/private websites)
57+
# ==========================================
58+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
59+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
60+
browserstackLocal: true # <boolean> (Default false)
61+
#browserStackLocalOptions:
62+
# Options to be passed to BrowserStack local in-case of advanced configurations
63+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
64+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
65+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
66+
67+
# ===================
68+
# Debugging features
69+
# ===================
70+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
71+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
72+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
73+
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)

build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories { mavenCentral() }
6+
7+
dependencies {
8+
implementation 'org.testng:testng:7.4.0'
9+
implementation 'commons-io:commons-io:1.3.2'
10+
implementation 'org.seleniumhq.selenium:selenium-java:4.1.4'
11+
implementation 'com.browserstack:browserstack-local-java:1.0.6'
12+
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
13+
compileOnly 'com.browserstack:browserstack-java-sdk:latest.release'
14+
}
15+
16+
group = 'com.browserstack'
17+
version = '1.0-SNAPSHOT'
18+
description = 'testng-browserstack'
19+
sourceCompatibility = '1.8'
20+
21+
def browserstackSDKArtifact = configurations.compileClasspath.resolvedConfiguration.resolvedArtifacts.find { it.name == 'browserstack-java-sdk' }
22+
23+
tasks.withType(JavaCompile) {
24+
options.encoding = 'UTF-8'
25+
}
26+
27+
task sampleTest(type: Test) {
28+
useTestNG() {
29+
dependsOn cleanTest
30+
useDefaultListeners = true
31+
suites "config/sample-test.testng.xml"
32+
jvmArgs "-javaagent:${browserstackSDKArtifact.file}"
33+
}
34+
}
35+
36+
task sampleLocalTest(type: Test) {
37+
useTestNG() {
38+
dependsOn cleanTest
39+
useDefaultListeners = true
40+
suites "config/sample-local-test.testng.xml"
41+
jvmArgs "-javaagent:${browserstackSDKArtifact.file}"
42+
}
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Local">
4+
<test name="LocalTest">
5+
<classes>
6+
<class name="com.browserstack.LocalTest"/>
7+
</classes>
8+
</test>
9+
</suite>

config/sample-test.testng.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Cross-Platform">
4+
<test name="BStackDemo">
5+
<classes>
6+
<class name="com.browserstack.BStackDemoTest" />
7+
</classes>
8+
</test>
9+
</suite>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.browserstack;
2+
3+
import com.browserstack.BrowserStackRemoteTest;
4+
import org.openqa.selenium.By;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
8+
public class BStackDemoTest extends BrowserStackRemoteTest {
9+
@Test
10+
public void addProductToCart() throws Exception {
11+
// navigate to bstackdemo
12+
driver.get("https://www.bstackdemo.com");
13+
14+
// Check the title
15+
Assert.assertTrue(driver.getTitle().matches("StackDemo"));
16+
17+
// Save the text of the product for later verify
18+
String productOnScreenText = driver.findElement(By.xpath("//*[@id=\"1\"]/p")).getText();
19+
// Click on add to cart button
20+
driver.findElement(By.xpath("//*[@id=\"1\"]/div[4]")).click();
21+
22+
// See if the cart is opened or not
23+
Assert.assertTrue(driver.findElement(By.className("float-cart__content")).isDisplayed());
24+
25+
// Check the product inside the cart is same as of the main page
26+
String productOnCartText = driver.findElement(By.xpath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")).getText();
27+
Assert.assertEquals(productOnScreenText, productOnCartText);
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.browserstack;
2+
3+
import java.net.URL;
4+
import java.util.HashMap;
5+
6+
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.MutableCapabilities;
8+
import org.openqa.selenium.remote.RemoteWebDriver;
9+
import org.testng.annotations.AfterMethod;
10+
import org.testng.annotations.BeforeMethod;
11+
12+
public class BrowserStackRemoteTest {
13+
public WebDriver driver;
14+
15+
@BeforeMethod(alwaysRun = true)
16+
@SuppressWarnings("unchecked")
17+
public void setUp() throws Exception {
18+
MutableCapabilities capabilities = new MutableCapabilities();
19+
HashMap<String, String> bstackOptionsMap = new HashMap<String, String>();
20+
bstackOptionsMap.put("source", "java-selenium:sample-main:v1.0");
21+
capabilities.setCapability("bstack:options", bstackOptionsMap);
22+
driver = new RemoteWebDriver(
23+
new URL("https://hub.browserstack.com/wd/hub"), capabilities);
24+
}
25+
26+
@AfterMethod(alwaysRun = true)
27+
public void tearDown() throws Exception {
28+
driver.quit();
29+
}
30+
}
31+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.browserstack;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class LocalTest extends BrowserStackRemoteTest {
7+
8+
@Test
9+
public void test() throws Exception {
10+
driver.get("http://bs-local.com:45454/");
11+
12+
Assert.assertTrue(driver.getTitle().contains("BrowserStack Local"));
13+
}
14+
}

0 commit comments

Comments
 (0)