Skip to content

Commit 9f60da0

Browse files
committed
Added selenium config
1 parent 577c4b4 commit 9f60da0

File tree

6 files changed

+314
-1
lines changed

6 files changed

+314
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
log
3+
src/.DS_Store
4+
src/test/.DS_Store
5+
src/test/java/.DS_Store
6+
src/test/java/com/.DS_Store

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
11
# browserstack-selenium-load-testing-sample
2-
Sample repository for Selenium load-testing test
2+
3+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
4+
5+
## Getting Started
6+
7+
### Run Sample Build
8+
9+
1. **Clone the repository**
10+
11+
```sh
12+
git clone https://github.com/browserstack/browserstack-playwright-load-testing-sample.git
13+
cd testng-browserstack
14+
```
15+
16+
2. **Install Maven dependencies**
17+
18+
```sh
19+
mvn compile
20+
```
21+
22+
3. **Install BrowserStack CLI**
23+
24+
Download the appropriate BrowserStack CLI binary based on your operating system:
25+
26+
- **macOS x86**
27+
[browserstack-cli-macOS-x86](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=x64)
28+
29+
- **macOS ARM**
30+
[browserstack-cli-macOS-arm](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=arm64)
31+
32+
- **Windows x86**
33+
[browserstack-cli-windows](https://load-api.browserstack.com/api/v1/binary?os=win&arch=x64)
34+
35+
- **Linux x86**
36+
[browserstack-cli-linux-x86](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=arm64)
37+
38+
- **Linux ARM**
39+
[browserstack-cli-linux-arm](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=x64)
40+
41+
> Place the downloaded `browserstack-cli` binary in the root of your project.
42+
43+
4. **Run tests using BrowserStack CLI**
44+
45+
```sh
46+
./browserstack-cli load run
47+
```
48+
49+
5. **View Test Results**
50+
51+
Visit the [BrowserStack Load-Testing Dashboard](https://load.browserstack.com/projects) to monitor and analyze your test runs.
52+
53+
---

browserstack-load.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY as env variables.
5+
userName: BROWSERSTACK_USERNAME
6+
accessKey: BROWSERSTACK_ACCESS_KEY
7+
8+
# ======================
9+
# BrowserStack Reporting
10+
# ======================
11+
# The following parameters are used to set up reporting on BrowserStack Load Testing:
12+
# Set 'projectName' to the name of your project. Example: 'Product ABC'. Tests under the same projectName will be grouped together.
13+
projectName: Default Project
14+
15+
# Set 'testName' to the name of your test. Example: 'First Load Test'. Test runs with the same testName will be grouped together.
16+
testName: Default Test
17+
18+
# ======================
19+
# Set Load Configuration
20+
# ======================
21+
# The following parameters are used to set load configuration for your test:
22+
# Set 'testType' to the type of load test that you want to execute. Example:'Playwright', 'Selenium'. This is a required parameter.
23+
testType: Selenium
24+
25+
# Set 'vus' to the maximum number of virtual users to simulate during the test.
26+
vus: 1
27+
28+
# Set multiple regions from which you would want to generate the load (percent should total 100 across all loadzones).
29+
regions:
30+
- loadzone: us-east-1
31+
percent: 100
32+
33+
# Set language to the programming language used in your project. Example: 'java', 'nodejs'.
34+
language: java
35+
36+
# Set framework to the test framework used in your Selenium project. Example: 'testng'.
37+
# framework: testng
38+
39+
# Add list of file paths under 'dependencies' to help set up the test environment by installing required packages. Example: path to 'pom.xml' for Java projects using Maven, path to 'package.json' for Node.js projects.
40+
# Add list of file paths under 'testConfigs' to define which configuration files should be used to run tests. Example: path to 'playwright.config.ts' for Playwright (Node.js), path to 'testng.xml' for Selenium (TestNG).
41+
files:
42+
dependencies:
43+
- ./pom.xml
44+
testConfigs:
45+
- ./src/test/resources/testng.xml

pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>selenium-testng-example</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<maven.compiler.source>11</maven.compiler.source>
15+
<maven.compiler.target>11</maven.compiler.target>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<selenium.version>4.15.0</selenium.version>
18+
<testng.version>7.8.0</testng.version>
19+
</properties>
20+
21+
<dependencies>
22+
<!-- Selenium WebDriver -->
23+
<dependency>
24+
<groupId>org.seleniumhq.selenium</groupId>
25+
<artifactId>selenium-java</artifactId>
26+
<version>${selenium.version}</version>
27+
</dependency>
28+
29+
<!-- TestNG -->
30+
<dependency>
31+
<groupId>org.testng</groupId>
32+
<artifactId>testng</artifactId>
33+
<version>${testng.version}</version>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<version>3.11.0</version>
43+
<configuration>
44+
<source>11</source>
45+
<target>11</target>
46+
</configuration>
47+
</plugin>
48+
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-surefire-plugin</artifactId>
52+
<version>3.1.2</version>
53+
<configuration>
54+
<suiteXmlFiles>
55+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
56+
</suiteXmlFiles>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.example;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.chrome.ChromeOptions;
7+
import org.openqa.selenium.remote.DesiredCapabilities;
8+
import org.openqa.selenium.remote.RemoteWebDriver;
9+
import org.testng.Assert;
10+
import org.testng.annotations.AfterMethod;
11+
import org.testng.annotations.BeforeMethod;
12+
import org.testng.annotations.Test;
13+
14+
import java.net.URL;
15+
import java.time.Duration;
16+
17+
public class SimpleTest {
18+
19+
private WebDriver driver;
20+
private static final String HUB_URL = "http://localhost:4444/wd/hub";
21+
22+
@BeforeMethod
23+
public void setUp() throws Exception {
24+
try {
25+
System.out.println("Setting up Chrome options...");
26+
27+
// Try a simpler approach first - just basic capabilities
28+
DesiredCapabilities caps = new DesiredCapabilities();
29+
caps.setBrowserName("chrome");
30+
31+
// Add Chrome options as a map
32+
ChromeOptions chromeOptions = new ChromeOptions();
33+
// Use timestamp for unique user data directory
34+
String uniqueUserDataDir = "/tmp/chrome-user-data-" + System.currentTimeMillis();
35+
chromeOptions.addArguments(
36+
"--headless",
37+
"--no-first-run",
38+
"--no-default-browser-check",
39+
"--disable-extensions",
40+
"--disable-default-apps",
41+
"--disable-gpu",
42+
"--disable-dev-shm-usage",
43+
"--disable-software-rasterizer",
44+
"--no-sandbox",
45+
"--disable-background-timer-throttling",
46+
"--disable-backgrounding-occluded-windows",
47+
"--disable-renderer-backgrounding",
48+
"--disable-features=TranslateUI",
49+
"--disable-ipc-flooding-protection",
50+
"--disable-web-security",
51+
"--disable-features=VizDisplayCompositor",
52+
"--disable-logging",
53+
"--silent"
54+
);
55+
56+
System.out.println("Chrome options configured. User data dir: " + uniqueUserDataDir);
57+
58+
// Merge the chrome options with capabilities
59+
60+
System.out.println("Connecting to Selenium Grid at: " + HUB_URL);
61+
driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions);
62+
63+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
64+
driver.manage().window().maximize();
65+
66+
System.out.println("Successfully connected to Selenium Grid!");
67+
68+
} catch (Exception e) {
69+
System.err.println("Error during setup: " + e.getMessage());
70+
e.printStackTrace();
71+
throw e;
72+
}
73+
}
74+
75+
@Test
76+
public void testAddToCartBStackDemo() {
77+
// Visit BStackDemo
78+
driver.get("https://bstackdemo.com/");
79+
80+
// Get name of product to add to cart (first product)
81+
WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p"));
82+
String productToAdd = productNameElem.getText();
83+
84+
// Click on add to cart
85+
WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn"));
86+
addToCartBtn.click();
87+
88+
// Get name of item in cart
89+
WebElement productInCartElem = driver.findElement(By.cssSelector("#__next > div > div > div.float-cart.float-cart--open > div.float-cart__content > div.float-cart__shelf-container > div > div.shelf-item__details > p.title"));
90+
String productInCart = productInCartElem.getText();
91+
92+
// Check if product in cart is same as one added
93+
Assert.assertEquals(productInCart, productToAdd);
94+
System.out.println("Test passed: Add to cart works!");
95+
}
96+
97+
@Test
98+
public void testCheckoutFlowBStackDemo() {
99+
// Visit BStackDemo
100+
driver.get("https://bstackdemo.com/");
101+
102+
// Sign in
103+
driver.findElement(By.id("signin")).click();
104+
driver.findElement(By.cssSelector("#username svg")).click();
105+
driver.findElement(By.id("react-select-2-option-0-0")).click();
106+
driver.findElement(By.cssSelector("#password svg")).click();
107+
driver.findElement(By.id("react-select-3-option-0-0")).click();
108+
driver.findElement(By.id("login-btn")).click();
109+
110+
// Wait for login
111+
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
112+
113+
// Click on buy item
114+
driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click();
115+
driver.findElement(By.cssSelector("div.float-cart__close-btn")).click();
116+
driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click();
117+
driver.findElement(By.cssSelector(".buy-btn")).click();
118+
119+
// Add address details
120+
driver.findElement(By.id("firstNameInput")).sendKeys("first");
121+
driver.findElement(By.id("lastNameInput")).sendKeys("last");
122+
driver.findElement(By.id("addressLine1Input")).sendKeys("address");
123+
driver.findElement(By.id("provinceInput")).sendKeys("province");
124+
driver.findElement(By.id("postCodeInput")).sendKeys("pincode");
125+
126+
// Checkout
127+
driver.findElement(By.id("checkout-shipping-continue")).click();
128+
driver.findElement(By.xpath("//*[text()='Continue']")).click();
129+
driver.findElement(By.xpath("//*[text()='Orders']")).click();
130+
131+
System.out.println("Test passed: Checkout flow works!");
132+
}
133+
134+
@AfterMethod
135+
public void tearDown() {
136+
if (driver != null) {
137+
driver.quit();
138+
System.out.println("Browser closed");
139+
}
140+
}
141+
}

src/test/resources/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="Simple Selenium Test Suite">
4+
<test name="Basic Tests">
5+
<classes>
6+
<class name="com.example.SimpleTest"/>
7+
</classes>
8+
</test>
9+
</suite>

0 commit comments

Comments
 (0)