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!!

Compatibility Testing Using Browserstack with Selenium



BrowserStack allows users to make manual and automation testing on different browsers and operation systems.To execute your test scripts using BrowserStack, you need to set parameters of Browsers and Platforms.

There are few steps to be followed to integrate Selenium with BrowserStack.

Step 1 : SignUp for BrowserStack account (Free Trail)
Step 2 : Get your UserName and Access Key (Click on Account at the top and click on ‘Automate‘)
Step 3 : Create your Test scripts using TestNG
Step 4 : Create a TestNG.xml file to run tests in parallel (set platforms and browsers with desired versions).
Step 5 : Execute TestNG.xml.
Step 6 : To view your results, Login and click on ‘Automate‘ link. You can view your project results.

Here I’m providing a sample code. I’m using TestNG to run tests in parallel.

package package1;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserStackTestCase {

private WebDriver driver;
public static final String USERNAME = “ARORAGLOBALSERVICES”;
public static final String AUTOMATE_KEY = “YourAccessKey”;
public static final String URL = “http://” + USERNAME + “:” + AUTOMATE_KEY
+ “@hub.browserstack.com/wd/hub”;

@BeforeTest
@Parameters(value = { “browser”, “version”, “platform” })
public void setUp(String browser, String version, String platform)
throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(“platform”, platform);
capability.setCapability(“browserName”, browser);
capability.setCapability(“browserVersion”, version);
capability.setCapability(“project”, “MyProject”);
capability.setCapability(“build”, “2.01”);
driver = new RemoteWebDriver(new URL(URL), capability);
}

@Test(priority = 1)
public void testcase001() throws Exception {
driver.get(“http://www.google.com”);
System.out.println(“Page title : ” + driver.getTitle());
Assert.assertEquals(“Google”, driver.getTitle());
WebElement element = driver.findElement(By.name(“q”));
element.sendKeys(“Merry christmas”);
element.sendKeys(Keys.ENTER);

}

@Test(priority = 2)
public void testcase002() {
driver.get(“http://seleniumhq.org”);
System.out.println(“Page title : ” + driver.getTitle());
Assert.assertEquals(“Selenium – Web Browser Automation”,
driver.getTitle());
}

@AfterMethod
public void takeScreenShot(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
driver = new Augmenter().augment(driver);

File srcFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File(“D:\\Screenshot”
+ result.getParameters().toString() + “.png”));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@AfterTest
public void tearDown() throws Exception {
driver.quit();
}

}

Now setup your configuration in xml file. Copy the below code: