Selenium & BrowserStack: Browsers, devices as parameters

In a previous post, we showed you how to get started with running your Selenium automation on BrowserStack. In this post Team Qxf2 will show you how to modify your existing automation scripts to run on different platforms, browsers and devices. Since a lot of our readers seem to like mobile automation, we have chosen to run these tests on the iPhone 5 and Samsung Galaxy Nexus.

Note: BrowserStack has a free trial that requires only a email to sign up. We encourage testers to take advantage of the free trial.


OVERVIEW

1. Update your test class
2. Accept command line options
3. Run the test
4. Check the result on BrowserStack

STEP 1: Update your test class
We had previously written a Selenium script to visit Chess.com, assert the title page and then click on Sign up button. If you have not seen it before, we recommend you quickly scan the script used. In this post, we will update the same script to accept platform, browser name and device as parameters. To parameterize the browser, platform and device, we will update the constructor and the setUp methods of our script with the code snippet mentioned below:

class SeleniumOnBrowserStack(unittest.TestCase):
    "Example class written to run Selenium tests on BrowserStack"
 
    def __init__(self,platform, browserName,device):
        "Constructor: Accepts the platform, browserName and device as parameters"
        self.platform = platform
        self.browserName = browserName
        self.device = device
 
    def setUp(self):
        desired_capabilities = {'platform': self.platform, 'browserName': self.browserName, 'device': self.device, 'browserstack.debug': 'true' }
        print 'Desired Capabilities', desired_capabilities
        self.driver = webdriver.Remote(command_executor='http://USERNAME:ACCESS_KEY@hub.browserstack.com:80/wd/hub',desired_capabilities=desired_capabilities)

Remember to update the script with your BrowserStack USERNAME:ACCESS_KEY.

STEP 2: Accept command line options
Python’s OptionParser module can be used for parsing command-line options. Here is a helpful code snippet:

from optparse import OptionParser
 
#---START OF SCRIPT
if __name__ == '__main__':
    #Lets accept some command line options from the user
    #We have chosen to use the Python module optparse 
    usage = "usage: %prog -p <platform> -b <browser Name> -d <device>\nE.g.1: %prog -p MAC -b iPhone  -d 'iPhone 5'\nE.g.2: %prog -p ANDROID -b android  -d 'Samsung Galaxy Nexus'\n---"
    parser = OptionParser(usage=usage)
    parser.add_option("-p","--platform",dest="platform",help="The name of the platform: ie, MAC, ANDROID",default="MAC")
    parser.add_option("-b","--browserName",dest="browserName",help="The browser: ie, iPhone, android ",default="iPhone")
    parser.add_option("-d","--device",dest="device",help="The device name: iPhone 5, Samsung Galaxy Nexus",default="iPhone 5")
    (options,args) = parser.parse_args()

STEP 3: Run the test
Bringing everything together, your script should look something like the code below

import unittest, time, sys, optparse
from optparse import OptionParser
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 
class SeleniumOnBrowserStack(unittest.TestCase):
    "Example class written to run Selenium tests on BrowserStack"
 
    def __init__(self,platform, browserName,device):
        "Constructor: Accepts the platform, browserName and device as parameters"
        self.platform = platform
        self.browserName = browserName
        self.device = device
 
    def setUp(self):
        desired_capabilities = {'platform': self.platform, 'browserName': self.browserName, 'device': self.device, 'browserstack.debug': 'true' }
        print 'Desired Capabilities', desired_capabilities
        self.driver = webdriver.Remote(command_executor='http://USERNAME:ACCESS_KEY@hub.browserstack.com:80/wd/hub',desired_capabilities=desired_capabilities)
 
 
    def test_chess(self):
        "An example test: Visit chess.com and click on sign up link"
        #Go to the URL 
        self.driver.get("http://www.chess.com")
        # Assert that the Home Page has title "Play Chess Online - Free Chess Games at Chess.com"
        self.assertIn("Play Chess Online - Free Chess Games at Chess.com", self.driver.title)
        # Identify the xpath for Sign up button and click on it to print the title of Sign up page
        elem = self.driver.find_element_by_xpath("//*[@id='signup-button']")
        elem.click()
        time.sleep(5)
        print self.driver.title
 
 
    def tearDown(self):
        self.driver.quit()
 
if __name__ == '__main__':
    #Lets accept some command line options from the user
    #We have chosen to use the Python module optparse 
    usage = "usage: %prog -p <platform> -b <browser Name> -d <device>\nE.g.1: %prog -p MAC -b iPhone  -d 'iPhone 5'\nE.g.2: %prog -p ANDROID -b android  -d 'Samsung Galaxy Nexus'\n---"
    parser = OptionParser(usage=usage)
    parser.add_option("-p","--platform",dest="platform",help="The name of the platform: ie, MAC, ANDROID",default="MAC")
    parser.add_option("-b","--browserName",dest="browserName",help="The browser: ie, iPhone, android ",default="iPhone")
    parser.add_option("-d","--device",dest="device",help="The device name: iPhone 5, Samsung Galaxy Nexus",default="iPhone 5")
    (options,args) = parser.parse_args()
 
    #Create a test obj with our parameters
    test_obj = SeleniumOnBrowserStack(platform=options.platform,browserName=options.browserName,device=options.device)
 
    #We are explicitly calling setup and tearDown because what we are showing is technically not a unit test
    test_obj.setUp()
 
    #Run the test itself. NOTE: This is NOT a unit test
    test_obj.test_chess()
 
    #We are explicitly calling setup and tearDown because what we are showing is technically not a unit test
    test_obj.tearDown()

You can run the test script the normal way you do. We like executing the script via the command prompt. Here is a screenshot from our machine.
Running BrowserStack Test

STEP 4: Check the result on BrowserStack
You can see the results on your web account. Login to your BrowserStack account and you should see a result as shown in the screen shot below.

BrowserStack Result Visual Log


There you go! In less than 50 lines of Python, you can now get cross browser coverage for all your Selenium automated checks without having to maintain a lab.

Avinash Shetty
I am a software tester with over 10 years of experience in software testing. Currently I am working at Qxf2 Services Bangalore. As a student of the context-driven approach to software testing I feel there is a lot to learn out there which keeps me very excited. My work has helped me gain good experience in different areas of testing like CRM, Web, Mobile and Database testing. I have good knowledge of building test scripts using Automation tools like Selenium and Appium using Java and Python. Beside testing I am a “Sports Fanatic” and love watching and playing sports.

© 2013-2017, Avinash Shetty. All rights reserved.

2 Comments

  1. vikas yadav said:

    HI can you guide me or provide me link for Selenium Java code

    September 23, 2016
    Reply

Leave a Reply

Your email address will not be published.