diff --git a/.gitignore b/.gitignore index 1b79009..696ba4b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ test-output/ dist/ bin/ *.png +local.log diff --git a/README.md b/README.md index 165a2d4..387c3d1 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/build.xml b/build.xml index 71f4e73..fd89c5f 100644 --- a/build.xml +++ b/build.xml @@ -42,6 +42,13 @@ + + + + + + + diff --git a/lib/browserstack-local-java-0.1.0.jar b/lib/browserstack-local-java-0.1.0.jar new file mode 100644 index 0000000..ce43afb Binary files /dev/null and b/lib/browserstack-local-java-0.1.0.jar differ diff --git a/src/com/browserstack/TestNGLocal.java b/src/com/browserstack/TestNGLocal.java new file mode 100644 index 0000000..a8af788 --- /dev/null +++ b/src/com/browserstack/TestNGLocal.java @@ -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 bsLocalArgs = new HashMap(); + 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"}, + }; + } +} diff --git a/src/com/browserstack/TestNGParallel.java b/src/com/browserstack/TestNGParallel.java index 9b92be8..ef112df 100644 --- a/src/com/browserstack/TestNGParallel.java +++ b/src/com/browserstack/TestNGParallel.java @@ -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; @@ -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"); diff --git a/src/com/browserstack/TestNGSample.java b/src/com/browserstack/TestNGSample.java index 97b6948..a00c76d 100644 --- a/src/com/browserstack/TestNGSample.java +++ b/src/com/browserstack/TestNGSample.java @@ -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; @@ -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);