Your guide to running Selenium Webdriver tests with TestNG on BrowserStack.
BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with TestNG on BrowserStack is simple. This guide will help you in:
To run Selenium tests with TestNG on BrowserSatck Automate, follow the below steps:
git clone https://github.com/browserstack/testng-browserstack.git
cd testng-browserstack
mvn compile
single.conf.json
files within the testng-browserstack/src/test/resources/conf
directory with your BrowserStack credentials as shown below:{
"server": "hub-cloud.browserstack.com",
"user": "YOUR_USERNAME",
"key": "YOUR_ACCESS_KEY",
"capabilities": {
"build": "testng-browserstack",
"name": "single_test",
"browserstack.debug": true
},
"environments": {
"chrome": {
"browser": "chrome"
}
}
}
mvn test -P single
Following is the sample TestNG test case that you ran above. The test searches for “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”. You can find the below script here
package com.browserstack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SingleTest extends BrowserStackTestNGTest {
@Test
public void test() throws Exception {
driver.get("https://www.google.com/ncr");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
Thread.sleep(5000);
Assert.assertEquals("BrowserStack - Google Search", driver.getTitle());
}
}
Your TestNG test case has been integrated to run on BrowserStack using the snippet shown below. A class has been defined which contains the methods to configure and create the connection with BrowserStack. You can find the script here.
import com.browserstack.local.Local;
import org.json.simple.parser.JSONParser;
public class BrowserStackTestNGTest {
public WebDriver driver;
private Local l;
@BeforeMethod(alwaysRun=true)
@org.testng.annotations.Parameters(value={"config", "environment"})
public void setUp(String config_file, String environment) throws Exception {
JSONParser parser = new JSONParser();
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
JSONObject envs = (JSONObject) config.get("environments");
DesiredCapabilities capabilities = new DesiredCapabilities();
Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
Iterator it = envCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
it = commonCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(capabilities.getCapability(pair.getKey().toString()) == null){
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}
}
String username = System.getenv("BROWSERSTACK_USERNAME");
if(username == null) {
username = (String) config.get("user");
}
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if(accessKey == null) {
accessKey = (String) config.get("key");
}
String app = System.getenv("BROWSERSTACK_APP_ID");
if(app != null && !app.isEmpty()) {
capabilities.setCapability("app", app);
}
if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}
driver = new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
}
@AfterMethod(alwaysRun=true)
public void tearDown() throws Exception {
driver.quit();
if(l != null) l.stop();
}
}
TestNG also needs the single.conf.json
as shown above for configuring your credentials and it also needs an XML file single.conf.xml
which you can find here, and is shown below, for the integration to work properly. This file specifies which *.conf
file to read for the particular test class to run.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Single">
<test name="SingleTest">
<parameter name="config" value="single.conf.json"/>
<parameter name="environment" value="chrome"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
</suite>
BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your TestNG test cases.
// Method to mark test as pass / fail on BrowserStack
public static void mark() throws URISyntaxException, UnsupportedEncodingException, IOException {
URI uri = new URI("https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json");
HttpPut putRequest = new HttpPut(uri);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add((new BasicNameValuePair("status", "completed")));
nameValuePairs.add((new BasicNameValuePair("reason", "")));
putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClientBuilder.create().build().execute(putRequest);
}
A full reference of our REST API can be found here.
BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.
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 automatically capture the screenshots generated at every Selenium command run through your TestNG tests. 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
:
"capabilities": {
"browserstack.debug": true
}
Sample Visual Logs from Automate Dashboard:
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. Complete details to enable all the debugging options can be found here
Once you have successfully run your first test on BrowserStack, you might want to do one of the following:
Contact our Support team for immediate help while we work on improving our docs.
Contact our Support team for immediate help while we work on improving our docs.