Skip to content

Commit 37de24b

Browse files
committed
Merge pull request #1 from browserstack/testng-tests
Testng tests
2 parents a6d8f0b + 6a3cc2b commit 37de24b

11 files changed

+118
-40
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test-output/
2+
dist/
3+
bin/
4+
*.png

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
TestNG-BrowserStack
2+
=========
3+
4+
Sample for running [TestNG] tests with BrowserStack Automate.
5+
6+
### Configuring the json
7+
- Open `src/com/browserstack/TestNGSample.java` or `src/com/browserstack/TestNGParallel.java`.
8+
- Add `username` and `accessKey` with your BrowserStack credentials. Don't have one? Get one on BrowserStack [dashboard].
9+
- Add / customise more [capabilities].
10+
- Optionally, you can add your BrowserStack credentials to the environment variables `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`.
11+
12+
### Running the tests
13+
- To start tests in series, run: `ant test-series`
14+
- To start parallel tests run: `ant test-parallel`
15+
16+
[TestNG]:http://testng.org
17+
[capabilities]:http://www.browserstack.com/automate/capabilities
18+
[dashboard]:https://www.browserstack.com/automate

Testng.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
<parameter name="version" value="20.0"/>
77
<parameter name="platform" value="MAC"/>
88
<classes>
9-
<class name="bs.TestNGParallel"/>
9+
<class name="com.browserstack.TestNGParallel"/>
1010
</classes>
1111
</test> <!-- Test -->
1212
<test name="SecondTest">
1313
<parameter name="browser" value="chrome"/>
1414
<parameter name="version" value="26.0"/>
1515
<parameter name="platform" value="WINDOWS"/>
1616
<classes>
17-
<class name="bs.TestNGParallel"/>
17+
<class name="com.browserstack.TestNGParallel"/>
1818
</classes>
1919
</test> <!-- Test -->
2020
<test name="ThirdTest">
2121
<parameter name="browser" value="safari"/>
2222
<parameter name="version" value="6.0"/>
2323
<parameter name="platform" value="MAC"/>
2424
<classes>
25-
<class name="bs.TestNGParallel"/>
25+
<class name="com.browserstack.TestNGParallel"/>
2626
</classes>
2727
</test> <!-- Test -->
2828
</suite> <!-- Suite -->

build.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project name="testng-browserstack" default="main" basedir=".">
2+
<description>
3+
Run Testng scripts on BrowserStack
4+
</description>
5+
6+
<property name="projectName" value="testng-browserstack" />
7+
8+
<property name="src.dir" location="src" />
9+
10+
<property name="build.dir" location="bin" />
11+
12+
<target name="init">
13+
<tstamp />
14+
<mkdir dir="${build.dir}" />
15+
</target>
16+
17+
<path id="build.classpath">
18+
<fileset dir="lib">
19+
<include name="*.jar"/>
20+
</fileset>
21+
</path>
22+
23+
<target name="compile" depends="init" description="compile the source ">
24+
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
25+
<classpath refid="build.classpath"/>
26+
</javac>
27+
</target>
28+
29+
<taskdef name="testng" classpathref="build.classpath" classname="org.testng.TestNGAntTask" />
30+
31+
<target name="test-parallel" depends="compile" description="run the tests on BrowserStack">
32+
<testng classpathref="build.classpath" verbose="10" >
33+
<classpath location="bin" />
34+
<xmlfileset dir="." includes="testng.xml"/>
35+
</testng>
36+
</target>
37+
38+
<target name="test-series" depends="compile" description="run the tests on BrowserStack">
39+
<testng classpathref="build.classpath" verbose="10" groups="series_test">
40+
<classpath location="bin" />
41+
<classfileset dir="bin" includes="**/**/TestNGSample.class"/>
42+
</testng>
43+
</target>
44+
45+
<target name="clean" description="clean up">
46+
<delete dir="${build.dir}" />
47+
</target>
48+
49+
<target name="main" depends="clean, compile, test-parallel" />
50+
51+
</project>

lib/commons-io-2.4.jar

181 KB
Binary file not shown.

lib/jcommander-1.27.jar

54.3 KB
Binary file not shown.

lib/selenium-java-2.52.0.jar

1.8 MB
Binary file not shown.
29.5 MB
Binary file not shown.

lib/testng-6.9.10-SNAPSHOT.jar

850 KB
Binary file not shown.
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package bs;
1+
package com.browserstack;
22

33
import org.testng.annotations.Test;
4-
import org.testng.annotations.AfterClass;
5-
import org.testng.annotations.BeforeClass;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
66
import org.testng.Assert;
77

88
import java.io.File;
@@ -11,39 +11,38 @@
1111

1212
import org.apache.commons.io.FileUtils;
1313
import org.openqa.selenium.By;
14-
import org.openqa.selenium.OutputType;
15-
import org.openqa.selenium.TakesScreenshot;
1614
import org.openqa.selenium.WebDriver;
1715
import org.openqa.selenium.WebElement;
16+
import org.openqa.selenium.OutputType;
17+
import org.openqa.selenium.TakesScreenshot;
1818
import org.openqa.selenium.remote.Augmenter;
19-
import org.openqa.selenium.remote.DesiredCapabilities;
2019
import org.openqa.selenium.remote.RemoteWebDriver;
20+
import org.openqa.selenium.remote.DesiredCapabilities;
2121

2222
public class TestNGParallel {
23+
private WebDriver driver;
2324

24-
private WebDriver driver;
25-
26-
@BeforeClass
25+
@BeforeMethod(alwaysRun=true)
2726
@org.testng.annotations.Parameters(value={"browser","version","platform"})
2827
public void setUp(String browser, String version, String platform) throws Exception {
2928
DesiredCapabilities capability = new DesiredCapabilities();
3029
capability.setCapability("platform",platform);
3130
capability.setCapability("browserName", browser);
3231
capability.setCapability("browserVersion", version);
33-
capability.setCapability("project", "P1");
34-
capability.setCapability("build", "1.0");
35-
driver = new RemoteWebDriver(
36-
new URL("http://USERNAME:[email protected]/wd/hub"),
37-
capability);
38-
}
32+
capability.setCapability("project", "Parallel Tests");
33+
capability.setCapability("build", "Sample TestNG Tests");
34+
String username = System.getenv("BROWSERSTACK_USERNAME");
35+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
36+
driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability);
37+
}
3938

4039
@Test
4140
public void testSimple() throws Exception {
4241
driver.get("http://www.google.com");
4342
System.out.println("Page title is: " + driver.getTitle());
4443
Assert.assertEquals("Google", driver.getTitle());
4544
WebElement element = driver.findElement(By.name("q"));
46-
element.sendKeys("Browser Stack");
45+
element.sendKeys("BrowserStack");
4746
element.submit();
4847
driver = new Augmenter().augment(driver);
4948
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
@@ -54,8 +53,8 @@ public void testSimple() throws Exception {
5453
}
5554
}
5655

57-
@AfterClass
58-
public void tearDown() throws Exception {
59-
driver.quit();
56+
@AfterMethod(alwaysRun=true)
57+
public void tearDown() throws Exception {
58+
driver.quit();
6059
}
6160
}

0 commit comments

Comments
 (0)