-
Notifications
You must be signed in to change notification settings - Fork 1
Added Vanilla Java Sample Project #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adityasonani-bs
wants to merge
3
commits into
main
Choose a base branch
from
vanilla-java-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # browserstack-selenium-load-testing-sample | ||
|
|
||
|  | ||
|
|
||
| ## 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 vanillajava | ||
| ``` | ||
|
|
||
| 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=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. | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # ============================= | ||
| # 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 | ||
|
|
||
| # 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: [] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <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://www.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example</groupId> | ||
| <artifactId>selenium-vanillajava-example</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <selenium.version>4.15.0</selenium.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- Selenium WebDriver --> | ||
| <dependency> | ||
| <groupId>org.seleniumhq.selenium</groupId> | ||
| <artifactId>selenium-java</artifactId> | ||
| <version>${selenium.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.11.0</version> | ||
| <configuration> | ||
| <source>${maven.compiler.source}</source> | ||
| <target>${maven.compiler.target}</target> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.1.2</version> | ||
| <configuration> | ||
| <includes> | ||
| <include>**/*Test.java</include> | ||
| </includes> | ||
| <useModulePath>false</useModulePath> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package com.example; | ||
|
|
||
| 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; | ||
|
|
||
adityasonani-bs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class SimpleTest { | ||
|
|
||
| private static final String HUB_URL = "http://localhost:4444/wd/hub"; | ||
|
|
||
| public static void main(String[] args) { | ||
| SimpleTest test = new SimpleTest(); | ||
|
|
||
| boolean allTestsPassed = true; | ||
|
|
||
| System.out.println("Starting Add to Cart test..."); | ||
| try { | ||
| test.testAddToCartBStackDemo(); | ||
| System.out.println("Add to Cart test completed successfully!"); | ||
| } catch (Exception e) { | ||
| System.err.println("Add to Cart test failed: " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| allTestsPassed = false; | ||
| } | ||
|
|
||
| System.out.println("\nStarting Checkout Flow test..."); | ||
| try { | ||
| test.testCheckoutFlowBStackDemo(); | ||
| System.out.println("Checkout Flow test completed successfully!"); | ||
| } catch (Exception e) { | ||
| System.err.println("Checkout Flow test failed: " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| allTestsPassed = false; | ||
| } | ||
|
|
||
| if (allTestsPassed) { | ||
| System.out.println("\nAll tests passed!"); | ||
| } else { | ||
| System.err.println("\nOne or more tests failed."); | ||
| System.exit(1); | ||
| } | ||
| } | ||
|
|
||
| private WebDriver createDriver() 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", | ||
adityasonani-bs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "--disable-features=VizDisplayCompositor", | ||
| "--disable-logging", | ||
| "--silent" | ||
| ); | ||
|
|
||
| System.out.println("Connecting to Selenium Grid at: " + HUB_URL); | ||
| WebDriver 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!"); | ||
| return driver; | ||
| } catch (Exception e) { | ||
| System.err.println("Error during setup: " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| public void testAddToCartBStackDemo() throws Exception { | ||
| WebDriver driver = createDriver(); | ||
|
|
||
| try { | ||
| 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(); | ||
|
|
||
| try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } | ||
adityasonani-bs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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(); | ||
|
|
||
| if (!productToAdd.equals(productInCart)) { | ||
| throw new AssertionError("Expected: " + productToAdd + ", but got: " + productInCart); | ||
| } | ||
| System.out.println("Test passed: Add to cart works!"); | ||
| } finally { | ||
| if (driver != null) { | ||
| driver.quit(); | ||
| System.out.println("Browser closed"); | ||
| } | ||
| } | ||
| } | ||
adityasonani-bs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public void testCheckoutFlowBStackDemo() throws Exception { | ||
| WebDriver driver = createDriver(); | ||
| try { | ||
| 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(); } | ||
adityasonani-bs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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(); | ||
|
|
||
| if (!"Your Order has been successfully placed.".equals(checkoutMessage)) { | ||
| throw new AssertionError("Expected: 'Your Order has been successfully placed.', but got: " + checkoutMessage); | ||
| } | ||
| System.out.println("Test passed: Checkout flow works!"); | ||
| } finally { | ||
| if (driver != null) { | ||
| driver.quit(); | ||
| System.out.println("Browser closed"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The browserstack-load.yml configuration is missing the 'framework' field that is present in all other test framework examples (junit-4, junit-5, testng). For vanilla Java without a test framework, you should either add a framework field with an appropriate value (possibly 'none' or 'vanilla') or explicitly document why it's omitted. This inconsistency may cause confusion or issues with the BrowserStack Load Testing service.