Skip to content

Commit a18ffa0

Browse files
author
Tejas Shah
committed
Add AppAutomate ios sample tests
1 parent b547aaa commit a18ffa0

23 files changed

+991
-0
lines changed

ios/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Setup
2+
3+
* Clone the repo
4+
* Install dependencies `mvn compile`
5+
* Update `*.conf.json` files inside the `src/test/resources/conf` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
6+
7+
## Running your tests
8+
- To run a single test, run `mvn test -P single`
9+
- To run parallel tests, run `mvn test -P parallel`
10+
- To run local tests, run `mvn test -P local`
11+
12+
## Notes
13+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
14+
* Refer [Get Started](https://www.browserstack.com/app-automate/get-started#getting-started) document to configure the capabilities
15+
* You can export the environment variables for the Username and Access Key of your BrowserStack account
16+
17+
```
18+
export BROWSERSTACK_USERNAME=<browserstack-username> &&
19+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
20+
```
21+
22+
## Additional Resources
23+
* [Getting Started with App Automate](https://www.browserstack.com/app-automate/get-started)

ios/config/local.testng.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Local">
4+
<test name="LocalTest">
5+
<parameter name="config" value="local.conf.json"/>
6+
<parameter name="environment" value="iPhone_7"/>
7+
<classes>
8+
<class name="com.browserstack.LocalTest"/>
9+
</classes>
10+
</test>
11+
</suite>

ios/config/parallel.testng.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Parallel" thread-count="2" parallel="tests">
4+
<test name="SingleTestiPhone7">
5+
<parameter name="config" value="parallel.conf.json"/>
6+
<parameter name="environment" value="iPhone_7"/>
7+
<classes>
8+
<class name="com.browserstack.SingleTest"/>
9+
</classes>
10+
</test>
11+
12+
<test name="SingleTestiPhone7Plus">
13+
<parameter name="config" value="parallel.conf.json"/>
14+
<parameter name="environment" value="iPhone_7_Plus"/>
15+
<classes>
16+
<class name="com.browserstack.SingleTest"/>
17+
</classes>
18+
</test>
19+
</suite>

ios/config/single.testng.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Single">
4+
<test name="SingleTest">
5+
<parameter name="config" value="single.conf.json"/>
6+
<parameter name="environment" value="iPhone_7"/>
7+
<classes>
8+
<class name="com.browserstack.SingleTest"/>
9+
</classes>
10+
</test>
11+
</suite>

ios/config/suite.testng.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Pool" parallel="tests">
4+
<test name="PoolTestiPhone7" thread-count="3" parallel="classes">
5+
<parameter name="config" value="suite.conf.json"/>
6+
<parameter name="environment" value="iPhone_7"/>
7+
<classes>
8+
<class name="com.browserstack.suite.SuiteTest01"/>
9+
<class name="com.browserstack.suite.SuiteTest02"/>
10+
<class name="com.browserstack.suite.SuiteTest03"/>
11+
<class name="com.browserstack.suite.SuiteTest04"/>
12+
<class name="com.browserstack.suite.SuiteTest05"/>
13+
<class name="com.browserstack.suite.SuiteTest06"/>
14+
<class name="com.browserstack.suite.SuiteTest07"/>
15+
<class name="com.browserstack.suite.SuiteTest08"/>
16+
<class name="com.browserstack.suite.SuiteTest09"/>
17+
<class name="com.browserstack.suite.SuiteTest10"/>
18+
</classes>
19+
</test>
20+
21+
<test name="PoolTestiPhone7Plus" thread-count="3" parallel="classes">
22+
<parameter name="config" value="suite.conf.json"/>
23+
<parameter name="environment" value="iPhone_7_Plus"/>
24+
<classes>
25+
<class name="com.browserstack.suite.SuiteTest01"/>
26+
<class name="com.browserstack.suite.SuiteTest02"/>
27+
<class name="com.browserstack.suite.SuiteTest03"/>
28+
<class name="com.browserstack.suite.SuiteTest04"/>
29+
<class name="com.browserstack.suite.SuiteTest05"/>
30+
<class name="com.browserstack.suite.SuiteTest06"/>
31+
<class name="com.browserstack.suite.SuiteTest07"/>
32+
<class name="com.browserstack.suite.SuiteTest08"/>
33+
<class name="com.browserstack.suite.SuiteTest09"/>
34+
<class name="com.browserstack.suite.SuiteTest10"/>
35+
</classes>
36+
</test>
37+
</suite>

ios/pom.xml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.browserstack</groupId>
6+
<artifactId>testng-browserstack</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>testng-browserstack</name>
11+
<url>https://github.com/browserstack/testng-appium-app-browserstack</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<surefire.version>2.19.1</surefire.version>
16+
17+
<test.file></test.file>
18+
<config.file>default</config.file>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.testng</groupId>
24+
<artifactId>testng</artifactId>
25+
<version>6.9.10</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.appium</groupId>
29+
<artifactId>java-client</artifactId>
30+
<version>4.1.2</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>commons-io</groupId>
34+
<artifactId>commons-io</artifactId>
35+
<version>1.3.2</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.seleniumhq.selenium</groupId>
39+
<artifactId>selenium-java</artifactId>
40+
<version>2.52.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.browserstack</groupId>
44+
<artifactId>browserstack-local-java</artifactId>
45+
<version>0.1.0</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.googlecode.json-simple</groupId>
49+
<artifactId>json-simple</artifactId>
50+
<version>1.1.1</version>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>2.12.4</version>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>2.18.1</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
<profiles>
70+
<profile>
71+
<id>single</id>
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<configuration>
78+
<suiteXmlFiles>
79+
<suiteXmlFile>config/single.testng.xml</suiteXmlFile>
80+
</suiteXmlFiles>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</profile>
86+
87+
<profile>
88+
<id>local</id>
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-surefire-plugin</artifactId>
94+
<configuration>
95+
<suiteXmlFiles>
96+
<suiteXmlFile>config/local.testng.xml</suiteXmlFile>
97+
</suiteXmlFiles>
98+
</configuration>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
</profile>
103+
104+
<profile>
105+
<id>parallel</id>
106+
<build>
107+
<plugins>
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-surefire-plugin</artifactId>
111+
<configuration>
112+
<suiteXmlFiles>
113+
<suiteXmlFile>config/parallel.testng.xml</suiteXmlFile>
114+
</suiteXmlFiles>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</build>
119+
</profile>
120+
121+
<profile>
122+
<id>suite</id>
123+
<build>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-surefire-plugin</artifactId>
128+
<configuration>
129+
<suiteXmlFiles>
130+
<suiteXmlFile>config/suite.testng.xml</suiteXmlFile>
131+
</suiteXmlFiles>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</profile>
137+
</profiles>
138+
139+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.browserstack;
2+
import com.browserstack.local.Local;
3+
4+
import java.net.URL;
5+
import java.util.Map;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.io.FileReader;
9+
import org.json.simple.JSONObject;
10+
import org.json.simple.parser.JSONParser;
11+
12+
import org.openqa.selenium.remote.DesiredCapabilities;
13+
14+
import org.testng.annotations.BeforeMethod;
15+
import org.testng.annotations.AfterMethod;
16+
import org.testng.annotations.Parameters;
17+
18+
import io.appium.java_client.ios.IOSDriver;
19+
import io.appium.java_client.ios.IOSElement;
20+
21+
22+
public class BrowserStackTestNGTest {
23+
public IOSDriver<IOSElement> driver;
24+
private Local l;
25+
26+
@BeforeMethod(alwaysRun=true)
27+
@org.testng.annotations.Parameters(value={"config", "environment"})
28+
public void setUp(String config_file, String environment) throws Exception {
29+
JSONParser parser = new JSONParser();
30+
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
31+
JSONObject envs = (JSONObject) config.get("environments");
32+
33+
DesiredCapabilities capabilities = new DesiredCapabilities();
34+
35+
Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
36+
Iterator it = envCapabilities.entrySet().iterator();
37+
while (it.hasNext()) {
38+
Map.Entry pair = (Map.Entry)it.next();
39+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
40+
}
41+
42+
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
43+
it = commonCapabilities.entrySet().iterator();
44+
while (it.hasNext()) {
45+
Map.Entry pair = (Map.Entry)it.next();
46+
if(capabilities.getCapability(pair.getKey().toString()) == null){
47+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
48+
}
49+
}
50+
51+
String username = System.getenv("BROWSERSTACK_USERNAME");
52+
if(username == null) {
53+
username = (String) config.get("user");
54+
}
55+
56+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
57+
if(accessKey == null) {
58+
accessKey = (String) config.get("key");
59+
}
60+
61+
if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
62+
l = new Local();
63+
Map<String, String> options = new HashMap<String, String>();
64+
options.put("key", accessKey);
65+
l.start(options);
66+
}
67+
68+
driver = new IOSDriver<IOSElement>(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
69+
}
70+
71+
@AfterMethod(alwaysRun=true)
72+
public void tearDown() throws Exception {
73+
driver.quit();
74+
if(l != null) l.stop();
75+
}
76+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.browserstack;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
import java.io.File;
7+
import org.apache.commons.io.FileUtils;
8+
9+
import org.openqa.selenium.WebDriver;
10+
import org.openqa.selenium.OutputType;
11+
import org.openqa.selenium.TakesScreenshot;
12+
import org.openqa.selenium.support.ui.WebDriverWait;
13+
import org.openqa.selenium.support.ui.ExpectedCondition;
14+
import org.openqa.selenium.support.ui.ExpectedConditions;
15+
16+
import io.appium.java_client.MobileBy;
17+
import io.appium.java_client.ios.IOSElement;
18+
19+
20+
public class LocalTest extends BrowserStackTestNGTest {
21+
22+
@Test
23+
public void test() throws Exception {
24+
IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until(
25+
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal")));
26+
testButton.click();
27+
28+
WebDriverWait wait = new WebDriverWait(driver, 30);
29+
wait.until(new ExpectedCondition<Boolean>() {
30+
@Override
31+
public Boolean apply(WebDriver d) {
32+
String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value");
33+
return result != null && result.length() > 0;
34+
}
35+
});
36+
IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal"));
37+
38+
String resultString = resultElement.getText().toLowerCase();
39+
System.out.println(resultString);
40+
if(resultString.contains("not working")) {
41+
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
42+
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png"));
43+
System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png");
44+
throw new Error("Unexpected BrowserStackLocal test result");
45+
}
46+
47+
String expectedString = "Up and running";
48+
Assert.assertTrue(resultString.contains(expectedString.toLowerCase()));
49+
}
50+
}

0 commit comments

Comments
 (0)