Skip to content

Commit d7a064a

Browse files
authored
Merge pull request #2 from browserstack/LTS_1050_junit_sample
Lts 1050 junit sample
2 parents faa2bc7 + d1296ae commit d7a064a

File tree

15 files changed

+623
-17
lines changed

15 files changed

+623
-17
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
target/
2-
log
1+
**/target/
2+
**/log
3+
**/browserstack-cli
4+
**/.DS_Store

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99
1. **Clone the repository**
1010

1111
```sh
12-
git clone https://github.com/browserstack/browserstack-playwright-load-testing-sample.git
13-
cd testng-browserstack
12+
git clone https://github.com/browserstack/browserstack-selenium-load-testing-sample.git
13+
cd browserstack-selenium-load-testing-sample
1414
```
1515

16-
2. **Install Maven dependencies**
16+
2. **Choose the framework to run**
17+
18+
cd into the the respective framework folder, for instance to run the testng sample:
19+
```sh
20+
cd testng
21+
```
22+
23+
3. **Install Maven dependencies**
1724

1825
```sh
1926
mvn compile
2027
```
2128

22-
3. **Install BrowserStack CLI**
29+
4. **Install BrowserStack CLI**
2330

2431
Download the appropriate BrowserStack CLI binary based on your operating system:
2532

@@ -40,14 +47,14 @@
4047

4148
> Place the downloaded `browserstack-cli` binary in the root of your project.
4249
43-
4. **Run tests using BrowserStack CLI**
50+
5. **Run tests using BrowserStack CLI**
4451

4552
```sh
4653
./browserstack-cli load run
4754
```
4855

49-
5. **View Test Results**
56+
6. **View Test Results**
5057

5158
Visit the [BrowserStack Load-Testing Dashboard](https://load.browserstack.com/projects) to monitor and analyze your test runs.
5259

53-
---
60+
---

junit-4/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-4
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-4/browserstack-load.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.
37+
framework: junit
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: []

junit-4/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-junit4-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+
<junit4.version>4.13.2</junit4.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 4 -->
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>${junit4.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+
<!-- Default config works for JUnit 4; tests in src/test/java picked up automatically -->
53+
<includes>
54+
<include>**/*Test.java</include>
55+
</includes>
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.After;
4+
import org.junit.Before;
5+
import org.junit.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.Assert.assertEquals;
16+
17+
public class SimpleTest {
18+
19+
private WebDriver driver;
20+
private static final String HUB_URL = "http://localhost:4444/wd/hub";
21+
22+
@Before
23+
public 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+
public 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+
public 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+
@After
108+
public void tearDown() {
109+
if (driver != null) {
110+
driver.quit();
111+
System.out.println("Browser closed");
112+
}
113+
}
114+
}

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+
---

0 commit comments

Comments
 (0)