How to Implement BrowserStack in Selenium

Posted by akhila priya on November 2nd, 2019

BrowserStack is a necessary supporter of Selenium training, resulting from it automating testing and making that tests run within the cloud. It is exceptionally straightforward and victimization BrowserStack. It tends to do Cross Browser Testing victimization in Selenium.

BrowserStack provides instant access to real Android & iOS devices for testing that conjointly supports multiple operating systems like Windows and macOS. A broad vary of debugging tools performs to troubleshoot tests instantly.

How does one implement BrowserStack Automate?

  1. Log in to BrowserStack and navigate to the modified page.

  2. In the high left corner, click on the 'Show +' button to retrieve your Username and Access Keys.

Before proceeding to the ensuing steps, 

  • It is essential to understand. 

  • In Selenium, the design base will be 'Hubs 'and 'Nodes. 

  • The Hub is that the central purpose, which will receive all the requests and knowledge on that browser, platform, and device the check ought, will distribute them to the registered nodes. 

  • Nodes area unit the corresponding tests can run. 

  • Every node may be a machine, a truly mobile device that registers with the Hub. once we write a node, the Hub has the node's data. 

  • It will show the browser and configuration details of the nodes.

  • The conditions needed to line up the BrowserStack area unit, the Capabilities object, and Remote WebDriver. 

  • Remote WebDriver is employed to hit the BrowserStack API when the capability objects facilitate the test's specific property and platform.

  • Based on the desired capabilities instance, the Hub can aim the tests to a node that matches the preference sets.

  • Here may be a sample code snip in Java that sets the aptitude to the purpose of the desired node to the individual Hub:

final String USERNAME = "";final String AUTOMATE_KEY = ""; final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; try {   DesiredCapabilities caps = new DesiredCapabilities();   caps.setCapability("browser",browser);   caps.setCapability("browser_version",browser_version);   caps.setCapability("os",os);   caps.setCapability("os_version",os_version);   caps.setCapability("resolution",resolution);   caps.setCapability("project","Project-1");   caps.setCapability("build","1.0");   caps.setCapability("browserstack.debug","true");   caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);   driver = new RemoteWebDriver(new URL(URL),caps); } catch(MalformedURLException e) {   e.getMessage(); }

<?xml version=”1.0″ encoding=”UTF-8″?><!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd”><suite thread-count=”3″ name=”Suite” parallel=”tests”><test name=”Test on Chrome”>  <parameter name=”browser” value=”Chrome”/>  <parameter name=”browser_version” value=”43.0″/>  <parameter name=”os” value=”Windows”/>  <parameter name=”os_version” value=”7″/>  <parameter name=”resolution” value=”1024×768″/>  <classes>    <class name=”BrowserStack.ExecuteFunctionalities”/>  </classes></test> <!– Test –><test name=”Test on Edge”>  <parameter name=”browser” value=”Edge”/>  <parameter name=”browser_version” value=”13.0″/>  <parameter name=”os” value=”Windows”/>  <parameter name=”os_version” value=”10″/>  <parameter name=”resolution” value=”1024×768″/>  <classes>    <class name=”BrowserStack.ExecuteFunctionalities”/>  </classes></test> <!– Test –><test name=”Test on Firefox”>  <parameter name=”browser” value=”Firefox”/>  <parameter name=”browser_version” value=”44.0″/>  <parameter name=”os” value=”Windows”/>  <parameter name=”os_version” value=”10″/>  <parameter name=”resolution” value=”1024×768″/>  <classes>    <class name=”BrowserStack.ExecuteFunctionalities”/>  </classes></test> <!– Test –></suite> <!– Suite –>

What are the Desired Capabilities in Selenium?

  • Desired Capabilities has a collection of code-value pairs that encode as a JSON object.

  • It helps QA's outline primarily take a look at needs like operational systems, browser mixtures, and browser versions.

  • This category has multiple ways that permit QAs to specify the specified properties relating to the specified take a look at environments.

  • Each specific action at law has to execute in an exceedingly different setting as per the market trends.

  • A QA may get to take a look at a Web-application on two different browsers (Chrome, Safari) put in on mobile devices running on totally different operating systems (Android, iOS).

  • Desired capabilities, allows QA’s to instruct the Selenium WebDriver about the setting to use.

  • QAs will perform parallel tests on desired devices, browsers, and operational systems that harm Selenium's set capabilities technique.

Methods:

  • getCapability();

This method helps in retrieving the capabilities of the current system on which the tests will perform.

public java.lang.Object getCapability(java.lang.String capabilityName)

  • setCapability();

The setCapability() method uses to declare test environments like device name, operating system name, operating system versions, browser, and browser versions.

Declarations in Java:

setCapability :public void setCapability(java.lang.String capabilityName,boolean value)


setCapability :public void setCapability(java.lang.String capabilityName,java.lang.String value)

setCapability :public void setCapability(java.lang.String capabilityName,Platform value)


setCapability :public void setCapability(java.lang.String key,java.lang.Object value).

  • getBrowserName();

This methodology helps in retrieving the browser name of the present system.

public java.lang.String getBrowserName()

  • setBrowserName();

This methodology is employed to line the browser's name on that tests area unit to be dead.

public void setBrowserName(java.lang.String browserName)

  • getVersion();

This methodology helps in retrieving the browser version or the package of the present system used for running the tests.

public java.lang.String getVersion()

  • setVersion();

This methodology helps in shaping the version of the browser or the package for running the tests.

public java.lang.String getVersion()

  • getPlatform();

This methodology helps in retrieving the small print of the package.

public Platform getPlatform()

  • setPlatform();

This method helps in describing the platform for a desired operating system.

public Platform setPlatform()

Debug your app

BrowserStack presents a scope of debugging tools to support you in establishing and fix bugs while learning through your machine-driven tests.

Text Logs

  • Text Logs square measure a comprehensive record.

  • This doesn’t establish the steps which are executed because it might troubleshoot errors for the failing step.

  • Text Logs square measure accessible from the modified dashboard or via our REST API.

Visual Logs

  • It mechanically captures the screenshots generated at each Selenium command run through your Java script. Visual logs facilitate debugging the precise step.

  • Therefore, the page wherever the failure occurred. They additionally facilitate to establish any layout or design-related problems along with your sites on totally different browsers.

  • Visual Logs disables by default. To change Visual Logs.

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

Video Recording

  • Everyone takes a look at runs on the BrowserStack Selenium grid is recorded.

  • It executes on our remote machine. This feature is implausibly useful whenever a browser takes a look at fails. 

  • You can able to access videos from modifying Dashboard for every session. 

  • You can also transfer the videos from the Dashboard or retrieve a link to transfer the video victimization to our REST API.

caps.setCapability("browserstack.video", "false");

Cross Browsing:

  • Cross Browser Testing may be various non-functional checks that make sure websites work in various internet browsers. 

  • We tend to do this testing on entirely different browsers, each manual, and intuitive approach. 

  • Cross Browser Testing will watch to perform tests for every browser and manually execute it. 

  • We tend to produce Selenium checks with multiple conditional statements that execute and support a formal browser in a natural approach. 

  • We want to confirm that our website has to look identical. For doing this, we would like to own all the browsers.

  • It can perform within the testing while not clearly in a manual approach.

Why is Cross Browser Testing Important?

  • Imagine that you just are attempting to access a web site that archives each bongo cat acculturation existing. 

  • Allow us to say you are doing it for initial|the primary} time from your first ever MacBook Air.

  • You open the campaign, kind the universal resource locator, press Enter, and look ahead to it to load no GIFs area unit loading once it will. 

  • Buttons and text area unit everywhere the page. You check your property and reload to check the constant screen.

  • In the end, you will doubtless to assume that the positioning has difficulty or broken and leave to search out another.

  • Browser vendors follow net Open Standards; however, they need their interpretations of it. 

  • It renders markup language, CSS, and JavaScript in unique ways. Debugging your website’s ASCII text file is not enough to confirm that your website can look and behave as totally different.

  • Cross-browser testing helps thereupon by pinpointing browser-specific compatibility errors. 

  • It makes sure that not opposing a significant part of your target audience due to your website does not work on their browser-OS.

Perform Cross Browser Testing Using Selenium:

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 = "your’s username";

    public static final String AUTOMATE_KEY = " your’s usernameaccesskey";

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

    

    @Test

    public void openSTM() throws Exception {

             

        DesiredCapabilities capability = new DesiredCapabilities();

        capability.setPlatform(Platform.MAC);

        capability.setBrowserName("firefox");

        capability.setVersion("38");

        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.abhjk.com/");

        

        String actualURL = driver.getCurrentUrl();

        

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

        driver.quit();

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

             

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

    }

}

Output:

[RemoteTestNG] detected TestNG version 6.12.0

URL is https://www.abhjk.com/

PASSED: openSTM

 

===============================================

    Default test

    Tests run: 1, Failures: 0, Skips: 0

===============================================

 

 

===============================================

Default suite

Total tests run: 1, Failures: 0, Skips: 0

===============================================

Conclusion:

This article has learned about BrowserStack, implementation of BrowserStack, description of desired capabilities and its methods, how to debug your application, and how does cross browsing works in selenium. You can learn more about it from Selenium Online Training India.

Like it? Share it!


akhila priya

About the Author

akhila priya
Joined: April 17th, 2019
Articles Posted: 11

More by this author