Skip to content

Commit 17317c0

Browse files
committed
add: junit-5 sample test
1 parent b97d1d1 commit 17317c0

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

junit-5/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 junit-5
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+
---

junit-5/pom.xml

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

0 commit comments

Comments
 (0)