Skip to content
Merged
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
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# browserstack-cucumber-testng-load-testing-sample
Sample repository for cucumber-testng framework to do load testing.

![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 [email protected]:browserstack/browserstack-cucumber-testng-load-testing-sample.git
```

2. **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=x64)

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

> 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 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: 5

# 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: 5m

# 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: java

# Set framework to the test framework used in your Selenium project. Example: 'testng'.
framework: cucumber-testng

# 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:
- ./pom.xml
testConfigs:
- src/test/java/runners/TestRunner.java
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>cucumber-testng-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cucumber TestNG Selenium Sample</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.20.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.16.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.16.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<includes>
<include>**/TestRunner.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
12 changes: 12 additions & 0 deletions src/test/java/features/BStackDemo.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Add product to cart on bstack demo
As a user of the bstack demo site
I want to add a product to the cart
So that I can verify the product appears correctly

Scenario: Add first product to cart
Given I open the bstack demo homepage
Then the bstack demo title should be "StackDemo"
When I note the name of the first product
And I add the first product to the cart
Then the mini cart should be displayed
And the product name in the cart should match noted name
12 changes: 12 additions & 0 deletions src/test/java/runners/TestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
features = "src/test/java/features/BStackDemo.feature",
glue = {"stepdefinitions"},
plugin = {"pretty", "html:target/cucumber-reports.html"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
73 changes: 73 additions & 0 deletions src/test/java/stepdefinitions/bstack_test_add_to_cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package stepdefinitions;

import io.cucumber.java.After;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;

import java.net.URL;
import java.time.Duration;

public class bstack_test_add_to_cart {

private RemoteWebDriver driver;
private String notedProductName;

@Given("I open the bstack demo homepage")
public void i_open_the_bstack_demo_homepage() throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");

// Set the BrowserStack HUB URL
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.bstackdemo.com");
}

@Then("the bstack demo title should be {string}")
public void the_bstack_demo_title_should_be(String expected) {
Assert.assertTrue(driver.getTitle().contains(expected), "Expected title to contain: " + expected + " but was: " + driver.getTitle());
}

@When("I note the name of the first product")
public void i_note_the_name_of_the_first_product() {
WebElement nameEl = driver.findElement(By.xpath("//*[@id='1']/p"));
notedProductName = nameEl.getText();
Assert.assertNotNull(notedProductName, "Product name should not be null");
}

@And("I add the first product to the cart")
public void i_add_the_first_product_to_the_cart() {
driver.findElement(By.xpath("//*[@id='1']/div[4]"))
.click();
}

@Then("the mini cart should be displayed")
public void the_mini_cart_should_be_displayed() {
WebElement cart = driver.findElement(By.cssSelector(".float\\-cart__content"));
Assert.assertTrue(cart.isDisplayed(), "Mini cart not displayed");
}

@And("the product name in the cart should match noted name")
public void the_product_name_in_the_cart_should_match_noted_name() {
WebElement nameInCart = driver.findElement(By.xpath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"));
String cartName = nameInCart.getText();
Assert.assertEquals(cartName, notedProductName, "Product names differ between main page and cart");
}

@After
public void tearDown() {
if (driver != null) {
try { driver.quit(); } catch (Exception ignored) {}
driver = null;
}
}
}