Run Selenium Tests with Java

Run Selenium Tests with Java

Your guide to running tests using Java on BrowserStack’s Selenium Grid of 3000+ real devices and desktop browsers.

This section covers running your first build with BrowserStack’s sample repo. To run builds with your existing test suite(s), skip to integrate your test suite.

Prerequisites

  • BrowserStack Username and Access key. You can find this in your account profile. If you do not have an account yet, please sign up for a Free Trial.

  • TestNG v6.8+, Java v8+ (if using Gradle, Java v9+ is required), Selenium v2.5+ (W3C/JSON Wire).

  • If you are using CLI for running tests, ensure that Maven or Gradle are installed on your machine, its environment variables are set, and its bin is added to system path, $PATH.

Run a sample build

Run a sample Java test build, which is based on the TestNG runner, using your preferred method on BrowserStack:

Get Sample Project

Download Project

Download ZIP

After downloading the sample project, unzip it in the desired location.

Import the downloaded project

Click File → Import. Expand General, select Projects from Folder or Archive, and click Next. Then, navigate to the earlier unzipped downloaded project .

Configure your browserstack.yml config file

The browserstack.yml file holds all the required capabilities to run your tests on BrowserStack

Set access credentials

Set userName and accessKey properties in the browserstack.yml file, available in the root directory, to authenticate your tests on BrowserStack.

Select desktop browsers or real devices from a list of 3000+ available combinations:

Update the browserstack.yml config file

Copy and replace the platforms object in the browserstack.yml file.

yml userName: YOUR_USERNAME accessKey: YOUR_ACCESS_KEY platforms: - YOUR_FIRST_OS_TYPE: YOUR_FIRST_OS YOUR_FIRST_OS_VERSION_TYPE: YOUR_FIRST_OS_VERSION YOUR_FIRST_BROWSER_TYPE: YOUR_FIRST_BROWSER YOUR_FIRST_BROWSER_VERSION_TYPE: YOUR_FIRST_BROWSER_VERSION - YOUR_SECOND_OS_TYPE: YOUR_SECOND_OS YOUR_SECOND_OS_VERSION_TYPE: YOUR_SECOND_OS_VERSION YOUR_SECOND_BROWSER_TYPE: YOUR_SECOND_BROWSER YOUR_SECOND_BROWSER_VERSION_TYPE: YOUR_SECOND_BROWSER_VERSION - YOUR_THIRD_OS_TYPE: YOUR_THIRD_OS YOUR_THIRD_OS_VERSION_TYPE: YOUR_THIRD_OS_VERSION YOUR_THIRD_BROWSER_TYPE: YOUR_THIRD_BROWSER YOUR_THIRD_BROWSER_VERSION_TYPE: YOUR_THIRD_BROWSER_VERSION browserstackLocal: true buildName: browserstack-build-1 projectName: BrowserStack Sample

Copy icon Copy snippet

Right-click your pom.xml file and click Run As → Maven test:

Get Sample Project

Download Project

Download ZIP

After downloading the sample project, unzip it in the desired location.

Import the downloaded project

Click File → Open. Navigate to the earlier unzipped downloaded project .

Configure your browserstack.yml config file

The browserstack.yml file holds all the required capabilities to run your tests on BrowserStack

Set access credentials

Set userName and accessKey properties in the browserstack.yml file,available in the root directory, to authenticate your tests on BrowserStack.

Select desktop browsers or real devices from a list of 3000+ available combinations:

Update the browserstack.yml config file

Copy and replace the platforms object in the browserstack.yml file.

yml userName: YOUR_USERNAME accessKey: YOUR_ACCESS_KEY platforms: - YOUR_FIRST_OS_TYPE: YOUR_FIRST_OS YOUR_FIRST_OS_VERSION_TYPE: YOUR_FIRST_OS_VERSION YOUR_FIRST_BROWSER_TYPE: YOUR_FIRST_BROWSER YOUR_FIRST_BROWSER_VERSION_TYPE: YOUR_FIRST_BROWSER_VERSION - YOUR_SECOND_OS_TYPE: YOUR_SECOND_OS YOUR_SECOND_OS_VERSION_TYPE: YOUR_SECOND_OS_VERSION YOUR_SECOND_BROWSER_TYPE: YOUR_SECOND_BROWSER YOUR_SECOND_BROWSER_VERSION_TYPE: YOUR_SECOND_BROWSER_VERSION - YOUR_THIRD_OS_TYPE: YOUR_THIRD_OS YOUR_THIRD_OS_VERSION_TYPE: YOUR_THIRD_OS_VERSION YOUR_THIRD_BROWSER_TYPE: YOUR_THIRD_BROWSER YOUR_THIRD_BROWSER_VERSION_TYPE: YOUR_THIRD_BROWSER_VERSION browserstackLocal: true buildName: browserstack-build-1 projectName: BrowserStack Sample

Copy icon Copy snippet

Run sample tests on BrowserStack

Run the tests using the following steps:

Click the Maven tool window on the right-hand side. If you can’t see the tool window, click View > Tool Windows > Maven to enable it:

Under Profiles, select the profiles for running tests:

Expand the Lifecycle drop-down, and then double-click to run the tests:

Get Sample Project

Get our sample project using one of the following options:

Option 2: Via Git

Clone our sample Git repository using the commands.

bash git clone cd testng-browserstack

Copy icon Copy snippet

Option 3: Via Maven Archetype

Setup using Maven archetype to create a new sample project with your BrowserStack credentials pre-configured.

bash mvn archetype:generate -B -DarchetypeGroupId=com.browserstack \ -DarchetypeArtifactId=browserstack-sdk-archetype-setup -DarchetypeVersion=1.1 \ -DgroupId=com.browserstack -DartifactId=java-browserstack -Dversion=1.0 \ -DBROWSERSTACK_USERNAME=YOUR_USERNAME -DBROWSERSTACK_ACCESS_KEY=YOUR_ACCESS_KEY -DBROWSERSTACK_PROJECT_REPO=testng-browserstack cd java-browserstack

Copy icon Copy snippet

bash mvn archetype:generate -B -DarchetypeGroupId="com.browserstack" -DarchetypeArtifactId="browserstack-sdk-archetype-setup" -DarchetypeVersion="1.1" -DgroupId="com.browserstack" -DartifactId="java-browserstack" -Dversion="1.0" -DBROWSERSTACK_USERNAME="YOUR_USERNAME" -DBROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY" -DBROWSERSTACK_PROJECT_REPO="testng-browserstack"; cd java-browserstack

Copy icon Copy snippet

bash mvn archetype:generate -B -DarchetypeGroupId="com.browserstack" -DarchetypeArtifactId="browserstack-sdk-archetype-setup" -DarchetypeVersion="1.1" -DgroupId="com.browserstack" -DartifactId="java-browserstack" -Dversion="1.0" -DBROWSERSTACK_USERNAME="YOUR_USERNAME" -DBROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY" -DBROWSERSTACK_PROJECT_REPO="testng-browserstack" && cd java-browserstack

Copy icon Copy snippet

Configure your browserstack.yml config file

The browserstack.yml file holds all the required capabilities to run your tests on BrowserStack

Set access credentials

Set userName and accessKey parameters in the browserstack.yml file, available in the root directory, to authenticate your tests on BrowserStack.

Select desktop browsers or real devices from a list of 3000+ available combinations:

Update the browserstack.yml config file

Copy and replace the platforms object in the browserstack.yml file.

yml userName: YOUR_USERNAME accessKey: YOUR_ACCESS_KEY platforms: - YOUR_FIRST_OS_TYPE: YOUR_FIRST_OS YOUR_FIRST_OS_VERSION_TYPE: YOUR_FIRST_OS_VERSION YOUR_FIRST_BROWSER_TYPE: YOUR_FIRST_BROWSER YOUR_FIRST_BROWSER_VERSION_TYPE: YOUR_FIRST_BROWSER_VERSION - YOUR_SECOND_OS_TYPE: YOUR_SECOND_OS YOUR_SECOND_OS_VERSION_TYPE: YOUR_SECOND_OS_VERSION YOUR_SECOND_BROWSER_TYPE: YOUR_SECOND_BROWSER YOUR_SECOND_BROWSER_VERSION_TYPE: YOUR_SECOND_BROWSER_VERSION - YOUR_THIRD_OS_TYPE: YOUR_THIRD_OS YOUR_THIRD_OS_VERSION_TYPE: YOUR_THIRD_OS_VERSION YOUR_THIRD_BROWSER_TYPE: YOUR_THIRD_BROWSER YOUR_THIRD_BROWSER_VERSION_TYPE: YOUR_THIRD_BROWSER_VERSION browserstackLocal: true buildName: browserstack-build-1 projectName: BrowserStack Sample

Copy icon Copy snippet

Run sample tests on BrowserStack!

Install dependencies and run a sample test build on BrowserStack:

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.