Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Selenium Integration with Sauce Labs

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.

selenium-integration
selenium-integration
selenium-integration

Converting Selenium Code to Run in Sauce Labs

Create a simple selenium script to navigate to google and search for sauce labs

public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\driver\\New folder\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.google.com/"); WebElement srcBox = driver.findElement(By.id("lst-ib")); srcBox.sendKeys("sauce labs"); srcBox.sendKeys(Keys.ENTER); }

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.

selenium-integration

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

public static final String URL = "https://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub";

Where user name and access key will be entered as obtained in above steps. Now our code should look as below

public class Demo { public static final String USERNAME = "testuser"; public static final String ACCESS_KEY = "8c53-4d48-ad4d-8557cbe9e6c2"; public static final String URL = "https://" + "testuser" + ":" + "8c53-4d48-ad4d-8557cbe9e6c2" + "@ondemand.saucelabs.com:443/wd/hub"; public static void main(String[] args) throws MalformedURLException { System.setProperty("webdriver.chrome.driver", "C:\\driver\\New folder\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.google.com/"); WebElement srcBox = driver.findElement(By.id("lst-ib")); srcBox.sendKeys("sauce labs"); srcBox.sendKeys(Keys.ENTER); } }s

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() 
  • 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

DesiredCapabilities caps = DesiredCapabilities.chrome(); caps.setCapability("platform", "Windows 10"); caps.setCapability("version", "latest");

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

public class Demo { public static final String USERNAME = "testuser"; public static final String ACCESS_KEY = "8c53-4d48-ad4d-8557cbe9e6c2"; public static final String URL = "https://" + "testuser" + ":" + "8c53-4d48-ad4d-8557cbe9e6c2" + "@ondemand.saucelabs.com:443/wd/hub"; public static void main(String[] args) throws MalformedURLException { // TODO Auto-generated method stub DesiredCapabilities caps = DesiredCapabilities.chrome(); caps.setCapability("platform", "Windows 10"); caps.setCapability("version", "latest"); WebDriver driver = new RemoteWebDriver(new URL(URL), caps); driver.get("https://www.google.com/"); WebElement srcBox = driver.findElement(By.id("lst-ib")); srcBox.sendKeys("sauce labs"); srcBox.sendKeys(Keys.ENTER); } }

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

selenium-integration

Run the above code in the IDE of your choice and in Sauce labs verify that the tests are running in Automated Tests section

selenium-integration

Click on the tests that is running to watch the live test being performed in cloud

selenium-integration

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.

Next Tutorial:

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients