BY Mike Arias 08/18/2019 0

How to run tests remotely in BrowserStack with Selenium

BrowserStack is one of several services that can be used to run a test remotely. Imagine the possibility of testing your system with absolutely any environment. Having that chance is the holy grail of testing since it takes a lot of the guessing game out of the equation.

With BrowserStack you can run your automated tests for either web or mobile apps, but you can also run manual tests in virtual devices. For this tutorial, we’ll focus on the automated part of it with web applications.

 

run-test-selenium

 

THE PROJECT

For this tutorial, we’ll be using this automated test framework as a codebase. If you’re interested in knowing how that framework was made step by step, you can take a look at this tutorial. Also, if you want to take a look at other Selenium tutorials, take a look at this.

 

THE SETUP

Before touching our code, we need to create a BrowserStack account. For this, go to https://www.browserstack.com/automate, click the free trial button in the upper right corner and create an account.

Now that we’re inside the Automate section of BrowserStack, we need to find the capabilities that we’ll need for our tests. For this, click the “Integrate your test suite” option.

 

test-selenium

 

You’ll be prompted to select a language; we’ll choose Python (Also, make sure that you have pop-ups enabled). That will take you to their documentation page for Python here which will guide you on how to install Selenium to get the basic prerequisites sorted out. This is not necessary at this point since we already prepared our environment using this tutorial where we got our codebase from.

What we do need to do is to select the OS, device, and resolution we want our tests to run. This will give us our desired capabilities.

testing-selenium

 

THE CODE

BrowserStack will now generate all the code we need so we can use it as we want. We will be making a few small changes to this code to make it more usable.

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
‘browser’: ‘Chrome’,
‘browser_version’: ‘64.0’,
‘os’: ‘Windows’,
‘os_version’: ’10’,
‘resolution’: ‘1024×768’
}

driver = webdriver.Remote(
command_executor=’http://mikearias2:6FB489KGkKpshxcFyqzR@hub.browserstack.com:80/wd/hub’,
desired_capabilities=desired_cap)

driver.get(“http://www.google.com”)
if not “Google” in driver.title:
raise Exception(“Unable to load google page!”)
elem = driver.find_element_by_name(“q”)
elem.send_keys(“BrowserStack”)
elem.submit()
print driver.title
driver.quit()

Our web driver looks like this at the moment:

 

from selenium import webdriver

class Driver:

    def __init__(self):
        self.instance = webdriver.Chrome()

    def navigate(self, url):
        if isinstance(url, str):
            self.instance.get(url)
        else:
            raise TypeError(“URL must be a string.”)

First, we’ll add the desired capabilities as an attribute of the class.

 

from selenium import webdriver

class Driver:

    def __init__(self):
        desired_cap = {
            ‘browser’: ‘Chrome’,
            ‘browser_version’: ‘64.0’,
             ‘os’: ‘Windows’,

             ‘os_version’: ’10’,
              ‘resolution’: ‘1024×768’

        }

                self.instance = webdriver.Chrome()

    def navigate(self, url):
        if isinstance(url, str):
             self.instance.get(url)
   else:
            raise TypeError(“URL must be a string.”)

Now, instead of using a web driver.Chrome() to initiate our driver, we’ll use a web driver.Remote(). To which we’ll pass the URL of the hub on which our tests will run, and the desired capabilities.

 

class Driver:

def __init__(self):
desired_cap = {
‘browser’: ‘Chrome’,
‘browser_version’: ‘64.0’,
‘os’: ‘Windows’,
‘os_version’: ’10’,
‘resolution’: ‘1024×768’
}

self.instance = webdriver.Remote(
command_executor=’http://mikearias2:6FB489KGkKpshxcFyqzR@hub.browserstack.com:80/wd/hub’,
desired_capabilities=desired_cap)

def navigate(self, url):
if isinstance(url, str):
self.instance.get(url)
else:
raise TypeError(“URL must be a string.”)

 

And with these tiny changes to our code, we’re done! Now all that’s left is to run our code. BrowserStack will give us a video, all the logs of the execution and report back to us.

 

run-test-selenium

 

You can use a very similar process to achieve the same results with another service like SauceLabs or even running native apps in the App Automate Section (with some small variations).

What are your thoughts on this? Share it down in the comments

 

This article was written by Mike Arias
Senior Software Testing Engineer of TechAID.
Twitter: @theqaboy
Blog: qaboy.com/

GO BACK

SHARE THIS POST

[addtoany]

Leave a Reply

Your email address will not be published. Required fields are marked *

OTHER POSTS YOU MIGHT LIKE

The True Cost of Hiring Someone for your QA Team

BY Maria Tejeda
05/18/2021 1

API Testing Tools: An Example-Based Guide

BY Manuel Marinez
03/29/2021 1