{"id":295,"date":"2014-05-07T07:36:02","date_gmt":"2014-05-07T11:36:02","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=295"},"modified":"2017-07-06T14:46:23","modified_gmt":"2017-07-06T18:46:23","slug":"sauce-labs-part","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/sauce-labs-part\/","title":{"rendered":"Running Selenium automation using Sauce Labs: Part I"},"content":{"rendered":"<p><strong>Problem:<\/strong> Maintaining infrastructure for automated Selenium cross browser tests is time consuming. <\/p>\n<p>At <a href=\"http:\/\/www.qxf2.com\/?utm_source=sauce_labs&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2 Services<\/a>, we use <a href=\"http:\/\/docs.seleniumhq.org\/projects\/webdriver\/\">Selenium<\/a> and <a href=\"https:\/\/www.python.org\/\">Python<\/a> for UI testing of web applications. Recently, we began using <a href=\"https:\/\/saucelabs.com\/home\">Sauce Labs<\/a> to run our automated tests against different browsers. Sauce Labs cloud testing platform allows you to test web applications on 200+ browsers and platforms. They support the major browsers like Firefox, Internet Explorer, Google Chrome, Safari, and Opera on different operating systems &#8211; Windows, Linux, Android and Mac. Sauce Labs cloud testing platform <a href=\"https:\/\/saucelabs.com\/selenium\">supports Selenium automation testing<\/a> using different languages like Java, Python, Ruby, C#, PHP etc. <\/p>\n<hr>\n<h3>Why we chose Sauce Labs<\/h3>\n<p>Prior to using Sauce Labs, we were writing our own test harness and maintaining virtual machines for different versions of different browsers. We felt the maintenance work was an extra overhead on our testers and took away time from what our testers do well &#8211; thinking and testing software. Given that Sauce Labs has worked out reasonably well for Qxf2 Services, we plan to write a series of blog posts helping you get started with Sauce Labs and integrating it with your current suite of automated Selenium tests.<\/p>\n<p><strong>Aside:<\/strong> Sauce Labs is co-founded by <a href=\"https:\/\/saucelabs.com\/company\/team\">Jason Huggins<\/a> &#8211; the creator of Selenium. <\/p>\n<hr>\n<h3>Getting started with Sauce Labs.<\/h3>\n<p>We are assuming you already have at least one Selenium test written. This section will show you how to run them on Sauce Labs cloud testing platform. In future blog posts we will show you how to modify your tests so they run across different browsers in parallel.<br \/>\n1. Sign up for a <a href=\"https:\/\/saucelabs.com\/signup\">Sauce Labs account<\/a><br \/>\n2. Get your Access Key<br \/>\n3. Choose a test you want to run on the cloud<br \/>\n4. Add a method to your test that initializes the browser, version and platform<br \/>\n5. Run the test<br \/>\n6. Check the result<\/p>\n<hr>\n<h3>A step by step guide<\/h3>\n<p><strong>STEP 1:<\/strong> Sign up for a <a href=\"https:\/\/saucelabs.com\/signup\">Sauce Labs account<\/a>.<br \/>\nYou can sign up for a free 14-day trial. The Sauce Labs team is good at following up with emails full of helpful resources.<\/p>\n<p><strong>STEP 2:<\/strong>  Get your Access Key<br \/>\nYour access key is listed on this page: https:\/\/saucelabs.com\/account. Currently its in the bottom left corner. We will be using this access key in combination with your username to interact with Sauce Labs.<\/p>\n<p><strong>STEP 3:<\/strong> Choose a test you want to run on the cloud<br \/>\n<a href=\"http:\/\/www.qxf2.com\/?utm_source=sauce_labs&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2 Services<\/a> loves using Python. For this example we are going to execute a Selenium test that visits http:\/\/www.python.org and search for the module <a href=\"http:\/\/www.crummy.com\/software\/BeautifulSoup\/\">BeautifulSoup<\/a>. Here is the code snippet:<\/p>\n<pre lang=\"python\">\r\nimport logging, unittest\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.common.keys import Keys\r\n\r\n\r\nclass Selenium2OnSauce(unittest.TestCase):\r\n\r\n    def setUp(self):\r\n        self.driver = webdriver.Firefox()\r\n        \r\n    def test_search_in_python_org(self):\r\n        #Go to the URL \r\n        self.driver.get(\"http:\/\/www.python.org\")\r\n\r\n        #Assert that the title is correct\r\n        self.assertIn(\"Python\", self.driver.title)\r\n\r\n        #Identify the xpath and send the string you want\r\n        elem = self.driver.find_element_by_xpath(\"\/\/input[@id='id-search-field']\")\r\n        print \"About to search for the string BeautifulSoup on python.org\"\r\n        elem.send_keys(\"BeautifulSoup\")\r\n        elem.send_keys(Keys.RETURN)\r\n        \r\n               \r\n    def tearDown(self):\r\n        self.driver.quit()\r\n\r\nif __name__ == '__main__':\r\n    unittest.main()\r\n<\/pre>\n<p><strong>STEP 4:<\/strong> Add a method to your test that initializes the browser, version and platform<br \/>\nThe magic occurs in this step. Change your setup method to look like the code below, where $USERNAME is your username and $ACCESS_KEY is the access key you obtained in Step 2.<\/p>\n<pre lang=\"python\">\r\n    def setUp(self):\r\n        desired_capabilities = webdriver.DesiredCapabilities.FIREFOX \r\n        desired_capabilities['version'] = '24'\r\n        desired_capabilities['platform'] = 'Windows 7'\r\n        desired_capabilities['name'] = 'Testing Search functionality in Python website using Python at Sauce'\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<\/pre>\n<p><strong>STEP 5:<\/strong>Run the test<br \/>\nYou can run the test script the normal way you do. We run it via the command prompt.<\/p>\n<p><strong>STEP 6:<\/strong> Check the result<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.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/feedback_running_test_sl.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\/feedback_running_test_sl-1024x100.jpg\" alt=\"feedback_running_test_sl\" width=\"474\" height=\"46\" class=\"aligncenter size-large wp-image-310\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/feedback_running_test_sl-1024x100.jpg 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/feedback_running_test_sl-300x29.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/feedback_running_test_sl.jpg 1259w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>Once the test is completed, you can also look at the detailed results as well as a screencast. To dive in to the details of the test run, click on the Session<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/Result.jpg\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-301\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/Result-300x128.jpg\" alt=\"Result\" width=\"300\" height=\"128\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/Result-300x128.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/Result-1024x437.jpg 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/05\/Result.jpg 1250w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<hr>\n<p>There you have it! A whirlwind tour of getting started with Sauce Labs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Maintaining infrastructure for automated Selenium cross browser tests is time consuming. At Qxf2 Services, we use Selenium and Python for UI testing of web applications. Recently, we began using Sauce Labs to run our automated tests against different browsers. Sauce Labs cloud testing platform allows you to test web applications on 200+ browsers and platforms. They support the major [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,18,31,30],"tags":[24,33,32,34],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-how-to","category-python","category-sauce-labs","category-selenium","tag-python-2","tag-sauce-labs-2","tag-selenium-2","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/295","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":18,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":6383,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions\/6383"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}