TechAIDBlogHow to run tests remotely in BrowserStack with Selenium
By Mike Arias 08/18/2019 0

How to run tests remotely in BrowserStack with Selenium

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 in 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

We’ll use this automated test framework as a codebase for this tutorial. If you’re interested in knowing how that framework was made step by step, you can look at this tutorial. Also, if you want to 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 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 unnecessary since we already prepared our environment using this tutorial from which we got our codebase.

 

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 to use it as we want. We will make 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(). 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 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.

 

Author: Mike Arias
Senior Software Testing Engineer of TechAID.
Twitter: @theqaboy
Blog: qaboy.com/

GO BACK TO Tools

Leave a Reply

OTHER POSTS YOU MIGHT LIKE

API Testing Tools: An Example-Based Guide

By Manuel Marinez 02/02/2023 2

API Testing Tools For API testing, there are many tools out there that let you perform the test and collect the results. In this article, We will focus on three tools by showing how to make a request using the Trello API.   But before…

If Clauses vs. ASSERT Statements – When to use which one?

By Mike Arias 09/17/2020 6

If Clauses vs. ASSERT Statements – When to use which one? If clauses and ASSERT statements serve different, yet similar functions. This article aims to establish once and for all when to use “if” and when to use “asserts” in our test cases. There will…

BrowserStack

Thank you for downloading our free resources.

We hope you can find it useful for your daily work. Learn more about what we do.

Your resume was uploaded!

Thank you for being interested in making part of our family.

Redirecting…

¡Tu currículum fue enviado!

Gracias por interesarte en formar parte de nuestra familia.

Redirigiendo…