{"id":1584,"date":"2014-09-29T01:04:53","date_gmt":"2014-09-29T05:04:53","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=1584"},"modified":"2017-11-06T04:26:48","modified_gmt":"2017-11-06T09:26:48","slug":"browserstack-part2","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/browserstack-part2\/","title":{"rendered":"Selenium &#038; BrowserStack: Browsers, devices as parameters"},"content":{"rendered":"<p>In a <a href=\"https:\/\/qxf2.com\/blog\/browserstack-part1\/\">previous post<\/a>, we showed you how to get started with running your Selenium automation on <a href=\"http:\/\/www.browserstack.com\/\">BrowserStack<\/a>. In this post <a href=\"http:\/\/www.qxf2.com\/?utm_source=browserstack_parametrize&amp;utm_medium=click&amp;utm_campaign=From%20blog\">Team Qxf2<\/a> 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 7 and Google Nexus 6.<\/p>\n<p><strong>Note:<\/strong> BrowserStack has a <a href=\"https:\/\/www.browserstack.com\/users\/sign_up\">free trial<\/a> that requires only a email to sign up. We encourage testers to take advantage of the free trial.<\/p>\n<hr \/>\n<h3>OVERVIEW<\/h3>\n<p>1. Update your test class<br \/>\n2. Accept command line options<br \/>\n3. Run the test<br \/>\n4. Check the result on BrowserStack<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 1: Update your test class<\/span><br \/>\nWe had <a href=\"https:\/\/qxf2.com\/blog\/browserstack-part1\/\">previously written<\/a> a Selenium script to visit <a href=\"http:\/\/www.chess.com\/\">Chess.com<\/a>, assert the title page and then click on <em>Sign up<\/em> 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:<\/p>\n<pre lang=\"python\">class SeleniumOnBrowserStack(unittest.TestCase):\r\n    \"Example class written to run Selenium tests on BrowserStack\"\r\n\r\n    def __init__(self, platform, realMobile, browserName, device):\r\n       #Constructor: Accepts the platform, realMobile, browserName and device as parameters\r\n       #realMobile if set as true will run the test in a real mobile device.\r\n       self.platform = platform\r\n       self.realMobile = realMobile\r\n       self.browserName = browserName\r\n       self.device = device\r\n    \r\n    def setUp(self):\r\n        desired_capabilities = {'platform': self.platform, 'realMobile': 'true', 'browserName': self.browserName, 'device': self.device, 'browserstack.debug': 'true' }\r\n        print 'Desired Capabilities', desired_capabilities\r\n        self.driver = webdriver.Remote(command_executor='http:\/\/USERNAME:ACCESS_KEY@hub.browserstack.com:80\/wd\/hub',desired_capabilities=desired_capabilities)\r\n<\/pre>\n<p>Remember to update the script with your BrowserStack USERNAME:ACCESS_KEY.<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 2: Accept command line options <\/span><br \/>\nPython&#8217;s OptionParser module can be used for parsing command-line options. Here is a helpful code snippet:<\/p>\n<pre lang=\"python\">from optparse import OptionParser\r\n\r\n#---START OF SCRIPT\r\nif __name__ == '__main__':\r\n    #Lets accept some command line options from the user\r\n    #We have chosen to use the Python module optparse \r\n    usage = \"usage: %prog -p  -r  -b  -d \\nE.g.1: %prog -p iOS -r true -b safari  -d 'iPhone 7'\"\r\n    parser = OptionParser(usage=usage)\r\n    parser.add_option(\"-p\",\"--platform\",dest=\"platform\",help=\"The name of the platform: ie, MAC, ANDROID\",default=\"MAC\")\r\n    parser.add_option(\"-r\", \"--real_Mobile\", dest=\"realMobile\", help=\"Want to test on real Mobile: ie, true, false\",\r\n                      default=\"true\")\r\n    parser.add_option(\"-b\",\"--browserName\",dest=\"browserName\",help=\"The browser: ie, iPhone, android \",default=\"iPhone\")\r\n    parser.add_option(\"-d\",\"--device\",dest=\"device\",help=\"The device name: iPhone 7, Google Nexus 6\",default=\"iPhone 7\")\r\n    (options,args) = parser.parse_args()\r\n<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 3: Run the test<\/span><br \/>\nBringing everything together, your script should look something like the code below<\/p>\n<pre lang=\"python\">import unittest, time, sys, optparse\r\nfrom optparse import OptionParser\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilities\r\n\r\nclass SeleniumOnBrowserStack(unittest.TestCase):\r\n    \"Example class written to run Selenium tests on BrowserStack\"\r\n\r\n    def __init__(self,platform, browserName,device):\r\n        \"Constructor: Accepts the platform, browserName and device as parameters\"\r\n        self.platform = platform\r\n        self.browserName = browserName\r\n        self.device = device\r\n    \r\n    def setUp(self):\r\n        desired_capabilities = {'platform': self.platform, 'realMobile': 'true', 'browserName': self.browserName, 'device': self.device, 'browserstack.debug': 'true' }\r\n        print 'Desired Capabilities', desired_capabilities\r\n        self.driver = webdriver.Remote(command_executor='http:\/\/USERNAME:ACCESS_KEY@hub.browserstack.com:80\/wd\/hub',desired_capabilities=desired_capabilities)\r\n        \r\n        \r\n    def test_chess(self):\r\n        #\"An example test: Visit chess.com and click on sign up link\"\r\n        # Go to the URL\r\n        self.driver.get(\"http:\/\/www.chess.com\")\r\n        # Assert that the Home Page has title \"Chess.com - Play Chess Online - Free Games\"\r\n        self.assertIn(\"Chess.com - Play Chess Online - Free Games\", self.driver.title)\r\n        # Identify the xpath for Play Now button which will take you to the sign up page\r\n        elem = self.driver.find_element_by_xpath(\"\/\/a[@title='Play Now']\")\r\n        elem.click()\r\n        time.sleep(5)\r\n        # Print the title of sign up page\r\n        print self.driver.title\r\n        \r\n\r\n    def tearDown(self):\r\n        self.driver.quit()\r\n \r\nif __name__ == '__main__':\r\n    #Lets accept some command line options from the user\r\n    #We have chosen to use the Python module optparse \r\n    usage = \"usage: %prog -p -r -b -d \\nE.g.1: %prog -p MAC -r true -b safari -d 'iPhone 7'\\nE.g.2: %prog -p ANDROID -r true -b android  -d 'Google Nexus 6'\\n---\"\r\n    parser = OptionParser(usage=usage)\r\n    parser.add_option(\"-p\",\"--platform\",dest=\"platform\",help=\"The name of the platform: ie, MAC, ANDROID\",default=\"MAC\")\r\n    parser.add_option(\"-r\", \"--real_Mobile\", dest=\"realMobile\", help=\"Want to test on real Mobile: ie, true, false\",\r\n                      default=\"true\")\r\n    parser.add_option(\"-b\",\"--browserName\",dest=\"browserName\",help=\"The browser: ie, iPhone, android \",default=\"iPhone\")\r\n    parser.add_option(\"-d\",\"--device\",dest=\"device\",help=\"The device name: iPhone 7, Google Nexus 6\",default=\"iPhone 7\")\r\n    (options,args) = parser.parse_args()\r\n\r\n    #Create a test obj with our parameters\r\n    test_obj = SeleniumOnBrowserStack(platform=options.platform,browserName=options.browserName,device=options.device)\r\n\r\n    #We are explicitly calling setup and tearDown because what we are showing is technically not a unit test\r\n    test_obj.setUp()\r\n\r\n    #Run the test itself. NOTE: This is NOT a unit test\r\n    test_obj.test_chess()\r\n\r\n    #We are explicitly calling setup and tearDown because what we are showing is technically not a unit test\r\n    test_obj.tearDown()\r\n\r\n<\/pre>\n<p>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.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7402\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3.png\" alt=\"Running BrowserStack Test\" width=\"1081\" height=\"99\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3.png 1081w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3-300x27.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3-768x70.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/browserstack_optionparser_result-3-1024x94.png 1024w\" sizes=\"auto, (max-width: 1081px) 100vw, 1081px\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 4:\u00a0Check the result on BrowserStack <\/span><br \/>\nYou 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.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-7396\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs-300x150.png\" alt=\"BrowserStack Result Visual Log\" width=\"300\" height=\"150\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs-300x150.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs-768x383.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs-1024x511.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/09\/Browserstack_Part2_Visual_logs.png 1813w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<hr \/>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,40,71,18,30],"tags":[],"class_list":["post-1584","post","type-post","status-publish","format-standard","hentry","category-automation","category-browserstack","category-mobile-automation","category-python","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=1584"}],"version-history":[{"count":27,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1584\/revisions"}],"predecessor-version":[{"id":7393,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1584\/revisions\/7393"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=1584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=1584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=1584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}