Integration of Selenium with Saucelabs


What is Saucelabs?
Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications).

Why do we need Saucelabs?
Straight forward answer : Browser Compatibility. We need to test all our testcases in various versions of browsers and various platforms too. We can’t install all those stuff in our local box. Now this Saucelabs plays a major role.

How to integrate your Test scripts with Saucelabs?
It’s quite simple buddies, Just need to follow few steps
Step 1: Sign up for a Sauce Labs account (Free Trail)
Step 2: Get your Access Key from your account (Login and click on ‘My Account‘, Now you will see Access key)
Step 3: Choose the Testcases which you want to run on the cloud
Step 4: Check your Results (Click on ‘Archives‘ to view the video, Screenshots)

Here’s a sample code for your reference

import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class SauceSele {

// enter your saucelabs user name here
public static final String USERNAME = “YourUserName”;

//enter your access key here
public static final String ACCESS_KEY = “XXXXXXXXXXXXXXXXXXXXXX55641A”;
public static final String SauceLabURL = “http://” + USERNAME + “:”
+ ACCESS_KEY + “@ondemand.saucelabs.com:80/wd/hub”;
private WebDriver driver;

@Test(priority = 1)
public void test_Windows_Firefox() throws Exception {
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability(“platform”, “Windows 7”);
caps.setCapability(“version”, “38”);
caps.setCapability(“name”, “Testing on Firefox 38”);
this.driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
driver.get(“https://seleniumbycharan.wordpress.com/”);
System.out.println(driver.getTitle());
System.out.println(“BrowserName :” + caps.getBrowserName() + ” – ”
+ “Version : ” + caps.getVersion());
System.out
.println(“————————————————————————————“);
}

@Test(priority = 2)
public void test_Windows_IE() throws Exception {

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(“platform”, “Windows 8.1”);
caps.setCapability(“version”, “11”);
caps.setCapability(“name”, “Testing on IE 11”);

this.driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
driver.get(“https://seleniumbycharan.wordpress.com/”);
System.out.println(driver.getTitle());
System.out.println(“BrowserName :” + caps.getBrowserName() + ” – ”
+ “Version : ” + caps.getVersion());
System.out
.println(“————————————————————————————“);

}

@Test(priority = 3)
public void test_Windows_Chrome() throws Exception {

DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(“platform”, “Windows XP”);
caps.setCapability(“version”, ” 43.0″);
caps.setCapability(“name”, “Testing on Chrome 43”);

this.driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
driver.get(“https://seleniumbycharan.wordpress.com/”);
System.out.println(driver.getTitle());
System.out.println(“BrowserName :” + caps.getBrowserName() + ” – ”
+ “Version : ” + caps.getVersion());
System.out
.println(“————————————————————————————“);

}

@Test(priority = 5)
public void test_Mac_Safari() throws Exception {

DesiredCapabilities caps = DesiredCapabilities.safari();
caps.setCapability(“platform”, “OS X 10.9”);
caps.setCapability(“version”, “7”);
caps.setCapability(“name”, “Testing on Safari”);

this.driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
driver.get(“https://seleniumbycharan.wordpress.com/”);
System.out.println(driver.getTitle());
System.out.println(“BrowserName :” + caps.getBrowserName() + ” – ”
+ “Version : ” + caps.getVersion());
System.out
.println(“————————————————————————————“);

}

@Test(priority = 6)
public void test_Linux_Firefox() throws Exception {

DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability(“platform”, “Linux”);
caps.setCapability(“version”, “36”);
caps.setCapability(“name”, “Testing on Linux firefox”);

this.driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
driver.get(“https://seleniumbycharan.wordpress.com/”);
System.out.println(driver.getTitle());
System.out.println(“BrowserName :” + caps.getBrowserName() + ” – ”
+ “Version : ” + caps.getVersion());
System.out
.println(“————————————————————————————“);

}

@AfterTest
public void tearDown() throws Exception {
driver.quit();
System.out.println(“driver was closed”);
}
}

That’s it! All your testcases was executed on different versions and different platforms.You can view the results on clicking ‘Archives‘ in your saucelabs dashboard.

What did I do here? No browser was invoked? Surprised?
Generally we’ll use WebDriver driver = new FirefoxDriver(); command to invoke a browser.
But here we don’t need that, we call RemoteWebDriver class to connect remote driver pointed at ondemand.saucelabs.com specifying your Sauce Labs account credentials and desired browser configuration.

We have DesiredCapabilities to set the configuration.
* browser Represents the browser to be used as part of the test run.
* version Represents the version of the browser to be used as part of the test run.
* os Represents the operating system to be used as part of the test run.
You can also name your job too.

You can also run your tests in parallel using TestNG, JUNIT yada yada. You can also integrate with your CI (Jenkins).This is quite simple and useful right!!

Advertisements

One thought on “Integration of Selenium with Saucelabs

  1. Tony praiaz November 15, 2015 / 6:30 pm

    This is cool Man, No more Selenium Grid. Thanks for sharing.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s