Skip to content

Commit edac79b

Browse files
committed
Merge pull request #2 from browserstack/restructure
Restructure
2 parents 37de24b + 2f54c1a commit edac79b

File tree

7 files changed

+125
-6
lines changed

7 files changed

+125
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ test-output/
22
dist/
33
bin/
44
*.png
5+
local.log

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Sample for running [TestNG] tests with BrowserStack Automate.
1010
- Optionally, you can add your BrowserStack credentials to the environment variables `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`.
1111

1212
### Running the tests
13+
- To start local tests run: `ant test-local`
1314
- To start tests in series, run: `ant test-series`
1415
- To start parallel tests run: `ant test-parallel`
1516

build.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
</testng>
4343
</target>
4444

45+
<target name="test-local" depends="compile" description="run the tests on BrowserStack">
46+
<testng classpathref="build.classpath" verbose="10" groups="local_test">
47+
<classpath location="bin" />
48+
<classfileset dir="bin" includes="**/**/TestNGLocal.class"/>
49+
</testng>
50+
</target>
51+
4552
<target name="clean" description="clean up">
4653
<delete dir="${build.dir}" />
4754
</target>

lib/browserstack-local-java-0.1.0.jar

7.71 KB
Binary file not shown.

src/com/browserstack/TestNGLocal.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.browserstack;
2+
3+
import com.browserstack.local.Local;
4+
5+
import org.testng.annotations.DataProvider;
6+
import org.testng.annotations.BeforeMethod;
7+
import org.testng.annotations.AfterMethod;
8+
import org.testng.annotations.BeforeSuite;
9+
import org.testng.annotations.AfterSuite;
10+
import org.testng.annotations.Factory;
11+
import org.testng.annotations.Test;
12+
import org.testng.Assert;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.net.URL;
17+
import java.util.HashMap;
18+
19+
import org.apache.commons.io.FileUtils;
20+
import org.openqa.selenium.By;
21+
import org.openqa.selenium.Platform;
22+
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebElement;
24+
import org.openqa.selenium.OutputType;
25+
import org.openqa.selenium.TakesScreenshot;
26+
import org.openqa.selenium.remote.Augmenter;
27+
import org.openqa.selenium.remote.RemoteWebDriver;
28+
import org.openqa.selenium.remote.DesiredCapabilities;
29+
30+
public class TestNGLocal {
31+
private String platform;
32+
private String browserName;
33+
private String browserVersion;
34+
private Local bsLocal;
35+
36+
@Factory(dataProvider = "getBrowsers")
37+
public TestNGLocal(String platform,String browserName,String browserVersion) {
38+
this.bsLocal = new Local();
39+
this.platform = platform;
40+
this.browserName = browserName;
41+
this.browserVersion = browserVersion;
42+
}
43+
44+
private WebDriver driver;
45+
46+
@BeforeSuite(alwaysRun=true)
47+
public void suiteSetup() throws Exception {
48+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
49+
50+
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
51+
bsLocalArgs.put("key", accessKey);
52+
bsLocalArgs.put("forcelocal", "");
53+
bsLocal.start(bsLocalArgs);
54+
}
55+
56+
@BeforeMethod(alwaysRun=true)
57+
public void setUp() throws Exception {
58+
String username = System.getenv("BROWSERSTACK_USERNAME");
59+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
60+
61+
DesiredCapabilities capability = new DesiredCapabilities();
62+
capability.setCapability("platform",platform);
63+
capability.setCapability("browser", browserName);
64+
capability.setCapability("browserVersion", browserVersion);
65+
capability.setCapability("name", "Sample TestNG Local Test");
66+
capability.setCapability("build", "Sample TestNG Tests");
67+
capability.setCapability("browserstack.local", "true");
68+
69+
driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability);
70+
}
71+
72+
@Test(groups = { "local_test" })
73+
public void testSimple() throws Exception {
74+
this.driver.get("http://www.google.com");
75+
System.out.println("Page title is: " + driver.getTitle());
76+
Assert.assertEquals("Google", driver.getTitle());
77+
WebElement element = driver.findElement(By.name("q"));
78+
element.sendKeys("BrowserStack");
79+
element.submit();
80+
driver = new Augmenter().augment(driver);
81+
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
82+
try {
83+
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
@AfterMethod(alwaysRun=true)
90+
public void tearDown() throws Exception {
91+
if(driver != null) {
92+
driver.quit();
93+
}
94+
}
95+
96+
@AfterSuite(alwaysRun=true)
97+
public void afterSuite() throws Exception {
98+
if(bsLocal != null) {
99+
bsLocal.stop();
100+
}
101+
}
102+
103+
@DataProvider(name = "getBrowsers")
104+
public static Object[][] createData1() {
105+
return new Object[][] {
106+
{ Platform.WINDOWS.toString(), "chrome", "48" },
107+
{ Platform.XP.toString(), "firefox", "44"},
108+
};
109+
}
110+
}

src/com/browserstack/TestNGParallel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.browserstack;
22

3-
import org.testng.annotations.Test;
4-
import org.testng.annotations.AfterMethod;
53
import org.testng.annotations.BeforeMethod;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.Test;
66
import org.testng.Assert;
77

88
import java.io.File;
@@ -29,7 +29,7 @@ public void setUp(String browser, String version, String platform) throws Except
2929
capability.setCapability("platform",platform);
3030
capability.setCapability("browserName", browser);
3131
capability.setCapability("browserVersion", version);
32-
capability.setCapability("project", "Parallel Tests");
32+
capability.setCapability("name", "Sample TestNG Parallel Tests");
3333
capability.setCapability("build", "Sample TestNG Tests");
3434
String username = System.getenv("BROWSERSTACK_USERNAME");
3535
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");

src/com/browserstack/TestNGSample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.browserstack;
22

33
import org.testng.annotations.DataProvider;
4+
import org.testng.annotations.BeforeMethod;
5+
import org.testng.annotations.AfterMethod;
46
import org.testng.annotations.Factory;
57
import org.testng.annotations.Test;
6-
import org.testng.annotations.AfterMethod;
7-
import org.testng.annotations.BeforeMethod;
88
import org.testng.Assert;
99

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

0 commit comments

Comments
 (0)