diff --git a/README.md b/README.md index 8633f02..16150b5 100644 --- a/README.md +++ b/README.md @@ -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 git@github.com: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. + +--- \ No newline at end of file diff --git a/browserstack-load.yml b/browserstack-load.yml new file mode 100644 index 0000000..81fe393 --- /dev/null +++ b/browserstack-load.yml @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..826bd93 --- /dev/null +++ b/pom.xml @@ -0,0 +1,51 @@ + + 4.0.0 + com.example + cucumber-testng-sample + 1.0-SNAPSHOT + jar + Cucumber TestNG Selenium Sample + + 1.8 + 1.8 + + + + org.seleniumhq.selenium + selenium-java + 4.20.0 + + + io.cucumber + cucumber-java + 7.16.1 + + + io.cucumber + cucumber-testng + 7.16.1 + + + org.testng + testng + 7.10.2 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + **/TestRunner.java + + + + + + diff --git a/src/test/java/features/BStackDemo.feature b/src/test/java/features/BStackDemo.feature new file mode 100644 index 0000000..8588d68 --- /dev/null +++ b/src/test/java/features/BStackDemo.feature @@ -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 diff --git a/src/test/java/runners/TestRunner.java b/src/test/java/runners/TestRunner.java new file mode 100644 index 0000000..7a2b02b --- /dev/null +++ b/src/test/java/runners/TestRunner.java @@ -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 { +} diff --git a/src/test/java/stepdefinitions/bstack_test_add_to_cart.java b/src/test/java/stepdefinitions/bstack_test_add_to_cart.java new file mode 100644 index 0000000..3aed052 --- /dev/null +++ b/src/test/java/stepdefinitions/bstack_test_add_to_cart.java @@ -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; + } + } +}