Contact Us

BrowserStack is the finest among the premium tools, efficiently supporting Selenium automated tests. It runs your Selenium tests on a cloud effortlessly and straightforwardly from end to end.

BrowserStack is an Indian cloud web and mobile testing platform that allows developers to test their websites and mobile applications across on-demand browsers, operating systems, and real mobile devices.

Using BrowserStack, you can easily do Cross Browser Testing over Selenium. Cross Browser Testing is a non-functional test that helps you ensure that the website or web application works as desired in various browsers. You could do Cross Browser Testing on different browsers, both in manual and automated ways.

To do it in an automated way, you first need to create Selenium tests with multiple conditional statements that execute test cases based on a specified browser type. Then, while testing a website, ensure that the website appears the same across all browsers. To do this is a must to have all the browsers. Fortunately, there are some tools like BrowserStack to perform cross-browser testing without testing individually in a manual way.

Below are the detailed prerequisites and benefits, following which is a step-by-step guide to configuring the Browser stack with Selenium.

Prerequisites to Perform Cross Browser Testing Using Selenium

Screenshot 2023-02-20 040357

Benefits of Using BrowserStack with Selenium

  • Leverage Open source tools
  • Comprehensive Device & Browser coverage
  • Integrate in minutes
  • Cross browser/device testing
  • Time saving
  • Cost effectiveness

 

Steps to Configure BrowserStack with Selenium

Step 1: Login to browser stack, then note your Username and Access Key. Pass the Username and Access key, along with the URL to run the scripts on BrowserStack Cloud. To do this, login – click on Automate – Copy your Username and Access key from the left-hand sidebar

Step 2: Create a sample maven project in eclipse and add the dependencies for selenium and TestNg.

Step 3: Code snippet for reference

Get the desired capabilities for different browsers using below link

https://www.browserstack.com/automate/capabilities

Use the below code to run the scripts using browser stack on single device like any browser/IOS/Android/tablet:

import java.net.URL;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Platform;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

 

public class Test1 {

 

public WebDriver driver;

public static final String USERNAME = " abcd";

public static final String AUTOMATE_KEY = "abcdkey";

public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY +     "@hub.browserstack.com/wd/hub";

 

@Test

public void openSTM() throws Exception {

// To execute scripts through remote server or grid on mulitple //browsers, we

// need to set capabilities of platform,

// version etc., to run the scripts

DesiredCapabilities capability = new DesiredCapabilities();

capability.setPlatform(Platform.MAC);

capability.setBrowserName("firefox");

capability.setVersion("38");

capability.setCapability("browserstack.debug", "true");

// Creatng URL object

URL browserStackUrl = new URL(URL);

// Create object of driver. We execute scripts remotely. So we use

// RemoteWebDriver

// There are many constructors to remotewebdriver

// To pass URL object and Capabilities object, use the below mentioned

// constructor

// RemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities)

driver = new RemoteWebDriver(browserStackUrl, capability);

// Implicit wait for 30 seconds

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// to open url

driver.get("https://www.softwaretestingmaterial.com");

// To get the current url

String actualURL = driver.getCurrentUrl();

// To print the URL

System.out.println("URL is " + actualURL);

driver.quit();

String expectedURL = "https://www.softwaretestingmaterial.com/";

// Assert to verify the actual and expected values

Assert.assertEquals(actualURL, expectedURL, "Expected and Actual are not same");

}

}

Use the below code to run the scripts using browser stack on multiple device like any browser/IOS/Android/tablet:

import java.io.IOException;

import java.net.URL;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Platform;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.Assert;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

 

public class Test1 {

public WebDriver driver;

public static final String USERNAME = "abcd";

public static final String AUTOMATE_KEY = "abcdkey";

public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub.browserstack.com/wd/hub";

 

// Here to pass multiple test data to the script, I am using @DataProvider

// annotation of TestNG

@Test(dataProvider = "EnvironmentDetails")

public void openSTM(Platform platform, String browserName, String browserVersion)

throws InterruptedException, IOException {

 

DesiredCapabilities capability = new DesiredCapabilities();

capability.setPlatform(platform);

capability.setBrowserName(browserName);

capability.setVersion(browserVersion);

capability.setCapability("browserstack.debug", "true");

 

URL browserStackUrl = new URL(URL);

 

driver = new RemoteWebDriver(browserStackUrl, capability);

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("https://www.softwaretestingmaterial.com");

 

String actualURL = driver.getCurrentUrl();

System.out.println("URL is " + actualURL);

driver.quit();

String expectedURL = "https://www.softwaretestingmaterial.com/";

Assert.assertEquals(actualURL, expectedURL, "Failed To Open");

}

// passing parallel = true to run the test scripts in parallel

// DataProvider supports Object return type

// Passing values such as MAC with Chrome 62, Windows 8 with Chrome 62, and

// Windows 7 with firefox 57

@DataProvider(name = "EnvironmentDetails", parallel = true)

public Object[][] getData() {

Object[][] testData = new Object[][] { { Platform.MAC, "chrome", "62.0" }, { Platform.WINDOWS, "firefox", "57" } };

return testData;

}

}

Selenium cross-browser testing is crucial since it ensures that the web application is compatible with all major browsers and offers a solid user experience everywhere. Testers may run automated visual UI tests in actual user environments using BrowserStack's cloud Selenium grid of more than 2000 genuine browsers and devices. Compared to the previous ten years, the mobile revolution has altered how websites are now viewed. Since there are billions of mobile users worldwide, the developer cannot disregard this figure. Any device, browser, version, OS, and resolution can access the website. Before the disaster occurs, developers and testers must be prepared.

Need Help?