Skip to content

Commit aad08ab

Browse files
authored
Merge pull request #1 from browserstack/LTS-new-sample-repo
Added sample cucumber-testng project
2 parents 4e31f00 + d1769d1 commit aad08ab

File tree

6 files changed

+241
-1
lines changed

6 files changed

+241
-1
lines changed

README.md

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

browserstack-load.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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: 5
27+
28+
# 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.
29+
duration: 5m
30+
31+
# Set multiple regions from which you would want to generate the load (percent should total 100 across all loadzones).
32+
regions:
33+
- loadzone: us-east-1
34+
percent: 100
35+
36+
# Set language to the programming language used in your project. Example: 'java', 'nodejs'.
37+
language: java
38+
39+
# Set framework to the test framework used in your Selenium project. Example: 'testng'.
40+
framework: cucumber-testng
41+
42+
# 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.
43+
# 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).
44+
files:
45+
dependencies:
46+
- ./pom.xml
47+
testConfigs:
48+
- src/test/java/runners/TestRunner.java

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.example</groupId>
6+
<artifactId>cucumber-testng-sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>Cucumber TestNG Selenium Sample</name>
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.seleniumhq.selenium</groupId>
17+
<artifactId>selenium-java</artifactId>
18+
<version>4.20.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.cucumber</groupId>
22+
<artifactId>cucumber-java</artifactId>
23+
<version>7.16.1</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.cucumber</groupId>
27+
<artifactId>cucumber-testng</artifactId>
28+
<version>7.16.1</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.testng</groupId>
32+
<artifactId>testng</artifactId>
33+
<version>7.10.2</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-surefire-plugin</artifactId>
42+
<version>3.2.5</version>
43+
<configuration>
44+
<includes>
45+
<include>**/TestRunner.java</include>
46+
</includes>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Add product to cart on bstack demo
2+
As a user of the bstack demo site
3+
I want to add a product to the cart
4+
So that I can verify the product appears correctly
5+
6+
Scenario: Add first product to cart
7+
Given I open the bstack demo homepage
8+
Then the bstack demo title should be "StackDemo"
9+
When I note the name of the first product
10+
And I add the first product to the cart
11+
Then the mini cart should be displayed
12+
And the product name in the cart should match noted name
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package runners;
2+
3+
import io.cucumber.testng.AbstractTestNGCucumberTests;
4+
import io.cucumber.testng.CucumberOptions;
5+
6+
@CucumberOptions(
7+
features = "src/test/java/features/BStackDemo.feature",
8+
glue = {"stepdefinitions"},
9+
plugin = {"pretty", "html:target/cucumber-reports.html"}
10+
)
11+
public class TestRunner extends AbstractTestNGCucumberTests {
12+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package stepdefinitions;
2+
3+
import io.cucumber.java.After;
4+
import io.cucumber.java.en.And;
5+
import io.cucumber.java.en.Given;
6+
import io.cucumber.java.en.Then;
7+
import io.cucumber.java.en.When;
8+
import org.openqa.selenium.By;
9+
import org.openqa.selenium.WebDriver;
10+
import org.openqa.selenium.WebElement;
11+
import org.openqa.selenium.chrome.ChromeDriver;
12+
import org.openqa.selenium.remote.DesiredCapabilities;
13+
import org.openqa.selenium.remote.RemoteWebDriver;
14+
import org.testng.Assert;
15+
16+
import java.net.URL;
17+
import java.time.Duration;
18+
19+
public class bstack_test_add_to_cart {
20+
21+
private RemoteWebDriver driver;
22+
private String notedProductName;
23+
24+
@Given("I open the bstack demo homepage")
25+
public void i_open_the_bstack_demo_homepage() throws Exception {
26+
DesiredCapabilities caps = new DesiredCapabilities();
27+
caps.setBrowserName("chrome");
28+
29+
// Set the BrowserStack HUB URL
30+
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
31+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
32+
driver.get("https://www.bstackdemo.com");
33+
}
34+
35+
@Then("the bstack demo title should be {string}")
36+
public void the_bstack_demo_title_should_be(String expected) {
37+
Assert.assertTrue(driver.getTitle().contains(expected), "Expected title to contain: " + expected + " but was: " + driver.getTitle());
38+
}
39+
40+
@When("I note the name of the first product")
41+
public void i_note_the_name_of_the_first_product() {
42+
WebElement nameEl = driver.findElement(By.xpath("//*[@id='1']/p"));
43+
notedProductName = nameEl.getText();
44+
Assert.assertNotNull(notedProductName, "Product name should not be null");
45+
}
46+
47+
@And("I add the first product to the cart")
48+
public void i_add_the_first_product_to_the_cart() {
49+
driver.findElement(By.xpath("//*[@id='1']/div[4]"))
50+
.click();
51+
}
52+
53+
@Then("the mini cart should be displayed")
54+
public void the_mini_cart_should_be_displayed() {
55+
WebElement cart = driver.findElement(By.cssSelector(".float\\-cart__content"));
56+
Assert.assertTrue(cart.isDisplayed(), "Mini cart not displayed");
57+
}
58+
59+
@And("the product name in the cart should match noted name")
60+
public void the_product_name_in_the_cart_should_match_noted_name() {
61+
WebElement nameInCart = driver.findElement(By.xpath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"));
62+
String cartName = nameInCart.getText();
63+
Assert.assertEquals(cartName, notedProductName, "Product names differ between main page and cart");
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
if (driver != null) {
69+
try { driver.quit(); } catch (Exception ignored) {}
70+
driver = null;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)