Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
**/log
**/browserstack-cli
**/.DS_Store
**/node_modules/
**/package-lock.json
**/yarn.lock
**/pnpm-lock.yaml
54 changes: 54 additions & 0 deletions jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# browserstack-selenium-load-testing-sample

![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)

## Getting Started

### Run Sample Build

1. **Clone the repository**

```sh
git clone https://github.com/browserstack/browserstack-selenium-load-testing-sample.git
cd browserstack-selenium-load-testing-sample
cd jest
```

2. **Install Node dependencies**

```sh
npm install
```

3. **Install BrowserStack CLI**

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

- **macOS x86**
[browserstack-cli-macOS-x86](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=x64)

- **macOS ARM**
[browserstack-cli-macOS-arm](https://load-api.browserstack.com/api/v1/binary?os=macos&arch=arm64)

- **Windows x86**
[browserstack-cli-windows](https://load-api.browserstack.com/api/v1/binary?os=win&arch=x64)

- **Linux x86**
[browserstack-cli-linux-x86](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=arm64)

- **Linux ARM**
[browserstack-cli-linux-arm](https://load-api.browserstack.com/api/v1/binary?os=linux&arch=x64)

> Place the downloaded `browserstack-cli` binary in the root of your project.

4. **Run tests using BrowserStack CLI**

```sh
./browserstack-cli load run
```

5. **View Test Results**

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

---
48 changes: 48 additions & 0 deletions jest/browserstack-load.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY as env variables.
userName: BROWSERSTACK_USERNAME
accessKey: BROWSERSTACK_ACCESS_KEY

# ======================
# BrowserStack Reporting
# ======================
# The following parameters are used to set up reporting on BrowserStack Load Testing:
# Set 'projectName' to the name of your project. Example: 'Product ABC'. Tests under the same projectName will be grouped together.
projectName: Default Project

# Set 'testName' to the name of your test. Example: 'First Load Test'. Test runs with the same testName will be grouped together.
testName: Default Test

# ======================
# Set Load Configuration
# ======================
# The following parameters are used to set load configuration for your test:
# Set 'testType' to the type of load test that you want to execute. Example:'Playwright', 'Selenium'. This is a required parameter.
testType: Selenium

# Set 'vus' to the maximum number of virtual users to simulate during the test.
vus: 1

# Set 'duration' to the total duration of the entire test, in minutes and seconds. The test will run infinite iterations until the duration is met. Example: '2m', '3m 40s'. This is not a required parameter.
duration: 1m

# Set multiple regions from which you would want to generate the load (percent should total 100 across all loadzones).
regions:
- loadzone: us-east-1
percent: 100

# Set language to the programming language used in your project. Example: 'java', 'nodejs'.
language: nodejs

# Set framework to the test framework used in your Selenium project.
framework: jest

# 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.
# 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).
files:
dependencies:
- ./package.json
testConfigs:
- ./jest.config.js
8 changes: 8 additions & 0 deletions jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest runs with --runInBand on BrowserStack so the single Selenium session
// per pod is never contended. testEnvironment must be "node" — Jest drives a
// remote WebDriver, not the in-process DOM.
module.exports = {
testEnvironment: "node",
testMatch: ["**/tests/**/*.test.js", "**/?(*.)+(spec|test).js"],
testTimeout: 120000,
};
13 changes: 13 additions & 0 deletions jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "browserstack-jest-load-testing-sample",
"version": "1.0.0",
"private": true,
"description": "Sample Jest + selenium-webdriver project for BrowserStack Load Testing",
"scripts": {
"test": "jest --runInBand --passWithNoTests=false"
},
"dependencies": {
"jest": "^29.7.0",
"selenium-webdriver": "^4.21.0"
}
}
46 changes: 46 additions & 0 deletions jest/tests/add-to-cart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { Builder, By } = require("selenium-webdriver");

// On BrowserStack Load Testing, the Selenium pod exposes a hub at
// http://localhost:4444/wd/hub. A plain `forBrowser("chrome").build()`
// would try to spawn a local browser the pod does not have.
const HUB_URL = "http://localhost:4444/wd/hub";

describe("BStackDemo test add to cart", () => {
let driver;

beforeAll(async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Driver lifecycle should be per-test, not shared across the file

Sibling samples use @BeforeMethod / @BeforeEach / @Before — a fresh driver per @Test. This file uses beforeAll / afterAll, so both test() blocks share one session. State from add-to-cart leaks into checkout (cart contents, cookies, scroll position). Swap to beforeEach / afterEach for parity with the other framework samples. Same change needed in checkout.test.js.

driver = await new Builder()
.usingServer(HUB_URL)
.forBrowser("chrome")
.build();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing implicit wait + window maximize (sibling parity)

All sibling samples configure a 10-second implicit wait and maximize the window after driver build:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();

Without these, findElement races against page render — flakes the first run on a fresh pod. Add after .build():

await driver.manage().setTimeouts({ implicit: 10000 });
await driver.manage().window().maximize();

Same change needed in checkout.test.js.

});

afterAll(async () => {
if (driver) await driver.quit();
});

test("should add product to cart successfully", async () => {
// visit the site
await driver.get("https://bstackdemo.com/");

// get name of product we want to add to cart
const productElement = await driver.findElement(By.css("#\\33 > p"));
const productToAdd = await productElement.getText();

// click on add to cart
await driver.findElement(By.css("#\\33 > .shelf-item__buy-btn")).click();

// get name of item in cart
const cartItemElement = await driver.findElement(
By.css(
"#__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",
),
);
const productInCart = await cartItemElement.getText();

// check if product in cart is same as one added
expect(productInCart).toBe(productToAdd);
});
});
55 changes: 55 additions & 0 deletions jest/tests/checkout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { Builder, By } = require("selenium-webdriver");

const HUB_URL = "http://localhost:4444/wd/hub";

describe("BStackDemo test checkout flow", () => {
let driver;

beforeAll(async () => {
driver = await new Builder()
.usingServer(HUB_URL)
.forBrowser("chrome")
.build();
});

afterAll(async () => {
if (driver) await driver.quit();
});

test("should complete checkout flow successfully", async () => {
// visit the site
await driver.get("https://bstackdemo.com/");

// sign in
await driver.findElement(By.id("signin")).click();
await driver.findElement(By.css("#username svg")).click();
await driver.findElement(By.id("react-select-2-option-0-0")).click();
await driver.findElement(By.css("#password svg")).click();
await driver.findElement(By.id("react-select-3-option-0-0")).click();

await driver.findElement(By.id("login-btn")).click();
await driver.sleep(500);

// click on buy item
await driver.findElement(By.css("#\\31 > .shelf-item__buy-btn")).click();
await driver.findElement(By.css(".float-cart__close-btn")).click();
await driver.findElement(By.css("#\\32 > .shelf-item__buy-btn")).click();
await driver.findElement(By.css(".buy-btn")).click();

// add address details
await driver.findElement(By.id("firstNameInput")).sendKeys("first");
await driver.findElement(By.id("lastNameInput")).sendKeys("last");
await driver.findElement(By.id("addressLine1Input")).sendKeys("address");
await driver.findElement(By.id("provinceInput")).sendKeys("province");
await driver.findElement(By.id("postCodeInput")).sendKeys("pincode");

// checkout
await driver.findElement(By.id("checkout-shipping-continue")).click();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing assertion + wrong final-step flow

All four sibling samples (java/, junit-4/, junit-5/, testng/) end the checkout test by asserting on the order confirmation message:

String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText();
assertEquals("Your Order has been successfully placed.", checkoutMessage);

This jest version clicks checkout-shipping-continue → an xpath Continue → an xpath Orders and exits with no expect(). A silent failure mid-flow is the only signal a successful run gives — passing tests prove nothing.

Suggested change:

await driver.findElement(By.id("checkout-shipping-continue")).click();
const checkoutMessage = await driver
  .findElement(By.id("confirmation-message"))
  .getText();
expect(checkoutMessage).toBe("Your Order has been successfully placed.");

Drop the two trailing xpath clicks — siblings do not have them.

await driver
.findElement(By.xpath("//*[contains(text(),'Continue')]"))
.click();
await driver
.findElement(By.xpath("//*[contains(text(),'Orders')]"))
.click();
});
});
Loading