Home Guide Setting up BrowserStack Automate

Setting up BrowserStack Automate

By Soumyajit Basu, Community Contributor and Praveen Umanath, Director of Product -

Any website, once developed, needs to be tested across multiple platforms and browsers. This way, you can ensure that user experience stays consistent across devices.

But how can you manually set up tests for every device your customers use? Also, how would you arrange for all the infrastructure required? It’s difficult to maintain multiple browsers on the same machine, let alone different versions of each browser, on every platform.

While you can spawn multiple instances, this is not scalable and is difficult to deploy. If you choose to automate, you’ll have to set up additional instances on a cloud server and handle each platform configuration—which is tedious.

What is the solution?

BrowserStack—a cloud-based cross-browser testing tool—lets you test your website across multiple browsers running on real devices. Say goodbye to emulators, simulators and virtual machines!

BrowserStack Live enables instant website testing on 2000+ browsers and real devices. Each browser comes pre-installed with developer tools to enable real-time debugging. You can even test native browser behavior such as keyboard shortcuts, video and audio streaming, etc.

Alternatively, BrowserStack Automate enables instant Selenium testing on desktop browsers, real Android and iOS devices. You can run hundreds of parallel tests concurrently and debug using text logs, console logs, network logs, screenshots, and video recordings.

How do you implement BrowserStack Automate?

  1. Log in to BrowserStack and navigate to the Automate page.
  2. In the top left corner, click on the ‘Show +’ button to retrieve your Username and Access Keys.

Before proceeding to the next steps, it is important to understand how BrowserStack works. The architecture is based on ‘Hubs‘ and ‘Nodes‘. The Hub is the central point which will receive all the requests along with information on which browser, platform (i.e Windows or Linux) and which device the test should be run on. Based on the request received, it will distribute them to the registered nodes. Nodes are where the corresponding tests will run. Each node is a machine (physical/virtual machine) or real mobile device that is registered with the hub. When we register a node, the hub has the information of the node and it will display the browser and configuration details of the nodes.

The prerequisites required to set up BrowserStack are the Capabilities object and Remote WebDriver. The capabilities object would help to configure the desired properties and platform for the tests, and Remote WebDriver is used to hit the BrowserStack API.
Based on the preferences set in the desired capabilities instance, the Hub will point the tests to a node that matches the preferences.

Here is a sample code snippet in Java that sets the capability to point the required node to the respective 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();
 }

Use the testng.xml to pass the browser properties as parameters. For example:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://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 –>

In this example, the TestNG annotation parameters have been used to get the value of the corresponding parameters and feed it to the capabilities object:

@org.testng.annotations.Parameters(value={"browser","browser_version","os","os_version","resolution"})

Now we can run the desired test automation suite on BrowserStack. Each test run has a unique session ID associated with it. Based on the session ID, all the details required for test execution will be fetched. Each test execution has bug logs generated, for example: a text log that gives a textual representation of each process running in the background. There is also a visual log that shows screenshots of the test being executed. The video log gives a live recording of your test execution. You can find all the features of BrowserStack Automate here.

For a complete run through of this code, check out this GitHub repo.

BrowserStack Logo Say Goodbye to your Internal Selenium Grid Get Started Free