Thursday, 16 June 2016

Testing web applications in Cloud with SauceLabs

We have already seen how to execute Selenium tests on BrowserStack platform.
SauceLabs also provides the similar service.

You can learn about the capabilities here.

Here is the sample code to invoke the driver on SauceLabs server.

String URL = "http://" + UserId+ ":" + Access_Key + "@ondemand.saucelabs.com:80/wd/hub";
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

To test the web applications hosted on local servers, you need to download and execute the Sauce Connect. Sauce Connect is an application that creates a secure tunnel connection between the Sauce Labs Cloud and the host from where you are executing the tests. It also allows you to pass through firewall.

sc.exe -u xyz -k anykey --proxy 1.11.1.11:8080 --proxy-userpwd userid:passwd  --pac http://pqr

Here is the sample example -

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

/**
 * Created by Sagar on 03-07-2016.
 */
public class BrowserStackTests {

    String Access_Key = "myxyzkey";
    String UserId = "reply2sagar";
    String url = "http://" + UserId+ ":" + Access_Key +
            "@ondemand.saucelabs.com:80/wd/hub";

    @Test
    public void testIPhone() {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("appiumVersion", "1.5.3");
        caps.setCapability("deviceName","iPhone 6");
        caps.setCapability("deviceOrientation", "portrait");
        caps.setCapability("platformVersion","9.3");
        caps.setCapability("platformName", "iOS");
        caps.setCapability("browserName", "Safari");
        caps.setCapability("name", "iPhone 6 Test");
        caps.setCapability("build", 11);

        try{
            WebDriver driver = new RemoteWebDriver(new URL(url), caps);
            driver.get("http://www.softpost.org/selenium-test-page/");
            driver.findElement(By.id("fn")).sendKeys("Sagar");
            driver.close();
            driver.quit();
    }catch (MalformedURLException ex){
            System.out.println("Malformed Exception");
        }
    }

    @Test
    public void testIpad() {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("appiumVersion", "1.5.3");
        caps.setCapability("deviceName","iPad 2");
        caps.setCapability("deviceOrientation", "portrait");
        caps.setCapability("platformVersion","9.3");
        caps.setCapability("platformName", "iOS");
        caps.setCapability("browserName", "Safari");
        caps.setCapability("name", "iPad 2 Test");
        caps.setCapability("build", 12);

        try{
            WebDriver driver = new RemoteWebDriver(new URL(url), caps);
            driver.get("http://www.softpost.org/selenium-test-page/");
            driver.findElement(By.id("fn")).sendKeys("Sagar");
            driver.close();
            driver.quit();
        }catch (MalformedURLException ex){
            System.out.println("Malformed Exception");
        }
    }
}

After you execute code locally, you will see the tests running on Sacuelabs VMs at below url.
https://saucelabs.com/beta/dashboard/builds



What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books