Skip to content

Commit c12eeef

Browse files
committed
add junit 4 and junit 5 for android
1 parent 911e8ab commit c12eeef

File tree

36 files changed

+1385
-63
lines changed

36 files changed

+1385
-63
lines changed

.DS_Store

6 KB
Binary file not shown.

android/.DS_Store

6 KB
Binary file not shown.
2.11 MB
Binary file not shown.

android/TestJunit5/pom.xml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>TestJunit5</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.junit.jupiter</groupId>
14+
<artifactId>junit-jupiter-engine</artifactId>
15+
<version>5.5.2</version>
16+
<scope>test</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter-params</artifactId>
21+
<version>5.4.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>commons-io</groupId>
26+
<artifactId>commons-io</artifactId>
27+
<version>1.3.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.seleniumhq.selenium</groupId>
31+
<artifactId>selenium-java</artifactId>
32+
<version>2.52.0</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.appium</groupId>
36+
<artifactId>java-client</artifactId>
37+
<version>4.1.2</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.browserstack</groupId>
41+
<artifactId>browserstack-local-java</artifactId>
42+
<version>1.0.3</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.googlecode.json-simple</groupId>
46+
<artifactId>json-simple</artifactId>
47+
<version>1.1.1</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>2.22.1</version>
57+
<configuration>
58+
<properties>
59+
<configurationParameters>
60+
junit.jupiter.execution.parallel.enabled=true
61+
</configurationParameters>
62+
</properties>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<configuration>
69+
<source>1.8</source>
70+
<target>1.8</target>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
<profiles>
77+
<profile>
78+
<id>single</id>
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-surefire-plugin</artifactId>
84+
<configuration>
85+
<includes>
86+
<include>SingleTest.java</include>
87+
</includes>
88+
<systemPropertyVariables>
89+
<config>single.conf.json</config>
90+
</systemPropertyVariables>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</profile>
96+
97+
<profile>
98+
<id>parallel</id>
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-surefire-plugin</artifactId>
104+
<configuration>
105+
<includes>
106+
<include>ParallelTest.java</include>
107+
</includes>
108+
<systemPropertyVariables>
109+
<config>parallel.conf.json</config>
110+
</systemPropertyVariables>
111+
</configuration>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</profile>
116+
117+
<profile>
118+
<id>local</id>
119+
<build>
120+
<plugins>
121+
<plugin>
122+
<groupId>org.apache.maven.plugins</groupId>
123+
<artifactId>maven-surefire-plugin</artifactId>
124+
<configuration>
125+
<includes>
126+
<include>LocalTest.java</include>
127+
</includes>
128+
<systemPropertyVariables>
129+
<config>local.conf.json</config>
130+
</systemPropertyVariables>
131+
</configuration>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
</profile>
136+
</profiles>
137+
138+
</project>
139+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import com.browserstack.local.Local;
2+
import io.appium.java_client.android.AndroidDriver;
3+
import io.appium.java_client.android.AndroidElement;
4+
import org.json.simple.JSONArray;
5+
import org.json.simple.JSONObject;
6+
import org.json.simple.parser.JSONParser;
7+
import org.json.simple.parser.ParseException;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeEach;
10+
11+
import org.junit.jupiter.api.parallel.Execution;
12+
import org.junit.jupiter.api.parallel.ExecutionMode;
13+
import org.openqa.selenium.remote.DesiredCapabilities;
14+
15+
import java.io.FileReader;
16+
import java.io.IOException;
17+
import java.util.*;
18+
import java.util.stream.Stream;
19+
20+
@Execution(ExecutionMode.CONCURRENT)
21+
public class AppTest {
22+
23+
public AndroidDriver<AndroidElement> driver;
24+
private Local l;
25+
public String username;
26+
public String accessKey;
27+
public DesiredCapabilities capabilities;
28+
29+
public static JSONObject config;
30+
31+
32+
private static Stream<Integer> devices() throws IOException, ParseException {
33+
34+
List<Integer> taskIDs = new ArrayList<Integer>();
35+
36+
if(System.getProperty("config") != null) {
37+
JSONParser parser = new JSONParser();
38+
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config")));
39+
int envs = ((JSONArray)config.get("environments")).size();
40+
41+
for(int i=0; i<envs; i++) {
42+
taskIDs.add(i);
43+
}
44+
}
45+
46+
return taskIDs.stream();
47+
}
48+
49+
@BeforeEach
50+
public void setup() throws Exception {
51+
System.out.println("@BeforeEach executed");
52+
53+
capabilities = new DesiredCapabilities();
54+
55+
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
56+
Iterator it = commonCapabilities.entrySet().iterator();
57+
while (it.hasNext()) {
58+
Map.Entry pair = (Map.Entry)it.next();
59+
if(capabilities.getCapability(pair.getKey().toString()) == null){
60+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
61+
}
62+
}
63+
64+
username = System.getenv("BROWSERSTACK_USERNAME");
65+
if(username == null) {
66+
username = (String) config.get("user");
67+
}
68+
69+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
70+
if(accessKey == null) {
71+
accessKey = (String) config.get("key");
72+
}
73+
74+
String app = System.getenv("BROWSERSTACK_APP_ID");
75+
if(app != null && !app.isEmpty()) {
76+
capabilities.setCapability("app", app);
77+
}
78+
79+
if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
80+
l = new Local();
81+
Map<String, String> options = new HashMap<String, String>();
82+
options.put("key", accessKey);
83+
l.start(options);
84+
}
85+
}
86+
87+
@AfterEach
88+
public void tearDown() throws Exception {
89+
System.out.println("@AfterEach executed");
90+
driver.quit();
91+
92+
if(l != null) l.stop();
93+
}
94+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import io.appium.java_client.android.AndroidDriver;
2+
import org.json.simple.JSONArray;
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.MethodSource;
5+
6+
import java.io.IOException;
7+
import java.net.URL;
8+
import java.util.Iterator;
9+
import java.util.Map;
10+
11+
public class LocalTest extends AppTest{
12+
13+
@ParameterizedTest
14+
@MethodSource("devices")
15+
void testCalcOne(int taskId) throws IOException {
16+
17+
JSONArray envs = (JSONArray) config.get("environments");
18+
19+
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskId);
20+
Iterator it = envCapabilities.entrySet().iterator();
21+
while (it.hasNext()) {
22+
Map.Entry pair = (Map.Entry)it.next();
23+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
24+
}
25+
26+
driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
27+
28+
System.out.println("Hi From testCalcOne");
29+
String sessionId = driver.getSessionId().toString();
30+
31+
System.out.println("Session Id: " + sessionId);
32+
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import io.appium.java_client.MobileBy;
2+
import io.appium.java_client.android.AndroidDriver;
3+
import io.appium.java_client.android.AndroidElement;
4+
import org.json.simple.JSONArray;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
10+
import java.io.IOException;
11+
import java.net.URL;
12+
import java.util.Iterator;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
public class ParallelTest extends AppTest{
19+
20+
@ParameterizedTest
21+
@MethodSource("devices")
22+
void testCalcOne(int taskId) throws IOException, InterruptedException {
23+
24+
JSONArray envs = (JSONArray) config.get("environments");
25+
26+
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskId);
27+
Iterator it = envCapabilities.entrySet().iterator();
28+
while (it.hasNext()) {
29+
Map.Entry pair = (Map.Entry)it.next();
30+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
31+
}
32+
33+
driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
34+
35+
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
36+
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));
37+
searchElement.click();
38+
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
39+
ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text")));
40+
insertTextElement.sendKeys("BrowserStack");
41+
Thread.sleep(5000);
42+
43+
List<AndroidElement> allProductsName = driver.findElementsByClassName("android.widget.TextView");
44+
assertTrue(allProductsName.size() > 0);
45+
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import io.appium.java_client.android.AndroidDriver;
2+
import org.json.simple.JSONArray;
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.MethodSource;
5+
6+
import java.io.IOException;
7+
import java.net.URL;
8+
import java.util.Iterator;
9+
import java.util.Map;
10+
11+
public class SingleTest extends AppTest{
12+
13+
@ParameterizedTest
14+
@MethodSource("devices")
15+
void testCalcOne(int taskId) throws IOException {
16+
17+
JSONArray envs = (JSONArray) config.get("environments");
18+
19+
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskId);
20+
Iterator it = envCapabilities.entrySet().iterator();
21+
while (it.hasNext()) {
22+
Map.Entry pair = (Map.Entry)it.next();
23+
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
24+
}
25+
26+
driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
27+
28+
System.out.println("Hi From testCalcOne");
29+
String sessionId = driver.getSessionId().toString();
30+
31+
System.out.println("Session Id: " + sessionId);
32+
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"server": "hub-cloud.browserstack.com",
3+
"user": "darshilgada1",
4+
"key": "fP6DdHuqEjCUEmnvJmz7",
5+
6+
"capabilities": {
7+
"project": "Test Junit5",
8+
"build": "Test junit-browserstack",
9+
"name": "local_appium_test",
10+
"browserstack.debug": true,
11+
"browserstack.local": true,
12+
"app": "bs://2e6540b05c17bb948bd72baf3f4f5e33948ec415"
13+
},
14+
15+
"environments": [{
16+
"device": "Samsung Galaxy S7"
17+
}]
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"server": "hub-cloud.browserstack.com",
3+
"user": "darshilgada1",
4+
"key": "fP6DdHuqEjCUEmnvJmz7",
5+
6+
"capabilities": {
7+
"project": "Test Junit5",
8+
"build": "Test junit-browserstack",
9+
"name": "parallel_appium_test",
10+
"browserstack.debug": true,
11+
"app": "bs://2e6540b05c17bb948bd72baf3f4f5e33948ec415"
12+
},
13+
14+
"environments": [{
15+
"device": "Google Pixel"
16+
}, {
17+
"device": "Samsung Galaxy S7"
18+
},{
19+
"device": "Samsung Galaxy S20"
20+
}]
21+
}

0 commit comments

Comments
 (0)