The purpose of this article is to show how easy is to integrate webdriver with architectures on the cloud, in this case with Browserstack.

For every new tool the first thing to do is to check out and examine the documentation.
The basic steps are very well documented, the examples and the thorough explanations give good impressions. I was able to make a simple test run on their architecture in a couple of minutes.
There is a section with an idea for parallelization, it is worth checking out, as you can get some ideas on improving your testbase. Link to their site is
http://www.browserstack.com/automate/java

Not wanting to make a post without some working code, I made a simple example which you can try out.
Prerequisites:
You have to have Eclipse and JUnit installed on your machine, and a project should be opened with the selenium libraries in the build path.

package tests;

import java.net.URL;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
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 org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MainTest{

	WebDriver driver;

	@Before
	public void setUp() throws Exception{
		DesiredCapabilities capability = DesiredCapabilities.firefox();
		driver = new RemoteWebDriver(
		new URL("http://username:accessKey@hub.browserstack.com/wd/hub"),
		capability
		);
	}

	@Test
	public void testMandatoryFields(){

		driver.get("http://molnarij.hu/");
		System.out.println("Page title is: " + driver.getTitle());

		waitForElementToAppear(By.linkText("Milyen íjat kaphatsz nálam?"));
		driver.findElement(By.linkText("Milyen íjat kaphatsz nálam?")).click();

		waitForElementToAppear(By.linkText("Magyar II íj, tradícionális magyar íj"));
		driver.findElement(By.linkText("Magyar II íj, tradícionális magyar íj")).click();

		System.out.println("And the Title is: " + driver.getTitle());
		Assert.assertEquals("Magyar II íj, tradícionális magyar íj",driver.getTitle());
	}

	@After
	public void tearDown() {
		driver.quit();
	}

	public void waitForElementToAppear(By selector) {
		WebDriverWait wait = new WebDriverWait(driver, 60);
		wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
	}
}

Logging is very done, both textually and visually, as this is a must for tools like this. To see the visual logs you have to enable them first by setting browserstack.debug=true.
If an exception appears during the test, you will have access to the stacktrace and a screenshot is being shown in between the logs too:
Browserstack exception

An example of a simple text log:
Image 3

One thing which seems different form other tools is the unlimited running time of a test. This can be helpful during the testing of more complex systems.

Similar Posts from the author:

One thought to “Browserstack integration with junit and webdriver”

  • vandana

    Hi , I am looking for sample code to configure for parallel execution on all browsers using browser stack.

    Please help me out any one
    thanks in advance.

Comments are closed.