diff --git a/.gitignore b/.gitignore index fc3475d..1120b9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -target/ -log +**/target/ +**/log +**/browserstack-cli +**/.DS_Store diff --git a/README.md b/README.md index f440653..990a282 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,24 @@ 1. **Clone the repository** ```sh - git clone https://github.com/browserstack/browserstack-playwright-load-testing-sample.git - cd testng-browserstack + git clone https://github.com/browserstack/browserstack-selenium-load-testing-sample.git + cd browserstack-selenium-load-testing-sample ``` -2. **Install Maven dependencies** +2. **Choose the framework to run** + + cd into the the respective framework folder, for instance to run the testng sample: + ```sh + cd testng + ``` + +3. **Install Maven dependencies** ```sh mvn compile ``` -3. **Install BrowserStack CLI** +4. **Install BrowserStack CLI** Download the appropriate BrowserStack CLI binary based on your operating system: @@ -40,14 +47,14 @@ > Place the downloaded `browserstack-cli` binary in the root of your project. -4. **Run tests using BrowserStack CLI** +5. **Run tests using BrowserStack CLI** ```sh ./browserstack-cli load run ``` -5. **View Test Results** +6. **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/junit-4/README.md b/junit-4/README.md new file mode 100644 index 0000000..5acdef7 --- /dev/null +++ b/junit-4/README.md @@ -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 junit-4 + ``` + +2. **Install Maven dependencies** + + ```sh + mvn compile + ``` + +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. + +--- diff --git a/junit-4/browserstack-load.yml b/junit-4/browserstack-load.yml new file mode 100644 index 0000000..558297a --- /dev/null +++ b/junit-4/browserstack-load.yml @@ -0,0 +1,44 @@ +# ============================= +# 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 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. +framework: junit + +# 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: [] diff --git a/junit-4/pom.xml b/junit-4/pom.xml new file mode 100644 index 0000000..6dcac32 --- /dev/null +++ b/junit-4/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + com.example + selenium-junit4-example + 1.0-SNAPSHOT + jar + + + 11 + 11 + UTF-8 + 4.15.0 + 4.13.2 + + + + + + org.seleniumhq.selenium + selenium-java + ${selenium.version} + + + + + junit + junit + ${junit4.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + + **/*Test.java + + + + + + diff --git a/junit-4/src/test/java/com/example/SimpleTest.java b/junit-4/src/test/java/com/example/SimpleTest.java new file mode 100644 index 0000000..612d298 --- /dev/null +++ b/junit-4/src/test/java/com/example/SimpleTest.java @@ -0,0 +1,114 @@ +package com.example; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.net.URL; +import java.time.Duration; + +import static org.junit.Assert.assertEquals; + +public class SimpleTest { + + private WebDriver driver; + private static final String HUB_URL = "http://localhost:4444/wd/hub"; + + @Before + public void setUp() throws Exception { + try { + System.out.println("Setting up Chrome options..."); + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments( + "--headless", + "--no-first-run", + "--no-default-browser-check", + "--disable-extensions", + "--disable-default-apps", + "--disable-gpu", + "--disable-dev-shm-usage", + "--disable-software-rasterizer", + "--no-sandbox", + "--disable-background-timer-throttling", + "--disable-backgrounding-occluded-windows", + "--disable-renderer-backgrounding", + "--disable-features=TranslateUI", + "--disable-ipc-flooding-protection", + "--disable-web-security", + "--disable-features=VizDisplayCompositor", + "--disable-logging", + "--silent" + ); + + System.out.println("Connecting to Selenium Grid at: " + HUB_URL); + driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + driver.manage().window().maximize(); + System.out.println("Successfully connected to Selenium Grid!"); + } catch (Exception e) { + System.err.println("Error during setup: " + e.getMessage()); + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAddToCartBStackDemo() { + driver.get("https://bstackdemo.com/"); + + WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p")); + String productToAdd = productNameElem.getText(); + + WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn")); + addToCartBtn.click(); + + 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")); + String productInCart = productInCartElem.getText(); + + assertEquals(productToAdd, productInCart); + System.out.println("Test passed: Add to cart works!"); + } + + @Test + public void testCheckoutFlowBStackDemo() { + driver.get("https://bstackdemo.com/"); + + driver.findElement(By.id("signin")).click(); + driver.findElement(By.cssSelector("#username svg")).click(); + driver.findElement(By.id("react-select-2-option-0-0")).click(); + driver.findElement(By.cssSelector("#password svg")).click(); + driver.findElement(By.id("react-select-3-option-0-0")).click(); + driver.findElement(By.id("login-btn")).click(); + + try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + + driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click(); + driver.findElement(By.cssSelector("div.float-cart__close-btn")).click(); + driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click(); + driver.findElement(By.cssSelector(".buy-btn")).click(); + + driver.findElement(By.id("firstNameInput")).sendKeys("first"); + driver.findElement(By.id("lastNameInput")).sendKeys("last"); + driver.findElement(By.id("addressLine1Input")).sendKeys("address"); + driver.findElement(By.id("provinceInput")).sendKeys("province"); + driver.findElement(By.id("postCodeInput")).sendKeys("pincode"); + + driver.findElement(By.id("checkout-shipping-continue")).click(); + String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText(); + assertEquals("Your Order has been successfully placed.", checkoutMessage); + System.out.println("Test passed: Checkout flow works!"); + } + + @After + public void tearDown() { + if (driver != null) { + driver.quit(); + System.out.println("Browser closed"); + } + } +} diff --git a/junit-5/README.md b/junit-5/README.md new file mode 100644 index 0000000..ebca6e2 --- /dev/null +++ b/junit-5/README.md @@ -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 junit-5 + ``` + +2. **Install Maven dependencies** + + ```sh + mvn compile + ``` + +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. + +--- diff --git a/junit-5/browserstack-load.yml b/junit-5/browserstack-load.yml new file mode 100644 index 0000000..558297a --- /dev/null +++ b/junit-5/browserstack-load.yml @@ -0,0 +1,44 @@ +# ============================= +# 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 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. +framework: junit + +# 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: [] diff --git a/junit-5/pom.xml b/junit-5/pom.xml new file mode 100644 index 0000000..ffca20a --- /dev/null +++ b/junit-5/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + com.example + selenium-junit5-example + 1.0-SNAPSHOT + jar + + + 11 + 11 + UTF-8 + 4.15.0 + 5.10.1 + + + + + + org.seleniumhq.selenium + selenium-java + ${selenium.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit.jupiter.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + **/*Test.java + + false + + + + + diff --git a/junit-5/src/test/java/com/example/SimpleTest.java b/junit-5/src/test/java/com/example/SimpleTest.java new file mode 100644 index 0000000..1d5be1d --- /dev/null +++ b/junit-5/src/test/java/com/example/SimpleTest.java @@ -0,0 +1,114 @@ +package com.example; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.net.URL; +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SimpleTest { + + private WebDriver driver; + private static final String HUB_URL = "http://localhost:4444/wd/hub"; + + @BeforeEach + void setUp() throws Exception { + try { + System.out.println("Setting up Chrome options..."); + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments( + "--headless", + "--no-first-run", + "--no-default-browser-check", + "--disable-extensions", + "--disable-default-apps", + "--disable-gpu", + "--disable-dev-shm-usage", + "--disable-software-rasterizer", + "--no-sandbox", + "--disable-background-timer-throttling", + "--disable-backgrounding-occluded-windows", + "--disable-renderer-backgrounding", + "--disable-features=TranslateUI", + "--disable-ipc-flooding-protection", + "--disable-web-security", + "--disable-features=VizDisplayCompositor", + "--disable-logging", + "--silent" + ); + + System.out.println("Connecting to Selenium Grid at: " + HUB_URL); + driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + driver.manage().window().maximize(); + System.out.println("Successfully connected to Selenium Grid!"); + } catch (Exception e) { + System.err.println("Error during setup: " + e.getMessage()); + e.printStackTrace(); + throw e; + } + } + + @Test + void testAddToCartBStackDemo() { + driver.get("https://bstackdemo.com/"); + + WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p")); + String productToAdd = productNameElem.getText(); + + WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn")); + addToCartBtn.click(); + + 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")); + String productInCart = productInCartElem.getText(); + + assertEquals(productToAdd, productInCart); + System.out.println("Test passed: Add to cart works!"); + } + + @Test + void testCheckoutFlowBStackDemo() { + driver.get("https://bstackdemo.com/"); + + driver.findElement(By.id("signin")).click(); + driver.findElement(By.cssSelector("#username svg")).click(); + driver.findElement(By.id("react-select-2-option-0-0")).click(); + driver.findElement(By.cssSelector("#password svg")).click(); + driver.findElement(By.id("react-select-3-option-0-0")).click(); + driver.findElement(By.id("login-btn")).click(); + + try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + + driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click(); + driver.findElement(By.cssSelector("div.float-cart__close-btn")).click(); + driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click(); + driver.findElement(By.cssSelector(".buy-btn")).click(); + + driver.findElement(By.id("firstNameInput")).sendKeys("first"); + driver.findElement(By.id("lastNameInput")).sendKeys("last"); + driver.findElement(By.id("addressLine1Input")).sendKeys("address"); + driver.findElement(By.id("provinceInput")).sendKeys("province"); + driver.findElement(By.id("postCodeInput")).sendKeys("pincode"); + + driver.findElement(By.id("checkout-shipping-continue")).click(); + String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText(); + assertEquals("Your Order has been successfully placed.", checkoutMessage); + System.out.println("Test passed: Checkout flow works!"); + } + + @AfterEach + void tearDown() { + if (driver != null) { + driver.quit(); + System.out.println("Browser closed"); + } + } +} diff --git a/testng/README.md b/testng/README.md new file mode 100644 index 0000000..9f4bc99 --- /dev/null +++ b/testng/README.md @@ -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 testng + ``` + +2. **Install Maven dependencies** + + ```sh + mvn compile + ``` + +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. + +--- diff --git a/browserstack-load.yml b/testng/browserstack-load.yml similarity index 94% rename from browserstack-load.yml rename to testng/browserstack-load.yml index 88ee9e9..0557adf 100644 --- a/browserstack-load.yml +++ b/testng/browserstack-load.yml @@ -33,13 +33,13 @@ regions: # 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: testng +# Set framework to the test framework used in your Selenium project. +framework: 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 + - ./pom.xml testConfigs: - - ./src/test/resources/testng.xml \ No newline at end of file + - ./src/test/resources/testng.xml diff --git a/pom.xml b/testng/pom.xml similarity index 100% rename from pom.xml rename to testng/pom.xml diff --git a/src/test/java/com/example/SimpleTest.java b/testng/src/test/java/com/example/SimpleTest.java similarity index 96% rename from src/test/java/com/example/SimpleTest.java rename to testng/src/test/java/com/example/SimpleTest.java index 3718721..4856111 100644 --- a/src/test/java/com/example/SimpleTest.java +++ b/testng/src/test/java/com/example/SimpleTest.java @@ -23,8 +23,7 @@ public class SimpleTest { public void setUp() throws Exception { try { System.out.println("Setting up Chrome options..."); - - // Try a simpler approach first - just basic capabilities + DesiredCapabilities caps = new DesiredCapabilities(); caps.setBrowserName("chrome"); @@ -125,8 +124,8 @@ public void testCheckoutFlowBStackDemo() { // Checkout driver.findElement(By.id("checkout-shipping-continue")).click(); - driver.findElement(By.xpath("//*[text()='Continue']")).click(); - driver.findElement(By.xpath("//*[text()='Orders']")).click(); + String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText(); + Assert.assertEquals(checkoutMessage, "Your Order has been successfully placed."); System.out.println("Test passed: Checkout flow works!"); } diff --git a/src/test/resources/testng.xml b/testng/src/test/resources/testng.xml similarity index 100% rename from src/test/resources/testng.xml rename to testng/src/test/resources/testng.xml