Skip to content

Commit 9ae25e4

Browse files
committed
Tests with ant script
1 parent a6d8f0b commit 9ae25e4

10 files changed

+113
-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

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
<property name="dist.dir" location="dist" />
13+
14+
<target name="init">
15+
<tstamp />
16+
<mkdir dir="${build.dir}" />
17+
</target>
18+
19+
<path id="build.classpath">
20+
<fileset dir="lib">
21+
<include name="*.jar"/>
22+
</fileset>
23+
</path>
24+
25+
<target name="compile" depends="init" description="compile the source ">
26+
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
27+
<classpath refid="build.classpath"/>
28+
</javac>
29+
</target>
30+
31+
<target name="dist" depends="compile" description="create a JAR for the tests">
32+
<mkdir dir="${dist.dir}" />
33+
34+
<jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}" >
35+
<manifest>
36+
<attribute name="Main-Class" value="com.browserstack.TestNGParallel" />
37+
</manifest>
38+
</jar>
39+
</target>
40+
41+
<taskdef name="testng" classpathref="build.classpath" classname="org.testng.TestNGAntTask" />
42+
43+
<target name="test-parallel" depends="dist" description="run the tests on BrowserStack">
44+
<testng classpathref="build.classpath" verbose="10" >
45+
<classpath location="bin" />
46+
<xmlfileset dir="." includes="testng.xml"/>
47+
</testng>
48+
</target>
49+
50+
<target name="test-series" depends="dist" description="run the tests on BrowserStack">
51+
<testng classpathref="build.classpath" verbose="10" groups="series_test">
52+
<classpath location="bin" />
53+
<classfileset dir="bin" includes="**/**/TestNGSample.class"/>
54+
</testng>
55+
</target>
56+
57+
<target name="clean" description="clean up">
58+
<delete dir="${build.dir}" />
59+
<delete dir="${dist.dir}" />
60+
</target>
61+
62+
<target name="main" depends="clean, compile, dist, test-parallel" />
63+
64+
</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
}

TestNGSample.java renamed to src/com/browserstack/TestNGSample.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.testng.annotations.DataProvider;
44
import org.testng.annotations.Factory;
55
import org.testng.annotations.Test;
6-
import org.testng.annotations.AfterClass;
7-
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.AfterMethod;
7+
import org.testng.annotations.BeforeMethod;
88
import org.testng.Assert;
99

1010
import java.io.File;
@@ -13,14 +13,14 @@
1313

1414
import org.apache.commons.io.FileUtils;
1515
import org.openqa.selenium.By;
16-
import org.openqa.selenium.OutputType;
1716
import org.openqa.selenium.Platform;
18-
import org.openqa.selenium.TakesScreenshot;
1917
import org.openqa.selenium.WebDriver;
2018
import org.openqa.selenium.WebElement;
19+
import org.openqa.selenium.OutputType;
20+
import org.openqa.selenium.TakesScreenshot;
2121
import org.openqa.selenium.remote.Augmenter;
22-
import org.openqa.selenium.remote.DesiredCapabilities;
2322
import org.openqa.selenium.remote.RemoteWebDriver;
23+
import org.openqa.selenium.remote.DesiredCapabilities;
2424

2525
public class TestNGSample {
2626
private String platform;
@@ -34,24 +34,28 @@ public TestNGSample(String platform,String browserName,String browserVersion) {
3434
this.browserVersion = browserVersion;
3535
}
3636

37-
private WebDriver driver;
37+
private WebDriver driver;
3838

39-
@BeforeClass
39+
@BeforeMethod(alwaysRun=true)
4040
public void setUp() throws Exception {
4141
DesiredCapabilities capability = new DesiredCapabilities();
4242
capability.setCapability("platform",platform);
4343
capability.setCapability("browser", browserName);
4444
capability.setCapability("browserVersion", browserVersion);
45-
driver = new RemoteWebDriver(new URL("http://USERNAME:[email protected]/wd/hub"), capability);
46-
}
45+
capability.setCapability("build", "Sample TestNG Tests");
46+
capability.setCapability("project", "Series Tests");
47+
String username = System.getenv("BROWSERSTACK_USERNAME");
48+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
49+
driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability);
50+
}
4751

48-
@Test
52+
@Test(groups = { "series_test" })
4953
public void testSimple() throws Exception {
50-
driver.get("http://www.google.com");
54+
this.driver.get("http://www.google.com");
5155
System.out.println("Page title is: " + driver.getTitle());
5256
Assert.assertEquals("Google", driver.getTitle());
5357
WebElement element = driver.findElement(By.name("q"));
54-
element.sendKeys("Browser Stack");
58+
element.sendKeys("BrowserStack");
5559
element.submit();
5660
driver = new Augmenter().augment(driver);
5761
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
@@ -62,16 +66,18 @@ public void testSimple() throws Exception {
6266
}
6367
}
6468

65-
@AfterClass
66-
public void tearDown() throws Exception {
67-
driver.quit();
68-
}
69+
@AfterMethod(alwaysRun=true)
70+
public void tearDown() throws Exception {
71+
if(driver != null) {
72+
driver.quit();
73+
}
74+
}
6975

7076
@DataProvider(name = "getBrowsers")
7177
public static Object[][] createData1() {
7278
return new Object[][] {
73-
{ Platform.WINDOWS.toString(), "chrome", "27" },
74-
{ Platform.XP.toString(), "firefox", "19"},
79+
{ Platform.WINDOWS.toString(), "chrome", "48" },
80+
{ Platform.XP.toString(), "firefox", "44"},
7581
};
7682
}
7783
}

0 commit comments

Comments
 (0)