Skip to content

Commit e0c0291

Browse files
Added Vanilla Java Sample Project
1 parent d7a064a commit e0c0291

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@
5858
Visit the [BrowserStack Load-Testing Dashboard](https://load.browserstack.com/projects) to monitor and analyze your test runs.
5959

6060
---
61+

vanillajava/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# browserstack-selenium-load-testing-sample
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-selenium-load-testing-sample.git
13+
cd browserstack-selenium-load-testing-sample
14+
cd vanillajava
15+
```
16+
17+
2. **Install Maven dependencies**
18+
19+
```sh
20+
mvn compile
21+
```
22+
23+
3. **Install BrowserStack CLI**
24+
25+
Download the appropriate BrowserStack CLI binary based on your operating system:
26+
27+
- **macOS x86**
28+
[browserstack-cli-macOS-x86](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=x64)
29+
30+
- **macOS ARM**
31+
[browserstack-cli-macOS-arm](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=arm64)
32+
33+
- **Windows x86**
34+
[browserstack-cli-windows](https://load-api.browserstack.com/api/v1/binary?os=win&arch=x64)
35+
36+
- **Linux x86**
37+
[browserstack-cli-linux-x86](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=arm64)
38+
39+
- **Linux ARM**
40+
[browserstack-cli-linux-arm](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=x64)
41+
42+
> Place the downloaded `browserstack-cli` binary in the root of your project.
43+
44+
4. **Run tests using BrowserStack CLI**
45+
46+
```sh
47+
./browserstack-cli load run
48+
```
49+
50+
5. **View Test Results**
51+
52+
Visit the [BrowserStack Load-Testing Dashboard](https://load.browserstack.com/projects) to monitor and analyze your test runs.
53+
54+
---

vanillajava/browserstack-load.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# 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.
37+
# 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).
38+
files:
39+
dependencies:
40+
- ./pom.xml
41+
testConfigs: []
42+

vanillajava/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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://www.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<artifactId>selenium-vanillajava-example</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<selenium.version>4.15.0</selenium.version>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- Selenium WebDriver -->
20+
<dependency>
21+
<groupId>org.seleniumhq.selenium</groupId>
22+
<artifactId>selenium-java</artifactId>
23+
<version>${selenium.version}</version>
24+
</dependency>
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.11.0</version>
33+
<configuration>
34+
<source>${maven.compiler.source}</source>
35+
<target>${maven.compiler.target}</target>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-surefire-plugin</artifactId>
41+
<version>3.1.2</version>
42+
<configuration>
43+
<includes>
44+
<include>**/*Test.java</include>
45+
</includes>
46+
<useModulePath>false</useModulePath>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
52+
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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.RemoteWebDriver;
8+
9+
import java.net.URL;
10+
import java.time.Duration;
11+
12+
public class SimpleTest {
13+
14+
private static final String HUB_URL = "http://localhost:4444/wd/hub";
15+
16+
public static void main(String[] args) {
17+
SimpleTest test = new SimpleTest();
18+
19+
try {
20+
System.out.println("Starting Add to Cart test...");
21+
test.testAddToCartBStackDemo();
22+
System.out.println("Add to Cart test completed successfully!");
23+
24+
System.out.println("\nStarting Checkout Flow test...");
25+
test.testCheckoutFlowBStackDemo();
26+
System.out.println("Checkout Flow test completed successfully!");
27+
28+
System.out.println("\nAll tests passed!");
29+
} catch (Exception e) {
30+
System.err.println("Test failed: " + e.getMessage());
31+
e.printStackTrace();
32+
System.exit(1);
33+
}
34+
}
35+
36+
private WebDriver createDriver() throws Exception {
37+
try {
38+
System.out.println("Setting up Chrome options...");
39+
ChromeOptions chromeOptions = new ChromeOptions();
40+
chromeOptions.addArguments(
41+
"--headless",
42+
"--no-first-run",
43+
"--no-default-browser-check",
44+
"--disable-extensions",
45+
"--disable-default-apps",
46+
"--disable-gpu",
47+
"--disable-dev-shm-usage",
48+
"--disable-software-rasterizer",
49+
"--no-sandbox",
50+
"--disable-background-timer-throttling",
51+
"--disable-backgrounding-occluded-windows",
52+
"--disable-renderer-backgrounding",
53+
"--disable-features=TranslateUI",
54+
"--disable-ipc-flooding-protection",
55+
"--disable-web-security",
56+
"--disable-features=VizDisplayCompositor",
57+
"--disable-logging",
58+
"--silent"
59+
);
60+
61+
System.out.println("Connecting to Selenium Grid at: " + HUB_URL);
62+
WebDriver driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions);
63+
64+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
65+
driver.manage().window().maximize();
66+
System.out.println("Successfully connected to Selenium Grid!");
67+
return driver;
68+
} catch (Exception e) {
69+
System.err.println("Error during setup: " + e.getMessage());
70+
e.printStackTrace();
71+
throw e;
72+
}
73+
}
74+
75+
public void testAddToCartBStackDemo() throws Exception {
76+
WebDriver driver = createDriver();
77+
78+
try {
79+
driver.get("https://bstackdemo.com/");
80+
81+
WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p"));
82+
83+
String productToAdd = productNameElem.getText();
84+
85+
WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn"));
86+
addToCartBtn.click();
87+
88+
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
89+
90+
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"));
91+
String productInCart = productInCartElem.getText();
92+
93+
if (!productToAdd.equals(productInCart)) {
94+
throw new AssertionError("Expected: " + productToAdd + ", but got: " + productInCart);
95+
}
96+
System.out.println("Test passed: Add to cart works!");
97+
} finally {
98+
if (driver != null) {
99+
driver.quit();
100+
System.out.println("Browser closed");
101+
}
102+
}
103+
}
104+
105+
public void testCheckoutFlowBStackDemo() throws Exception {
106+
WebDriver driver = createDriver();
107+
try {
108+
driver.get("https://bstackdemo.com/");
109+
110+
driver.findElement(By.id("signin")).click();
111+
driver.findElement(By.cssSelector("#username svg")).click();
112+
driver.findElement(By.id("react-select-2-option-0-0")).click();
113+
driver.findElement(By.cssSelector("#password svg")).click();
114+
driver.findElement(By.id("react-select-3-option-0-0")).click();
115+
driver.findElement(By.id("login-btn")).click();
116+
117+
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
118+
119+
driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click();
120+
driver.findElement(By.cssSelector("div.float-cart__close-btn")).click();
121+
driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click();
122+
driver.findElement(By.cssSelector(".buy-btn")).click();
123+
124+
driver.findElement(By.id("firstNameInput")).sendKeys("first");
125+
driver.findElement(By.id("lastNameInput")).sendKeys("last");
126+
driver.findElement(By.id("addressLine1Input")).sendKeys("address");
127+
driver.findElement(By.id("provinceInput")).sendKeys("province");
128+
driver.findElement(By.id("postCodeInput")).sendKeys("pincode");
129+
130+
driver.findElement(By.id("checkout-shipping-continue")).click();
131+
String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText();
132+
133+
if (!"Your Order has been successfully placed.".equals(checkoutMessage)) {
134+
throw new AssertionError("Expected: 'Your Order has been successfully placed.', but got: " + checkoutMessage);
135+
}
136+
System.out.println("Test passed: Checkout flow works!");
137+
} finally {
138+
if (driver != null) {
139+
driver.quit();
140+
System.out.println("Browser closed");
141+
}
142+
}
143+
}
144+
}
145+

0 commit comments

Comments
 (0)