Selenium with Java
Your guide to run Selenium Webdriver tests with Java on BrowserStack.
How to run selenium tests on Browserstack with the framework of your choice
The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the repository.
Introduction
BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Java on BrowserStack is simple. This guide will help you:
Prerequisites
- You need to have BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Before you can start running your Selenium tests with Java, you need to create a setup with required Selenium language bindings. If you already have a setup that runs Selenium tests, then you can skip this step. Following are the steps in few of the most commonly used Java development environments:
If you are using Maven to build your project, then you have to add the following in your pom.xml
so that the dependencies on Selenium libraries are automatically resolved:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
- Download the Selenium Java bindings from the Selenium website and extract the ZIP file to your Eclipse project directory (will be used in a later step)
- Open Eclipse IDE and create a new JAVA project.
- Once the project is created, right click on the project name and go to Configure Build Path:
- Select Classpath in the subsequent step as shown below:
- Click on Add external JARs and add the Selenium Java bindings (a JAR file) and all the dependent libraries (JAR files in the /libs folder) as extracted in the first step. After this step, your Classpath should look like below:
- Your Eclipse setup is now ready for the first Selenium test to be run on BrowserStack.
- Download the Selenium Java bindings from the Selenium website and extract the ZIP file to your Eclipse project directory (will be used in a later step)
- Open IntelliJ IDEA IDE and create a new JAVA project
- Go to File -> Project Structure. Look for Modules and then Dependencies in the Project Structure tab as shown below:
- Click on (+) as shown above and add JARs or directories by selecting the Selenium Java bindings (a JAR file) and all the dependent libraries (JAR files in the /libs folder) as extracted in the first step.
- After the above step, your Project Structure should look like below:
- Your IDE setup is now ready to be able to run Selenium tests on BrowserStack as explained in the next section.
- Download the Selenium Java bindings from the Selenium website and extract the ZIP file to your Eclipse project directory
- Open your IDE and create a new JAVA project
- Find the option to add Classpath for adding external JARs as dependencies and add Selenium Java bindings (a JAR file) and all the dependent libraries (JAR files in the /libs folder) as extracted from the ZIP in first step.
- Your setup should be now ready to run Selenium tests. The next section shows how to execute your first test on BrowserStack.
Run your first test
To get started, let’s run a simple Selenium Webdriver test. The Java script below opens the bstackdemo.com
website, adds a product to the cart, verifies whether the product has been added to the cart, and then marks the test as passed or failed based on whether the product is available in the cart.
Step 1 - Select the OS and device/browser combination
Select the OS and Device/Browser combination you’d like to test on using the drop-down menus below. This will automatically update the Java code sample below:
Look for the icon to select a real device.
driver.quit()
statement is required, otherwise the test continues to execute, leading to a timeout.
Step 2 - Run the sample test code
Create a new empty class using your IDE or editor. Copy the sample code shown above into class file, save it as <class-name>.java
. You can then execute the class using the Run
command in your IDE or by using mvn test
if you are using Maven.
Step 3 - Verify the results
Once the test has run successfully, it is time to verify the results. The Selenium Webdriver test should have opened bstackdemo.com
, added a product to the cart, verified whether the product has been added to the cart, and marked the test as passed or failed based on whether the product is available in the cart.
Mark tests as passed or failed
BrowserStack does not know whether your test’s assertions have passed or failed because only the test script knows whether the assertions have passed. Therefore, based on the assertions on your script, you have to explicitly inform BrowserStack whether your tests have passed or not and this document will help you in doing that exactly.
It is possible to mark tests as either a pass or a fail and also give a reason for the same, using the following snippet:
import org.openqa.selenium.JavascriptExecutor; // Additional import required
public static void markTestStatus(String status, String reason, WebDriver driver) { // the same WebDriver instance should be passed that is being used to run the test in the calling funtion
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+status+"\", \"reason\": \""+reason+"\"}}");
}
You can refer to the sample test script, given in Step 1, to learn implementation of this code snippet.
status
and reason
.
-
status
accepts eitherpassed
orfailed
as the value -
reason
accepts a value in string datatype
Marking test as pass/fail is also possible using our REST API at any point in the test or also after the test has concluded. You can read more about marking test using REST API and use it if it fits your use case.
Debug your app
BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.
Text logs
Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.
Visual logs
Visual Logs automatically capture the screenshots generated at every Selenium command run through your Java script. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.
Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug
capability to true
:
caps.setCapability("browserstack.debug", "true");
Sample Visual Logs from Automate Dashboard:
Video recording
Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.
browserstack.video
capability to false
.
In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options.
Next steps
Once you have successfully run your first test on BrowserStack, you might want to do one of the following:
- Migrate existing tests to BrowserStack
- Run multiple tests in parallel to speed up the build execution
- Test on private websites that are hosted on your internal networks
- Select browsers and devices where you want to test
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!