{"id":317,"date":"2014-05-13T08:35:17","date_gmt":"2014-05-13T12:35:17","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=317"},"modified":"2017-07-06T14:46:39","modified_gmt":"2017-07-06T18:46:39","slug":"running-selenium-automation-using-sauce-labs-part-2","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/running-selenium-automation-using-sauce-labs-part-2\/","title":{"rendered":"Running Selenium automation using Sauce Labs: Part 2"},"content":{"rendered":"<p><span style=\"font-weight: bold; font-style: inherit;\"><strong>Problem:<\/strong><\/span>\u00a0Selenium automation should run tests across different browsers and platforms on <a href=\"https:\/\/saucelabs.com\/\">Sauce labs<\/a>. <\/p>\n<p>In a previous post titled <a href=\"https:\/\/qxf2.com\/blog\/sauce-labs-part\/\">Running Selenium automation using Sauce Labs: Part I<\/a>, we showed you how to get started with running your Selenium tests on Sauce Labs. In this post we will show you how to parameterize \u00a0the test so that you can run the test across different browsers, browser versions and operating systems. For <a href=\"https:\/\/www.python.org\/\">Python<\/a> newbies, we have a short section on using the Python module <a href=\"http:\/\/pymotw.com\/2\/optparse\/\">OptionParser<\/a>.<\/p>\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 Sauce Labs<\/p>\n<p>&nbsp;<br \/>\n<span style=\"font-weight: bold; font-style: inherit;\">STEP 1: Update your test class<\/span><br \/>\nWe will update the script used in <a href=\"https:\/\/qxf2.com\/blog\/sauce-labs-part\/\">Part 1<\/a> of this tutorial to include\u00a0the code which accepts browser, version and platform as parameters. Update the constructor and setUp methods with the code snippet mentioned below:<\/p>\n<pre lang=\"python\">\r\nclass Selenium2OnSauce(unittest.TestCase):\r\n    \"Example class written to run Selenium tests on Sauce Labs\"\r\n    def __init__(self,browser,browser_version,platform):\r\n        \"Constructor: Accepts the browser, browser version and OS(platform) as parameters\"\r\n        self.browser = browser\r\n        self.browser_version = browser_version\r\n        self.platform = platform\r\n\r\n    def setUp(self):\r\n        \"Setup for this test involves spinning up the right virtual machine on Sauce Labs\"\r\n        if self.browser.lower() == 'ff' or self.browser.lower() == 'firefox':\r\n            desired_capabilities = webdriver.DesiredCapabilities.FIREFOX\r\n            desired_capabilities['version'] = self.browser_version\r\n            desired_capabilities['platform'] = self.platform\r\n        elif self.browser.lower() == 'ie':\r\n            desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER\r\n            desired_capabilities['version'] = self.browser_version\r\n            desired_capabilities['platform'] = self.platform\r\n        elif self.browser.lower() == 'chrome':\r\n            desired_capabilities = webdriver.DesiredCapabilities.CHROME\r\n            desired_capabilities['version'] = self.browser_version\r\n            desired_capabilities['platform'] = self.platform\r\n            \r\n        desired_capabilities['name'] = 'Testing Script in different Browser, Version and Platform'\r\n\r\n        self.driver = webdriver.Remote(\r\n            desired_capabilities=desired_capabilities,\r\n            command_executor=\"http:\/\/$USERNAME:$ACCESS_KEY@ondemand.saucelabs.com:80\/wd\/hub\"\r\n        )\r\n        self.driver.implicitly_wait(30)\r\n\r\n<\/pre>\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\">\r\nfrom 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 -b <browser> -v <browser version number> -p <platform>\\nE.g.1: %prog -b ie -v 8 -p \\\"Windows 7\\\"\\nE.g.2: %prog -b ff -v 26 -p \\\"Windows 8\\\"\\n---\"\r\n    parser = OptionParser(usage=usage)\r\n    parser.add_option(\"-b\",\"--browser\",dest=\"browser\",help=\"The name of the browser: ie, firefox, chrome\",default=\"firefox\")\r\n    parser.add_option(\"-v\",\"--version\",dest=\"browser_version\",help=\"The version of the browser: a whole number\",default=None)\r\n    parser.add_option(\"-p\",\"--platform\",dest=\"platform\",help=\"The operating system: Windows 7, Linux\",default=\"Windows 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 \/>\nYou can run the test script the normal way you do. We run it via the command prompt. Here is a screenshot from our machine.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/sauce_labs_parameters.jpg\" 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\/05\/sauce_labs_parameters.jpg\" alt=\"Run the test \" width=\"673\" height=\"354\" class=\"aligncenter size-full wp-image-339\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/sauce_labs_parameters.jpg 673w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/sauce_labs_parameters-300x157.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/sauce_labs_parameters-672x354.jpg 672w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 4:\u00a0Check the result on Sauce Labs <\/span><br \/>\nYou can see the results on your web account. Login to your Sauce Labs 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\/05\/check_the_result_sauce_labs.jpg\" 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\/05\/check_the_result_sauce_labs.jpg\" alt=\"Check the result on Sauce Labs\" width=\"863\" height=\"387\" class=\"aligncenter size-full wp-image-340\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/check_the_result_sauce_labs.jpg 863w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/check_the_result_sauce_labs-300x134.jpg 300w\" sizes=\"auto, (max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>Ta-daaa! 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>Problem:\u00a0Selenium automation should run tests across different browsers and platforms on Sauce labs. In a previous post titled Running Selenium automation using Sauce Labs: Part I, we showed you how to get started with running your Selenium tests on Sauce Labs. In this post we will show you how to parameterize \u00a0the test so that you can run the test [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,18,31,30],"tags":[37,35,36,24,33,32],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-how-to","category-python","category-sauce-labs","category-selenium","tag-cross-browser-automation","tag-optionparser","tag-parametrize","tag-python-2","tag-sauce-labs-2","tag-selenium-2"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/317","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=317"}],"version-history":[{"count":20,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":6384,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions\/6384"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}