Skip to content

TestNG iOS AppAutomate samples #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions ios/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Setup

* Clone the repo
* Install dependencies `mvn compile`
* Update `*.conf.json` files inside the `src/test/resources/conf` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)

## Running your tests
- To run a single test, run `mvn test -P single`
- To run parallel tests, run `mvn test -P parallel`
- To run local tests, run `mvn test -P local`

## Notes
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
* Refer [Get Started](https://www.browserstack.com/app-automate/get-started#getting-started) document to configure the capabilities
* You can export the environment variables for the Username and Access Key of your BrowserStack account

```
export BROWSERSTACK_USERNAME=<browserstack-username> &&
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```

## Additional Resources
* [Getting Started with App Automate](https://www.browserstack.com/app-automate/get-started)
11 changes: 11 additions & 0 deletions ios/config/local.testng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Local">
<test name="LocalTest">
<parameter name="config" value="local.conf.json"/>
<parameter name="environment" value="iPhone_7"/>
<classes>
<class name="com.browserstack.LocalTest"/>
</classes>
</test>
</suite>
19 changes: 19 additions & 0 deletions ios/config/parallel.testng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="2" parallel="tests">
<test name="SingleTestiPhone7">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="iPhone_7"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>

<test name="SingleTestiPhone7Plus">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="iPhone_7_Plus"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
</suite>
11 changes: 11 additions & 0 deletions ios/config/single.testng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Single">
<test name="SingleTest">
<parameter name="config" value="single.conf.json"/>
<parameter name="environment" value="iPhone_7"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
</suite>
37 changes: 37 additions & 0 deletions ios/config/suite.testng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Pool" parallel="tests">
<test name="PoolTestiPhone7" thread-count="3" parallel="classes">
<parameter name="config" value="suite.conf.json"/>
<parameter name="environment" value="iPhone_7"/>
<classes>
<class name="com.browserstack.suite.SuiteTest01"/>
<class name="com.browserstack.suite.SuiteTest02"/>
<class name="com.browserstack.suite.SuiteTest03"/>
<class name="com.browserstack.suite.SuiteTest04"/>
<class name="com.browserstack.suite.SuiteTest05"/>
<class name="com.browserstack.suite.SuiteTest06"/>
<class name="com.browserstack.suite.SuiteTest07"/>
<class name="com.browserstack.suite.SuiteTest08"/>
<class name="com.browserstack.suite.SuiteTest09"/>
<class name="com.browserstack.suite.SuiteTest10"/>
</classes>
</test>

<test name="PoolTestiPhone7Plus" thread-count="3" parallel="classes">
<parameter name="config" value="suite.conf.json"/>
<parameter name="environment" value="iPhone_7_Plus"/>
<classes>
<class name="com.browserstack.suite.SuiteTest01"/>
<class name="com.browserstack.suite.SuiteTest02"/>
<class name="com.browserstack.suite.SuiteTest03"/>
<class name="com.browserstack.suite.SuiteTest04"/>
<class name="com.browserstack.suite.SuiteTest05"/>
<class name="com.browserstack.suite.SuiteTest06"/>
<class name="com.browserstack.suite.SuiteTest07"/>
<class name="com.browserstack.suite.SuiteTest08"/>
<class name="com.browserstack.suite.SuiteTest09"/>
<class name="com.browserstack.suite.SuiteTest10"/>
</classes>
</test>
</suite>
139 changes: 139 additions & 0 deletions ios/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.browserstack</groupId>
<artifactId>testng-browserstack</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>testng-browserstack</name>
<url>https://github.com/browserstack/testng-appium-app-browserstack</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.version>2.19.1</surefire.version>

<test.file></test.file>
<config.file>default</config.file>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.52.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>single</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/single.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>local</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/local.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>parallel</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/parallel.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>suite</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/suite.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
76 changes: 76 additions & 0 deletions ios/src/test/java/com/browserstack/BrowserStackTestNGTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.browserstack;
import com.browserstack.local.Local;

import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Parameters;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;


public class BrowserStackTestNGTest {
public IOSDriver<IOSElement> driver;
private Local l;

@BeforeMethod(alwaysRun=true)
@org.testng.annotations.Parameters(value={"config", "environment"})
public void setUp(String config_file, String environment) throws Exception {
JSONParser parser = new JSONParser();
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
JSONObject envs = (JSONObject) config.get("environments");

DesiredCapabilities capabilities = new DesiredCapabilities();

Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
Iterator it = envCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}

Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
it = commonCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(capabilities.getCapability(pair.getKey().toString()) == null){
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}
}

String username = System.getenv("BROWSERSTACK_USERNAME");
if(username == null) {
username = (String) config.get("user");
}

String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if(accessKey == null) {
accessKey = (String) config.get("key");
}

if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}

driver = new IOSDriver<IOSElement>(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
}

@AfterMethod(alwaysRun=true)
public void tearDown() throws Exception {
driver.quit();
if(l != null) l.stop();
}
}
50 changes: 50 additions & 0 deletions ios/src/test/java/com/browserstack/LocalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.browserstack;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import org.apache.commons.io.FileUtils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;

import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSElement;


public class LocalTest extends BrowserStackTestNGTest {

@Test
public void test() throws Exception {
IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until(
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal")));
testButton.click();

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver d) {
String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value");
return result != null && result.length() > 0;
}
});
IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal"));

String resultString = resultElement.getText().toLowerCase();
System.out.println(resultString);
if(resultString.contains("not working")) {
File scrFile = (File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png"));
System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png");
throw new Error("Unexpected BrowserStackLocal test result");
}

String expectedString = "Up and running";
Assert.assertTrue(resultString.contains(expectedString.toLowerCase()));
}
}
Loading