How To Run Selenium Tests On BrowserStack [Cross Browser Testing using Selenium]
How To Run Selenium Tests On BrowserStack [Cross Browser Testing]
In this post, we see how to run Selenium Tests on BrowserStack. Before that let’s learn why do we execute Selenium WebDriver tests on BrowserStack. In this post, we will learn following.
- 1. What is BrowserStack
- 2. What is Cross Browser Testing
- 3. Run Selenium Tests on BrowserStack
- 4. Run Selenium Tests on Multiple Browsers in Parallel on BrowserStack
- 5. Capturing Screenshots While Executing Selenium Scripts on BrowserStack
What is BrowserStack
BrowserStack is one of the premium sponsors of Selenium. BrowserStack supports Selenium automated tests. It runs our Selenium tests on a cloud. It’s very simple and straightforward. Here I am not talking about the features of BrowserStack. It’s out of the scope of this article. Using BrowserStack we could do Cross Browser Testing using Selenium. In this article, I will show you how to do cross-browser testing using Selenium on BrowserStack.
You could learn more about BrowserStack on BrowserStack Official Site
What is Cross Browser Testing
Cross Browser Testing is a type of non-functional test which helps us ensure that our website or web application works as expected in various web browsers. We could do Cross Browser Testing on different browsers both manual and automated way. To do Cross Browser Testing manually, we (Software Testers) create tests for each browser and execute it manually on each browser. To do it in an automated way, we could create Selenium tests with multiple conditional statements that execute test cases based on a specified browser type. While testing a website, we need to ensure that our website is appearing same across all the browsers. To do this we need to have all the browsers. Fortunately, there are some tools (e.g., CrossBrowserTesting, BrowserStack) to perform cross-browser testing without testing individually in a manual way.
Read more on Cross Browser Testing
Check below video to watch “How To Execute Selenium Scripts in Parallel using BrowserStack”
Please be patient. The video will load in some time.
Run Selenium Tests on BrowserStack
Let’s get started with a sample test, which opens SoftwareTestingMaterial site’s homepage and gets the title of the page and verifies it using Assertion.
Prerequisites to perform Cross Browser Testing Using Selenium:
- BrowserStack Account – Here is a signup link
- BrowserStack Username and Access Key
- Selenium Jars
- TestNG Jars
Step 1: After few minutes of signing up for the trial, you receive an email from BrowserStack support asking if you need any help setting up integration with BrowserStack.
After login, note your Username and Access Key. We need to pass the Username and Accesskey along with the URL to run the scripts on BrowserStack Cloud. To do this do login – click on Automate – Copy your Username and Accesskey from the left-hand sidebar
Step 2: Create a maven project – check this post for the same
Step 3: Copy the below code and Run Selenium Test on BrowserStack
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package tests; import java.io.File; import java.net.URL; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.Platform; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.Assert; import org.testng.annotations.Test; public class BrowserStackTests { public WebDriver driver; public static final String USERNAME = "rajkumar"; public static final String AUTOMATE_KEY = "rajkumaraccesskey"; 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"); } }
Output:
[RemoteTestNG] detected TestNG version 6.12.0 URL is https://www.softwaretestingmaterial.com/ PASSED: openSTM =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================
Screenshots:
Visual Logs:
Run Selenium Tests on Multiple Browsers in Parallel on BrowserStack
Copy the below code and Run Selenium Test on BrowserStack
package tests; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.Platform; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.Augmenter; 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 BrowserStackTestsWithDataProvider { public WebDriver driver; public static final String USERNAME = "rajkumar"; public static final String AUTOMATE_KEY = "rajkumaraccesskey"; 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.WIN8, "chrome", "62.0"}, {Platform.WINDOWS, "firefox", "57"} }; return testData; } }
Must Read: Learn More About DataProviders In TestNG
Output:
[RemoteTestNG] detected TestNG version 6.12.0 URL is https://www.softwaretestingmaterial.com/ URL is https://www.softwaretestingmaterial.com/ URL is https://www.softwaretestingmaterial.com/ PASSED: openSTM(WINDOWS, "firefox", "57") PASSED: openSTM(MAC, "chrome", "62.0") PASSED: openSTM(WIN8, "chrome", "62.0") =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
Screenshots:
Capturing Screenshots While Executing Selenium Scripts on BrowserStack
To get the screenshots you have to run the following code:
driver = (RemoteWebDriver) new Augmenter().augment(driver); File srcFile = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(srcFile, new File("/location/to/screenshot.png"))
Complete Code:
package tests; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.Platform; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.Augmenter; 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 BrowserStackTestsWithDataProvider { public WebDriver driver; public static final String USERNAME = "rajkumar"; public static final String AUTOMATE_KEY = "rajkumaraccesskey"; public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub.browserstack.com/wd/hub"; @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"); // following 3 lines of code captures the screenshot driver = (RemoteWebDriver) new Augmenter().augment(driver); File srcFile = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(srcFile, new File("/location/to/screenshot.png")); 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"); } @DataProvider(name="EnvironmentDetails", parallel=true) public Object[][] getData(){ Object[][] testData = new Object[][]{ {Platform.MAC, "chrome", "62.0"}, {Platform.WIN8, "chrome", "62.0"}, {Platform.WINDOWS, "firefox", "57"} }; return testData; } }
Conclusion:
Have you tried running BrowserStack. Share your experience by commenting below in the comments section. If you like this post, share it with your friends.