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.







Parallel Testing thumb rules:

Parallel Testing thumb rules:

Each Test must be atomic, must have no dependency.

Implement Grouping mechanism to run in small batches.

Write meaningful comments for each test.


Choose a reliable GRID environment.

XPath vs. CSS Selectors

XPath vs. CSS Selectors

IE tests runs 2-3 times slower than the same tests on Firefox.

Reason is that, Internet Explorer has a very slow XPath engine.

To fix this issue, it's recommended that ids or CSS Selectors be used whenever trying to locate elements.

For more information on CSS Selectors and Selenium:

To test CSS Selectors:



Will Add more details soon.