Selenium Test on BrowserStack


In previous post we have seen how to run selenium test on Testing cloud. 
In this post we will see another cloud service called BrowserStack, using this we can write simple selenium webdriver scripts and can run on BrowserStack.


 Note : We will run anuko timesheet login on BrowserSTack for winxp OS and browser IE 7.

Need to know about Desired Capabilities :  It is used to configure the driver instance of Selenium WebDriver. We can configure all driver instance like FirefoxDriver, ChromeDriver, InternetExplorerDriver using desired capabilities.

Step 1 : Register with BrowserStack for 100 mins free trail : 


Step 2 : Once done login to BrowserStack with your id : https://www.browserstack.com/users/sign_in
Step 3: After login go to accounts-> Setting  section and get the client key 
Step 4 : Open eclipse and create a project called "CloudTest" and create a package com.test

 Step 5 : Create a testNG class file or simple class as "RunOnBrowserStack" under com.test package. 
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class RunOnBrowserStack {
public static final String USERNAME = "gururajhm";
public static final String AUTOMATE_KEY = "yourkey";
public static final String URL = "http://" + USERNAME + ":" + AUTOMATE_KEY + "@hub.browserstack.com/wd/hub";
public static WebDriver driver=null;
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");
driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("https://timetracker.anuko.com/login.php");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// maximizze the window.
driver.manage().window().maximize();
WebElement enterusername=driver.findElement(By.id("login"));
waitforelement(enterusername);
// if success then enter data to username
enterusername.sendKeys("guest");
WebElement enterpwd=driver.findElement(By.id("password"));
waitforelement(enterpwd);
// if success then enter data to username
enterpwd.sendKeys("guest");
// to click on button
WebElement loginbtn=driver.findElement(By.id("btn_login"));
waitforelement(loginbtn);
// if success then enter data to username
loginbtn.click();
Thread.sleep(5000);
driver.close();
}
public static String waitforelement(WebElement element) throws IOException{
String msg;
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
// below method will check for visibility of the element i.e in this case its username element
wait.until(ExpectedConditions.visibilityOf(element));
msg="PASS";
System.out.println(msg);
} catch (Exception e) {
System.out.println("waitForElementPresent method failed! "+ e.getMessage());
msg="FAIL -waitForElementPresent method failed! "+ e.getMessage() ;
}
return msg;
}
}





Step 6 : Right click the program and run as Java Application

Step 7 : Login to Browser stack and go under Automate tab should be able to see the dashboard like this