Thursday, 16 June 2016

Running Selenium tests in Cloud with BrowserStack

BrowserStack is the company which provides running of web tests on various platforms and browsers in the cloud. It supports various platforms and OS as mentioned below.

  1. OS - windows, linux, OSX, iOS, Android
  2. Browsers - chrome, IE, Firefox, Opera, Safari
  3. Device - iPhone 5, Samsung galaxy, iPad
We provide the username/access key + driver capabilities to BrowserStack and they provide us with the driver with specified capabilities. In below example - We are requesting the IE driver on Windows OS. BrowserStack provides the sample capabilities code as well.



String URL = "https://" + USER_ID + ":" + ACCESS_KEY + 
"@hub-cloud.browserstack.com/wd/hub";
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "XP");
    caps.setCapability("browser", "IE");
    caps.setCapability("browser_version", "7.0");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

Testing web applications hosted on local servers

If the web application you are testing is in public domain, you do not need to do any extra set up. But if your application is accessible in only your local network, you need to run the binary so that cloud network of BrowserStack can access your local servers through secured connection. This binary can be downloaded here.

You can execute binary using below syntax.

BrowserStackLocal.exe key -forcelocal -proxyHost 1.1.1.1 -proxyPort 8080 -proxyUser user -proxyPass password -localIdentifier localWindows

After running the binary, ensure that you also pass below capability.

caps.setCapability("browserstack.local", "true");

BrowserStack Specific Capabilities

Here is the list of some BrowserStack Specific Capabilities

  1. caps.setCapability("build", "B1");
  2. caps.setCapability("project", "Softpost");
  3. caps.setCapability("browserstack.ie.noFlash", "true");
  4. caps.setCapability("browserstack.ie.enablePopups", "true");
  5. caps.setCapability("browserstack.safari.enablePopups", "true");
  6. caps.setCapability("browserstack.debug", "true");


Taking screenshot on BrowserStack

driver = (RemoteWebDriver) new Augmenter().augment(driver);
File file1= (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file1, new File("abc.png"));

Automating website that asks for authentication
driver.get("https://user:password@www.softpost.org");

Setting proxy server settings

System.getProperties().put("http.proxyHost", "proxyhost");
System.getProperties().put("http.proxyPort", "8080");
System.getProperties().put("http.proxyUser", "proxyuser");
System.getProperties().put("http.proxyPassword", "proxy-password");

//For HTTPS servers, we can use below syntax.
System.getProperties().put("https.proxyHost", "proxyhost");
System.getProperties().put("https.proxyPort", "8080");
System.getProperties().put("https.proxyUser", "proxyuser");
System.getProperties().put("https.proxyPassword", "proxy-password");


Here is the complete example on BrowserStack.

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 = "mykey";

    String UserId = "sagar211";
    String url = "http://" + UserId+ ":" + Access_Key +
            "@hub-cloud.browserstack.com/wd/hub";

    @Test
    public void testIPhone() {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("browserName", "iPhone");
        caps.setCapability("platform", "MAC");
        caps.setCapability("device", "iPhone 6S");

        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("browserName", "iPad");
        caps.setCapability("platform", "MAC");
        caps.setCapability("device", "iPad Pro");

        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 testAndroid() {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("browserName", "android");
        caps.setCapability("platform", "ANDROID");
        caps.setCapability("device", "Samsung Galaxy S5");

        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.quit();
        }catch (MalformedURLException ex){
            System.out.println("Malformed Exception");
        }
    }
}


You can watch your tests executing on below url.
https://www.browserstack.com/automate

Below image shows how the BrowserStack dashboard looks like.




Here is the list of important links to know more about BrowserStack
  1. https://www.browserstack.com/automate/java
  2. https://www.browserstack.com/automate/capabilities
  3. https://www.browserstack.com/local-testing#configuration

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

Contributors