Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ test-output/
dist/
bin/
*.png
local.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Sample for running [TestNG] tests with BrowserStack Automate.
- Optionally, you can add your BrowserStack credentials to the environment variables `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`.

### Running the tests
- To start local tests run: `ant test-local`
- To start tests in series, run: `ant test-series`
- To start parallel tests run: `ant test-parallel`

Expand Down
7 changes: 7 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
</testng>
</target>

<target name="test-local" depends="compile" description="run the tests on BrowserStack">
<testng classpathref="build.classpath" verbose="10" groups="local_test">
<classpath location="bin" />
<classfileset dir="bin" includes="**/**/TestNGLocal.class"/>
</testng>
</target>

<target name="clean" description="clean up">
<delete dir="${build.dir}" />
</target>
Expand Down
Binary file added lib/browserstack-local-java-0.1.0.jar
Binary file not shown.
110 changes: 110 additions & 0 deletions src/com/browserstack/TestNGLocal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.browserstack;

import com.browserstack.local.Local;

import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.Assert;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestNGLocal {
private String platform;
private String browserName;
private String browserVersion;
private Local bsLocal;

@Factory(dataProvider = "getBrowsers")
public TestNGLocal(String platform,String browserName,String browserVersion) {
this.bsLocal = new Local();
this.platform = platform;
this.browserName = browserName;
this.browserVersion = browserVersion;
}

private WebDriver driver;

@BeforeSuite(alwaysRun=true)
public void suiteSetup() throws Exception {
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");

HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
bsLocalArgs.put("key", accessKey);
bsLocalArgs.put("forcelocal", "");
bsLocal.start(bsLocalArgs);
}

@BeforeMethod(alwaysRun=true)
public void setUp() throws Exception {
String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");

DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform",platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("name", "Sample TestNG Local Test");
capability.setCapability("build", "Sample TestNG Tests");
capability.setCapability("browserstack.local", "true");

driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability);
}

@Test(groups = { "local_test" })
public void testSimple() throws Exception {
this.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("BrowserStack");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}

@AfterMethod(alwaysRun=true)
public void tearDown() throws Exception {
if(driver != null) {
driver.quit();
}
}

@AfterSuite(alwaysRun=true)
public void afterSuite() throws Exception {
if(bsLocal != null) {
bsLocal.stop();
}
}

@DataProvider(name = "getBrowsers")
public static Object[][] createData1() {
return new Object[][] {
{ Platform.WINDOWS.toString(), "chrome", "48" },
{ Platform.XP.toString(), "firefox", "44"},
};
}
}
6 changes: 3 additions & 3 deletions src/com/browserstack/TestNGParallel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.browserstack;

import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.Assert;

import java.io.File;
Expand All @@ -29,7 +29,7 @@ public void setUp(String browser, String version, String platform) throws Except
capability.setCapability("platform",platform);
capability.setCapability("browserName", browser);
capability.setCapability("browserVersion", version);
capability.setCapability("project", "Parallel Tests");
capability.setCapability("name", "Sample TestNG Parallel Tests");
capability.setCapability("build", "Sample TestNG Tests");
String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
Expand Down
6 changes: 3 additions & 3 deletions src/com/browserstack/TestNGSample.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.browserstack;

import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;

import java.io.File;
Expand Down Expand Up @@ -42,8 +42,8 @@ public void setUp() throws Exception {
capability.setCapability("platform",platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("name", "Sample TestNG Series Tests");
capability.setCapability("build", "Sample TestNG Tests");
capability.setCapability("project", "Series Tests");
String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability);
Expand Down