Monday, 14 October 2013

Run Selenium scripts on Cloud - Browserstack | TestNG

Browserstack provides similar Saucelabs setup for cloud based automation using Selenium.  Given below a generic test using TestNG from Eclipse IDE that run on Browserstack cloud server.

1| Login to Bowserstack.com
2| Obtain the username and accesskey
3| Make use of the following script and do edit whenever necessary.

package packagename;

import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import java.net.URL;
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;

public class Classname{

  public static final String USERNAME = "yourusername";
  public static final String ACCESS_KEY = "youraccesskey";
  public static final String URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@hub.browserstack.com/wd/hub";

  private WebDriver driver;  

  @BeforeClass
  public void setUp() throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "Firefox");
caps.setCapability("browser_version", "23.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "XP");
        caps.setCapability("browserstack.debug", "true"); //This enable Visual Logs
        driver = new RemoteWebDriver(new URL(URL), caps);
  }  

  @Test
  public void testSimple() throws Exception {
    driver.get("http://www.google.com");
    System.out.println("Page title is: " + driver.getTitle());
    Assert.assertEquals("Google", driver.getTitle());
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("seleniumworks");
    element.submit();    
  }
  
  @AfterClass  
  public void tearDown() throws Exception {  
    driver.quit();  
  }  

}

4| Got to this link and get your logs.
5| For running tests parallel, refer: this link




2 comments: