Friday, 15 July 2016

Selenium Remote WebDriver + Sauce Labs

Sauce Labs is a Monster Grid to run our tests remotely.

The Sauce Labs cloud-based testing platform helps you run your SeleniumWebdriver tests on the cloud, test web and mobile apps immediately on 350+ browsers and platforms, and easily debug your code with the help of videos and screenshots.

Lest quickly setup the things.


Step 1: Create a sauce labs account: https://saucelabs.com/home

Choose Free Trail, fill the info and then click on Create Account button.



Then verify your account by clicking link in the Email.

Now, you can see Dashboard screen.



Observe, Bottom left corner where you can see your account Firstname and Last name.

Click on it,



We need UserName and AccessKey to connect to sauce labs from our selenium code.



Step 2: Write Remote WebDriver code:

Here is the sample code.

package com.sauceLabs.automation;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class Test {

       public static final String USERNAME = "kXXXXXX";
       public static final String AUTOMATE_KEY = "d87XXXX-XXX-XXX-XXX-XXX";
       public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@ondemand.saucelabs.com:80/wd/hub";

       public static void main(String[] args) throws Exception {

              DesiredCapabilities caps = new DesiredCapabilities();
              caps.setCapability("browser", "IE");
              caps.setCapability("browser_version", "7.0");
              caps.setCapability("os", "Windows");
              caps.setCapability("os_version", "XP");
              caps.setCapability("browserstack.debug", "true");

              WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
              driver.get("http://www.google.com");
              WebElement element = driver.findElement(By.name("q"));

              element.sendKeys("BrowserStack");
              element.submit();

              System.out.println(driver.getTitle());
              driver.quit();

       }

}


Just run it, That’s all.







No comments:

Post a Comment