Why Sauce Labs?
Many of us have used Selenium grid to run selenium tests in multiple machine to achieve concurrency. But the same is not so cost effective as we need to have many physical machines presents with us. To solve this problem of acquiring physical machine and adding to cost, organization have slowly started to move in to cloud. Sauce Labs is one such service provider in cloud that has one of the largest grid for executing selenium and Appium test cases. Sauce labs supports multiple operating systems, browsers, mobile emulators, simulators and real devices even. As per Sauce Labs Official site they are supporting more than 800 combinations of browsers and operating systems along with more than 200 emulators and simulators support.
Create an Account in Sauce Labs
Follow the below step to create a free trail sauce account
- Navigate to https://saucelabs.com/
- Click on free trail button
- Fill the form and click on create Account button
- An email will be triggered to your mail account. Verify the email and you are ready to go
Log in to Sauce Labs with the credentials provided during filling of the form. Make sure you are navigated to dashboard page.
Converting Selenium Code to Run in Sauce Labs
Create a simple selenium script to navigate to google and search for sauce labs
Now we will try to create an equivalent script that can run in sauce cloud. For this create another class and define three final strings.
public static final String USERNAME public static final String ACCESS_KEY public static final String URL
The User Name string will take the user name we have used to log in. The access key can be found by navigating to my account where a password protected access key will be present.
Click on show button and enter the password to view the access key. The next is we need to form the URL.
The selenium script will use that URL to log in to sauce labs and run our script in sauce cloud. The URL format is as below
Where user name and access key will be entered as obtained in above steps. Now our code should look as below
Since we are going to run our script in cloud we will not be needing the local web driver instance and the path to driver. We need to modify this such that our script should run in sauce cloud.
For this we need to use RemoteWebDriver instance provided by selenium. This implementation of web driver will help us to test our script in remote machine(cloud or in other grid nodes).There are four constructors for Remote WebDriver class and they are as below
- RemoteWebDriver(Capabilities capabilities)
- RemoteWebDriver(CommandExecutor executor, Capabilities capabilities)
- RemoteWebDriver(java.net.URL remoteAddress, Capabilities capabilities)
We will be using the fourth constructor but before that we need to set our Desired Capabilities. They will help us to set the properties to be used by RemoteWebDriver instance. Refer the below lines to see how to set desired capabilities
In the above lines we have defined the capabilities to be used as chrome browser and platform to be used as Windows 10.
Once the desired capabilities are set then we can define the RemoteWebDriver instance and define our script that should run in sauce labs as below
Once our script is ready as shown above, log in to Sauce labs and navigate to dashboards. In dashboard there would be tab called automated tests where we need to navigate
Run the above code in the IDE of your choice and in Sauce labs verify that the tests are running in Automated Tests section
Click on the tests that is running to watch the live test being performed in cloud
Sauce labs has also many other added advantages in Testing Framework like Testing which we as Automated Web Testing Services providers will discuss in other articles.