{"id":1773,"date":"2014-10-11T01:04:35","date_gmt":"2014-10-11T05:04:35","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=1773"},"modified":"2017-07-06T14:47:45","modified_gmt":"2017-07-06T18:47:45","slug":"browserstack-configuration-selenium","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/browserstack-configuration-selenium\/","title":{"rendered":"BrowserStack configuration for Selenium automation"},"content":{"rendered":"<p><a href=\"http:\/\/www.browserstack.com\/\">BrowserStack<\/a> has a number of useful configuration options. In this post, we will show you how to make your automated test runs to use specific configuration parameters of BrowserStack. We walk you through the steps needed to modify your existing automated test runs to use specific configuration parameters of BrowserStack. We assume you have a BrowserStack account. If not, please sign up for a free trial (no credit card required) <a href=\"https:\/\/www.browserstack.com\/users\/sign_up\">over here<\/a>.<\/p>\n<hr>\n<h2>BrowserStack configuration option<\/h2>\n<p>In our <a href=\"https:\/\/qxf2.com\/blog\/browserstack-part2\/\">previous post<\/a>, we wrote a Selenium test to click on the sign up button on <a href=\"http:\/\/www.chess.com\/\">Chess.com<\/a>. In this example, we will reuse the automated test and run it on a Android device and an iPhone. The automated iPhone test will report to the nightly run while the automated Android test will report to an acceptance run. We also added some useful reporting parameters like project, build and test run name to help testers identify and organize different test runs easily. We are going to show you how to set the following parameters:<br \/>\n1. platform<br \/>\n2. browser Name<br \/>\n3. device<br \/>\n4. project: name of the project under which the test is associated<br \/>\n5. build: version of your application the test was running against<br \/>\n6. name: name your test case<br \/>\n7. browserstack.debug: enables visual logs if it is set to true.<\/p>\n<p>The configuration parameters will be listed in a file and read via the Python module <a href=\"http:\/\/pymotw.com\/2\/ConfigParser\/\">ConfigParser<\/a>.<\/p>\n<hr>\n<h2>OVERVIEW<\/h2>\n<p>1. Create a configuration file<br \/>\n2. Accept command line options for configuration file and test run name<br \/>\n3. Use ConfigParser to read the configuration<br \/>\n4. Run the tests<br \/>\n5. Check the result on BrowserStack<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 1: Create a configuration file<\/span><br \/>\nWe can create a configuration file that uses the test run name to identify sections and list the desired configuration under it. For this example we created a file called <strong>test_configuration.ini<\/strong>. Add the contents below to your configuration file and tweak the parameter values as you like. In this example we are showing you two test runs (Nightly Run and Acceptance Run) using different configurations.<\/p>\n<pre lang=\"python\">[Nightly Run]\r\nplatform = MAC\r\nbrowserName = iPhone\r\ndevice = iPhone 5\r\nproject = Chess App Nightly Run\r\nbuild = Build 15.1\r\nname = 10-Oct Nightly Run\r\nbrowserstack.debug = true \r\n\r\n\r\n[Acceptance Run]\r\nplatform = ANDROID\r\nbrowserName = android\r\ndevice = Samsung Galaxy S3\r\nproject = Chess App Acceptance\r\nbuild = Build 15.1\r\nname = 9-Oct Acceptance Run\r\nbrowserstack.debug = false\r\n<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 2: Accept command line options for configuration file and test run name<\/span><br \/>\nSince we want to show you how to modify an existing test,  please use the script (from a preivous blog post) in Step 3 over <a href=\"https:\/\/qxf2.com\/blog\/browserstack-part2\/\">here<\/a>. Notice that we are already accepting command line arguments in our test script. In this post we will modify the command line parameters to accept the configuration file and test run name. The code snippet to accept the configuration file and command line is given below.<\/p>\n<pre lang=\"python\">#---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 -c  -t  \\nE.g.1: %prog -c C:\\\\tests\\\\conf\\\\test_configuration.ini -t \\\"Nightly Run\\\"\\n---\"\r\n    parser = OptionParser(usage=usage)\r\n    parser.add_option(\"-c\",\"--config\",dest=\"config_file\",help=\"The full path of the configuration file\")\r\n    parser.add_option(\"-t\",\"--test_run\",dest=\"test_run\",help=\"The name of the test run\")\r\n    (options,args) = parser.parse_args()\r\n<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 3: Use ConfigParser to read the configuration <\/span><br \/>\n<a href=\"http:\/\/www.qxf2.com\/?utm_source=browserstack_configuration&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2 Services<\/a> loves Python. We have chosen to use the module ConfigParser to read our configuration file. Python newbies can use either <a href=\"http:\/\/pymotw.com\/2\/ConfigParser\/\">this link (pmotw)<\/a> or <a href=\"https:\/\/wiki.python.org\/moin\/ConfigParserExamples\">this link (Python wiki)<\/a> to get started with ConfigParser. On our end, we are modifying our SeleniumOnBrowserStack class to look like the snippet 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,config_file,test_run):\r\n        \"Constructor: Accepts the configuration file and the test run name as parameters\"\r\n        self.config_file = config_file\r\n        self.test_run = test_run\r\n            \r\n    def setUp(self):\r\n        \"Setup for this test involves spinning up the right virtual machine on BrowserStack\"\r\n        parser = SafeConfigParser()\r\n        parser.read(self.config_file)\r\n        desired_capabilities = {'platform' : parser.get(self.test_run, 'platform'),\r\n                                'browserName' : parser.get(self.test_run, 'browserName'),\r\n                                'device' : parser.get(self.test_run, 'device'),\r\n                                'project' : parser.get(self.test_run, 'project'),\r\n                                'name' : parser.get(self.test_run, 'name'),\r\n                                'build' : parser.get(self.test_run, 'build'),\r\n                                'browserstack.debug' : parser.get(self.test_run, 'browserstack.debug')}\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<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 4. Run the tests <\/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 ConfigParser import SafeConfigParser\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,config_file,test_run):\r\n        \"Constructor: Accepts the configuration file and the test run name as parameters\"\r\n        self.config_file = config_file\r\n        self.test_run = test_run\r\n            \r\n    def setUp(self):\r\n        \"Setup for this test involves spinning up the right virtual machine on BrowserStack\"\r\n        parser = SafeConfigParser()\r\n        parser.read(self.config_file)\r\n        desired_capabilities = {'platform' : parser.get(self.test_run, 'platform'),\r\n                                'browserName' : parser.get(self.test_run, 'browserName'),\r\n                                'device' : parser.get(self.test_run, 'device'),\r\n                                'project' : parser.get(self.test_run, 'project'),\r\n                                'name' : parser.get(self.test_run, 'name'),\r\n                                'build' : parser.get(self.test_run, 'build'),\r\n                                'browserstack.debug' : parser.get(self.test_run, 'browserstack.debug')}\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 \"Play Chess Online - Free Chess Games at Chess.com\"\r\n        self.assertIn(\"Play Chess Online - Free Chess Games at Chess.com\", self.driver.title)\r\n        # Identify the xpath for Sign up button and click on it to print the title of Sign up page\r\n        elem = self.driver.find_element_by_xpath(\"\/\/*[@id='signup-button']\")\r\n        elem.click()\r\n        time.sleep(5)\r\n        self.driver.save_screenshot('signup.png')\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 -c <full path of config file> -t <test run> \\nE.g.1: %prog -c C:\\\\tests\\\\conf\\\\test_configuration.ini -t \\\"Nightly Run\\\"\\n---\"\r\n    parser = OptionParser(usage=usage)\r\n    parser.add_option(\"-c\",\"--config\",dest=\"config_file\",help=\"The full path of the configuration file\")\r\n    parser.add_option(\"-t\",\"--test_run\",dest=\"test_run\",help=\"The name of the test run\")\r\n    (options,args) = parser.parse_args()\r\n\r\n    #Create a test obj with our parameters\r\n    test_obj = SeleniumOnBrowserStack(config_file=options.config_file,test_run=options.test_run)\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 snd 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 run it via the command prompt. Here is a screenshot from our machine.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/Run_BrowserStack.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/Run_BrowserStack.png\" alt=\"Run BrowserStack Test using command prompt\" width=\"664\" height=\"238\" class=\"aligncenter size-full wp-image-1778\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/Run_BrowserStack.png 664w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/Run_BrowserStack-300x107.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 5: Check 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 table like the screen shot below.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/BrowserStackResult.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/BrowserStackResult-1024x404.png\" alt=\"BrowserStack Result\" width=\"1024\" height=\"404\" class=\"aligncenter size-large wp-image-1779\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/BrowserStackResult-1024x404.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/BrowserStackResult-300x118.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/BrowserStackResult.png 1348w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<hr>\n<p>Woot! You learnt how to use different BrowserStack configurations in under an hour!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>BrowserStack has a number of useful configuration options. In this post, we will show you how to make your automated test runs to use specific configuration parameters of BrowserStack. We walk you through the steps needed to modify your existing automated test runs to use specific configuration parameters of BrowserStack. We assume you have a BrowserStack account. If not, please [&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,47,71,18,30],"tags":[],"class_list":["post-1773","post","type-post","status-publish","format-standard","hentry","category-automation","category-browserstack","category-mobile","category-mobile-automation","category-python","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1773","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=1773"}],"version-history":[{"count":17,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"predecessor-version":[{"id":6387,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1773\/revisions\/6387"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}