How to Run Your Tests on Any Browser & Operating System
Intro
In order to test features in a previous version of Chrome, you would need to run a virtual machine (VM) on your computer with a "legit" version of Windows installed on the VM.
Now what about when the other half of your users use Chrome and Safari on macOS, and maybe other operating systems? What if you need to run those tests in parallel and scale up the number of VMs available? Then you're looking at provisioning, running, and maintaining your own farm of machines and standing up something like Selenium Grid to coordinate tests across them.
And all you wanted to do was run your tests on the browsers you cared about... but there's a better solution to all of this.
A Solution
Rather than take on the overhead of a test infrastructure, you can easily outsource things to a third-party cloud provider (a.k.a. Someone Elses Grid). There are a handful of players in this space, but there's one that stands out -- Sauce Labs.
At the heart of Selenium at scale is the use of Selenium Grid. Selenium Grid lets you distribute test execution across several machines and you connect to it with Selenium WebDriver. With Selenium WebDriver you specify the browser type, browser version, and operating system.
This is fundamentally how Sauce Labs works. Behind the curtain they are ultimately running Selenium Grid, and they receive and execute your tests through Selenium WebDriver.
Here is an example.
Example
NOTE: You'll need an account to use Sauce Labs. You can sign up for a free trial account (no credit-card required).
We recommend exporting your Sauce Labs username and access key as the environment variables SAUCE_USERNAME and
SAUCE_ACCESS_KEY, instead of hard-coding them in your tests. Here are instructions for setting environment
variables on each Operating System:
In each example we create the options for the browser we want (Chrome in this case), set the browser version
(latest, but you can use a specific one, e.g., latest-1 or 140) and the operating system (Windows 11), and
add the Sauce Labs specific options (sauce:options) with our credentials and the name of the test. Then we connect
to Sauce Labs with a Remote WebDriver, pointing it to the Sauce Labs URL.
If the credentials are not set, the test is skipped, so it doesn't fail on machines that don't have them.
Another Browser
If you want to change up the browser, you would just need to use the options for that browser (e.g., Firefox, Edge, or Safari) instead of the Chrome ones, and set the version and operating system you want.
NOTE: You can find a full list of configuration options along with example code for all the Sauce Labs supported languages at their Platform Configurator.
Expected Behavior
- Open the specified browser in Sauce Labs
- Test runs
- Browser closes
- Test results (along with a video recording, screenshots, and other debugging information) are available on the test results dashboard.
Summary
Hopefully this tip has helped you get your tests up and running against numerous browsers.
There are some other things to consider when using Sauce Labs (e.g., testing your secure apps through Sauce Connect, setting the pass/fail status for your tests, and dynamically setting the test name). You can find more information about these things in the Sauce Labs docs.
Happy Testing!