Getting Started with Selenium - Chapter 7: How To Scale Your Tests
Sauce AI for Test Authoring: Move from design to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from design to execution in minutes.
|
x
Blog
Getting Started with Selenium - Chapter 7: How To Scale Your Tests
This post is the seventh in a series of “ Getting Started with Selenium Testing ” posts from Dave Haeffner, a renowned expert on Selenium and automate testing, and a frequent contributor to the Sauce blog and Selenium community. This series is for those who are brand new to test mechanisation with Selenium and a new chapter will be post every Tuesday (eight chapters in all).
How To Scale Your Tests
If you & # x27; ve always need to test lineament in an older browser like Internet Explorer 8 then odds are you ran a practical machine (VM) on your estimator with a & quot; legit & quot; version of Windows XP. Handy, but what happens when you want to check thing on multiple adaptation of IE? Now you & # x27; re looking at multiple VMs. And what about when you need to scale and cover other browser and Operating System (OS) combination? Now you & # x27; re looking at provisioning, escape, and maintaining your own farm of machines and stand up something like Selenium Grid to coordinate tests across them. And all you wanted to do was run your tryout on the browsers you cared about ... Rather than direct on the overhead of a test infrastructure you can easily outsource thing to a third-party cloud supplier - like Sauce Labs
A Selenium Remote, Selenium Grid, And Sauce Labs Primer
At the bosom of Selenium at scale is the use of Selenium Grid and Selenium Remote. Selenium Grid lets you distribute test execution across respective machine and you connect to it with Selenium Remote - specifying the browser type and version through the use of Selenium Remote & # x27; sCapabilities. This is fundamentally how Sauce Labs works. Behind the curtain they are ultimately running Selenium Grid, and they receive and action your tests through Selenium Remote - knowing which browser and operating system to use because of theCapabilitiesits users specify. Let & # x27; s dig in with an exemplar.
An Example
Part 1: Initial Setup
NOTE: You & # x27; ll require an account to use Sauce Labs. Theiroffers plenty to get you start.With Sauce Labs we ask to provide specifics about what we need in our test environment, our certification, and configure Selenium a little bit differently than we have been. Building onthe last write-up, let & # x27; s kickoff by creating a new config file for cloud execution (& # x27; config_cloud.rb & # x27;).filename: config_cloud.rb
1ENV[& # x27; base_url & # x27;]||=& # x27; http: //the-internet.herokuapp.com & # x27;2ENV[& # x27; host & # x27;]=& # x27; saucelabs & # x27;3ENV[& # x27; operating_system & # x27;]||=& # x27; Windows XP & # x27;4ENV[& # x27; browser & # x27;]||=& # x27; internet_explorer & # x27;5ENV[& # x27; browser_version & # x27;]||=& # x27; 8 & # x27;6ENV[& # x27; SAUCE_USERNAME & # x27;]||=& # x27; your-sauce-username & # x27;7ENV[& # x27; SAUCE_ACCESS_KEY & # x27;]||=& # x27; your-sauce-access-key & # x27;8Notice the use ofa host surround variable.Thisis what we& # x27; ll use in our spec_helper file to determine whether to run things topically or in the cloud -- and we & # x27;ll use the other environment variable to live theCapabilities.filename: spec/spec_helper.rb9require & # x27; selenium-webdriver & # x27;1011RSpec.configuredo|config|1213config.before(:each)do14caseENV[& # x27; host & # x27;]15when & # x27; saucelabs & # x27;16caps =Selenium::WebDriver::Remote::Capabilities.send(ENV[& # x27; browser & # x27;])17caps.version=ENV[& # x27; browser_version & # x27;]18caps.platform=ENV[& # x27; operating_system & # x27;]19caps[:name]= example.metadata[:full_description]2021@driver =Selenium::WebDriver.for(22:remote,23url:& quot; http: // # {ENV [& # x27; SAUCE_USERNAME & # x27;]}: # {ENV [& # x27; SAUCE_ACCESS_KEY & # x27;]} @ ondemand.saucelabs.com:80/wd/hub & quot;,24desired_capabilities: caps)25else26@driver =Selenium::WebDriver.for:firefox27end28end2930config.after(:each)do31@driver.quit32end3334end35
Notice that we & # x27; ve added a conditional to assure on the legion environs variable. If the host is set to & # x27; saucelabs & # x27;, so we configure the capabilities for Selenium Remote, passing in the requisite information that we will need for our Sauce Labs session. Otherwise, it will run our tests locally using Firefox. Now if we run our tryout entourage (rspec -r ./config_cloud.rb) and navigate to our Sauce Labs Account page then we should see each of the test lead in their own job, with proper names, against Internet Explorer 8.
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
Part 2: Test Status
The only thing miss now is the pass/fail status of the job. In our local terminal window everything should be arrive up green. But in the list of our Sauce jobs, the & # x27; Results & # x27; panel for everything will just say & # x27; Finished & # x27;. This will make our results less useful in the long run, so let & # x27; s fix it. Thanks to Sauce Labs & # x27; gem, it & # x27; s really simple to do. After we install it we will need to require it someplace, and our & # x27; config_cloud.rb & # x27; file look like a consistent property, since we will only need it when bunk our tests in Sauce. So let & # x27; s add it to the top of the file.filename: config_cloud.rb
require & # x27; sauce_whisk & # x27; ...
All that & # x27; s leave is to add an activeness to ourafter (: each)cube in our & # x27; spec_helper & # x27; file. Before we issue@ driver.quitwe will want to grab the job ID from our@driverobject and set the job status based on the test result. Also, we & # x27; ll need to make sure that it only fulfil when running examination against Sauce Labs -- so we & # x27; ll require to wrap it in a conditional check against the host environment variable.filename: spec/spec_helper.rb
1require & # x27; selenium-webdriver & # x27;23RSpec.configuredo|config|4...56config.after(:each)do7ifENV[& # x27; horde & # x27;]==& # x27; saucelabs & # x27;8if example.exception.nil?9SauceWhisk::Jobs.pass_job @driver.session_id10else11SauceWhisk::Jobs.fail_job @driver.session_id12end13end1415@driver.quit16end1718end
Now when we run our tests (rspec -r ./config_cloud.rb) and navigate to our Sauce Labs Account page, we should see our tests running like before - but now when they finish there should be a proper test status (e.g., & # x27; Pass & # x27; or & # x27; Fail & # x27;).
Speeding Up Your Tests
Now we can easy run our tests in Sauce Labs, but it & # x27; s a real bummer that all of our trial are fulfill in series. As our suite grows, thing will quickly commence to add up and actually hamper our ability to get feedback quickly. With parallelization we can quickly remedy this -- and there are a few ways to go about achieve it.
In code
Through a exam moon-curser
Through your Continuous Integration (CI) Server
Outro
For a deeper dive on how to scale your tests (e.g., running tests in parallel, testing coating behind a firewall, output Sauce Labs job information into your exam results, etc.) so be sure to insure out The Selenium Guidebook. Up next, in my last write-up, I & # x27; ll assist you tie everything together through the use of Uninterrupted Integration.
Dave is the author of Elemental Selenium (a free, once weekly Selenium tip newssheet that is read by hundreds of testing professionals) as good as a new book, The Selenium Guidebook. He is also the creator and sustainer ofChemistryKit(an open-source Selenium framework). He has helped legion companies successfully implement automated acceptance screen; include The Motley Fool, ManTech International, Sittercity, and Animoto. He is a founder and co-organizer of theSelenium Hangoutand has spoken at numerous conferences and meetups about acceptance testing.
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free