Browser Automation usig Saucelabs

Sun, Nov 10, 2019

Read in 2 minutes

Let’s see how to open desired browser using Java + Saucelabs + Selenium

Saucelabs is a cloud server where you can access any browser , any version , any resolution and in any Operating system.

RemoteWebDriver remoteDriver = null;

DesiredCapabilities remoteDriver_capabilities;

remoteDriver_capabilities=DesiredCapabilities.chrome(); remoteDriver_capabilities.setCapability(“version”, “81.0”); remoteDriver_capabilities.setCapability(“platform”, “Windows 10”);

sauceUrl = String.format(“http://%s:%s@ondemand.saucelabs.com:80/wd/hub”, “<SAUCELABS USERNAME>” , “<SAUCELABS ACCESS KEY>”);

remoteDriver = new RemoteWebDriver(new URL(sauceUrl), remoteDriver_capabilities);

remoteDriver.navigate().to(“https://www.google.com”);

Now you can login to saucelabs and view the test running and desired URL is opened in desired browser

Other capabilities in SauceLabs – You can accept SSL certificate error as below,

remoteDriver_capabilities.setCapability(“acceptSslCerts”,true);

By default max duration for a single session is 180 Sec. Can be increased as below,

remoteDriver_capabilities.setCapability(“maxDuration”, 10800);

Browser latest version can be access as below,

remoteDriver_capabilities.setCapability(“version”, “latest”);

Full Code Open google.com in chrome browser , Windows 10 and in desired resolution

package demo;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class demo
{
	public static void main( String args[] ) throws URISyntaxException, InterruptedException
	{
		RemoteWebDriver remoteDriver = null;
		/*
		 * Declare remote driver desired capabilities
		 */
		DesiredCapabilities remoteDriver_capabilities;
		remoteDriver_capabilities=DesiredCapabilities.chrome(); // Browser
		remoteDriver_capabilities.setCapability("version", "81.0"); // Browser Version
		remoteDriver_capabilities.setCapability("platform", "Windows 10"); //OS version
		remoteDriver_capabilities.setCapability("screenResolution", "1280x800"); //set the browser resolution
		String sauceUrl = String.format("http://%s:%s@ondemand.saucelabs.com:80/wd/hub", "<SAUCELABS USERNAME>","<SAUCELABS ACCESS KEY>");
		try {
			remoteDriver = new RemoteWebDriver(new URL(sauceUrl), remoteDriver_capabilities);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		remoteDriver.navigate().to("https://www.google.com");

	}

}