Skip to content

Commit 328788c

Browse files
authored
Merge pull request #17 from darshilgada24/AA_3417-update_junit_code_samples
Update Junit Code samples
2 parents 911e8ab + e309e22 commit 328788c

File tree

51 files changed

+2019
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2019
-291
lines changed

README.md

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,104 @@
11
# junit-appium-app-browserstack
2-
[JUnit](http://junit.org/junit4/) Integration with BrowserStack.
32

4-
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
3+
This repository demonstrates how to run Appium tests in [JUnit4](http://junit.org/junit4/) and [Junit5](https://junit.org/junit5/) on BrowserStack App Automate.
54

6-
![JUnit](http://junit.org/junit4/images/junit-logo.png)
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
76

8-
Code samples to get started with Appium tests for your Native App using the JUnit framework.
7+
## Setup
98

10-
## Resources
11-
* [Android Get Started guide](https://www.browserstack.com/app-automate/get-started)
12-
* [Android sample code repo](https://github.com/browserstack/junit-appium-app-browserstack/tree/master/android)
13-
* [IOS Get Started guide](https://www.browserstack.com/app-automate/get-started-ios)
14-
* [IOS sample code repo](https://github.com/browserstack/junit-appium-app-browserstack/tree/master/ios)
9+
### Requirements
10+
11+
1. Java 8+
12+
13+
- If Java is not installed, follow these instructions:
14+
- For Windows, download latest java version from [here](https://java.com/en/download/) and run the installer executable
15+
- For Mac and Linux, run `java -version` to see what java version is pre-installed. If you want a different version download from [here](https://java.com/en/download/)
16+
17+
2. Maven
18+
- If Maven is not downloaded, download it from [here](https://maven.apache.org/download.cgi)
19+
- For installation, follow the instructions [here](https://maven.apache.org/install.html)
20+
21+
### Install the dependencies
22+
23+
To install the dependencies for Android tests, run :
24+
25+
- For Junit4
26+
27+
```sh
28+
cd android/junit4-examples
29+
mvn clean
30+
```
31+
32+
- For Junit5
33+
34+
```sh
35+
cd android/junit5-examples
36+
mvn clean
37+
```
38+
39+
Or,
40+
41+
To install the dependencies for iOS tests, run :
42+
43+
- For Junit4
44+
45+
```sh
46+
cd ios/junit4-examples
47+
mvn clean
48+
```
49+
50+
- For Junit5
51+
52+
```sh
53+
cd ios/junit5-examples
54+
mvn clean
55+
```
56+
57+
## Getting Started
58+
59+
Getting Started with Appium tests in Junit4 and Junit5 on BrowserStack couldn't be easier!
60+
61+
### **Run first test :**
62+
63+
- Junit4
64+
- Switch to `run_first_test` directory under [Android Junit4 examples](android/junit4-examples) or [iOS Junit4 examples](ios/junit4-examples)
65+
- Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit)
66+
67+
- Junit5
68+
- Switch to `run_first_test` directory under [Android Junit5 examples](android/junit5-examples) or [iOS Junit5 examples](ios/junit5-examples)
69+
- Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit)
70+
71+
### **Speed up test execution with parallel testing :**
72+
73+
- Junit4
74+
- Switch to `run_parallel_test` directory under [Android Junit4 examples](android/junit4-examples/) or [iOS Junit4 examples](ios/junit4-examples/)
75+
- Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/parallelize-tests)
76+
77+
- Junit5
78+
- Switch to `run_parallel_test` directory under [Android Junit5 examples](android/junit5-examples/) or [iOS Junit5 examples](ios/junit5-examples/)
79+
- Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/parallelize-tests)
80+
81+
### **Use Local testing for apps that access resources hosted in development or testing environments :**
82+
83+
- Junit4
84+
- Switch to `run_local_test` directory under [Android Junit4 examples](android/junit4-examples/) or [iOS Junit4 examples](ios/junit4-examples/)
85+
- Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/local-testing)
86+
87+
- Junit5
88+
- Switch to `run_local_test` directory under [Android Junit5 examples](android/junit5-examples/) or [iOS Junit5 examples](ios/junit5-examples/)
89+
- Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/local-testing)
90+
91+
**Note**: If you are facing any issues, refer [Getting Help section](#Getting-Help)
92+
93+
## Integration with other Java frameworks
94+
95+
For other Java frameworks samples, refer to following repositories :
96+
97+
- [TestNG](https://github.com/browserstack/testng-appium-app-browserstack)
98+
- [Java](https://github.com/browserstack/java-appium-app-browserstack)
99+
100+
Note: For other test frameworks supported by App-Automate refer our [Developer documentation](https://www.browserstack.com/docs/)
101+
102+
## Getting Help
103+
104+
If you are running into any issues or have any queries, please check [Browserstack Support page](https://www.browserstack.com/support/app-automate) or [get in touch with us](https://www.browserstack.com/contact?ref=help).

android/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.browserstack</groupId>
6-
<artifactId>junit-browserstack</artifactId>
6+
<artifactId>junit4-browserstack</artifactId>
77
<version>1.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

@@ -32,12 +32,12 @@
3232
<dependency>
3333
<groupId>org.seleniumhq.selenium</groupId>
3434
<artifactId>selenium-java</artifactId>
35-
<version>2.52.0</version>
35+
<version>3.141.59</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>io.appium</groupId>
3939
<artifactId>java-client</artifactId>
40-
<version>4.1.2</version>
40+
<version>7.0.0</version>
4141
</dependency>
4242
<dependency>
4343
<groupId>com.browserstack</groupId>
@@ -56,26 +56,32 @@
5656
<plugin>
5757
<groupId>org.apache.maven.plugins</groupId>
5858
<artifactId>maven-surefire-plugin</artifactId>
59-
<version>2.12.4</version>
59+
<version>2.22.2</version>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<version>3.8.1</version>
65+
<configuration>
66+
<source>1.8</source>
67+
<target>1.8</target>
68+
</configuration>
6069
</plugin>
6170
</plugins>
6271
</build>
6372

6473
<profiles>
6574
<profile>
66-
<id>single</id>
75+
<id>first</id>
6776
<build>
6877
<plugins>
6978
<plugin>
7079
<groupId>org.apache.maven.plugins</groupId>
7180
<artifactId>maven-surefire-plugin</artifactId>
7281
<configuration>
7382
<includes>
74-
<include>com/browserstack/SingleTest.java</include>
83+
<include>com/browserstack/run_first_test/FirstTest.java</include>
7584
</includes>
76-
<systemPropertyVariables>
77-
<config>single.conf.json</config>
78-
</systemPropertyVariables>
7985
</configuration>
8086
</plugin>
8187
</plugins>
@@ -91,11 +97,8 @@
9197
<artifactId>maven-surefire-plugin</artifactId>
9298
<configuration>
9399
<includes>
94-
<include>com/browserstack/LocalTest.java</include>
100+
<include>com/browserstack/run_local_test/LocalTest.java</include>
95101
</includes>
96-
<systemPropertyVariables>
97-
<config>local.conf.json</config>
98-
</systemPropertyVariables>
99102
</configuration>
100103
</plugin>
101104
</plugins>
@@ -111,11 +114,8 @@
111114
<artifactId>maven-surefire-plugin</artifactId>
112115
<configuration>
113116
<includes>
114-
<include>com/browserstack/SingleTest.java</include>
117+
<include>com/browserstack/run_parallel_test/ParallelTest.java</include>
115118
</includes>
116-
<systemPropertyVariables>
117-
<config>parallel.conf.json</config>
118-
</systemPropertyVariables>
119119
</configuration>
120120
</plugin>
121121
</plugins>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.browserstack.run_first_test;
2+
3+
import java.net.URL;
4+
import java.util.Map;
5+
import java.util.List;
6+
import java.util.Iterator;
7+
import java.util.ArrayList;
8+
import java.io.FileReader;
9+
10+
import org.json.simple.JSONArray;
11+
import org.json.simple.JSONObject;
12+
import org.json.simple.parser.JSONParser;
13+
14+
import io.appium.java_client.android.AndroidDriver;
15+
import io.appium.java_client.android.AndroidElement;
16+
17+
import org.junit.runners.Parameterized;
18+
import org.openqa.selenium.remote.DesiredCapabilities;
19+
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized.Parameter;
24+
import org.junit.runners.Parameterized.Parameters;
25+
26+
@RunWith(Parameterized.class)
27+
public class BrowserStackJUnitTest {
28+
public AndroidDriver<AndroidElement> driver;
29+
30+
private static JSONObject config;
31+
32+
@Parameter(value = 0)
33+
public int taskID;
34+
35+
@Parameters
36+
public static Iterable<? extends Object> data() throws Exception {
37+
List<Integer> taskIDs = new ArrayList<Integer>();
38+
39+
JSONParser parser = new JSONParser();
40+
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_first_test/first.conf.json"));
41+
int envs = ((JSONArray) config.get("environments")).size();
42+
43+
for (int i = 0; i < envs; i++) {
44+
taskIDs.add(i);
45+
}
46+
47+
return taskIDs;
48+
}
49+
50+
@Before
51+
public void setUp() throws Exception {
52+
JSONArray envs = (JSONArray) config.get("environments");
53+
54+
DesiredCapabilities capabilities = new DesiredCapabilities();
55+
56+
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskID);
57+
Iterator it = envCapabilities.entrySet().iterator();
58+
while (it.hasNext()) {
59+
Map.Entry pair = (Map.Entry) it.next();
60+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
61+
}
62+
63+
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
64+
it = commonCapabilities.entrySet().iterator();
65+
while (it.hasNext()) {
66+
Map.Entry pair = (Map.Entry) it.next();
67+
if (capabilities.getCapability(pair.getKey().toString()) == null) {
68+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
69+
}
70+
}
71+
72+
String username = System.getenv("BROWSERSTACK_USERNAME");
73+
if (username == null) {
74+
username = (String) config.get("username");
75+
}
76+
77+
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
78+
if (accessKey == null) {
79+
accessKey = (String) config.get("access_key");
80+
}
81+
82+
String app = System.getenv("BROWSERSTACK_APP_ID");
83+
if (app != null && !app.isEmpty()) {
84+
capabilities.setCapability("app", app);
85+
}
86+
87+
driver = new AndroidDriver(new URL("http://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities);
88+
}
89+
90+
@After
91+
public void tearDown() throws Exception {
92+
// Invoke driver.quit() to indicate that the test is completed.
93+
// Otherwise, it will appear as timed out on BrowserStack.
94+
driver.quit();
95+
}
96+
}

android/src/test/java/com/browserstack/SingleTest.java renamed to android/junit4-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.browserstack;
1+
package com.browserstack.run_first_test;
22

33
import static org.junit.Assert.*;
44

@@ -11,7 +11,7 @@
1111
import org.openqa.selenium.support.ui.WebDriverWait;
1212
import org.openqa.selenium.support.ui.ExpectedConditions;
1313

14-
public class SingleTest extends BrowserStackJUnitTest {
14+
public class FirstTest extends BrowserStackJUnitTest {
1515

1616
@Test
1717
public void test() throws Exception {

0 commit comments

Comments
 (0)