Skip to content

Commit 55aa28e

Browse files
kamal-kaur04francisf
authored andcommitted
add browserstack sdk to integrate JUnit Tests with browserstack
1 parent f1a90af commit 55aa28e

File tree

8 files changed

+136
-233
lines changed

8 files changed

+136
-233
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
local.log
44
.idea
55
*.iml
6+
android/junit4-examples/logs

android/WikipediaSample.apk

19.4 MB
Binary file not shown.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: YOUR_USERNAME
7+
accessKey: YOUR_ACCESS_KEY
8+
9+
# ======================
10+
# Organizing your tests
11+
# ======================
12+
# Use `projectName`, `buildName`, `name` capabilities to organize your tests
13+
# `name` is the name of your test sessions and is automatically picked from your
14+
# test name and doesn't need to be set manually when using BrowserStack SDK
15+
# `buildName` is used to name your CI/CD job or the execution of your test suite.
16+
# Ensure you add a dynamic identifier, like an incremental build number from your
17+
# CI/CD or timestamp at the end of every build; otherwise tests from different
18+
# executions will be grouped together on BrowserStack
19+
buildName: browserstack-build-1
20+
# Use `projectName` to set the name of your project. Example, Marketing Website
21+
projectName: BrowserStack Samples
22+
# Use `framework` to set the framework of your project. Example, testng, cucumber, cucumber-testng
23+
framework: junit4
24+
25+
source: junit:sample-sdk:v1.0
26+
27+
app: ../WikipediaSample.apk
28+
29+
# =======================================
30+
# Platforms (Browsers / Devices to test)
31+
# =======================================
32+
# Platforms object contains all the browser / device combinations you want to test on.
33+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
34+
platforms:
35+
- deviceName: Samsung Galaxy S22 Ultra
36+
osVersion: 12.0
37+
platformName: android
38+
- deviceName: Samsung Galaxy S21
39+
osVersion: 11.0
40+
platformName: android
41+
- deviceName: Google Pixel 6 Pro
42+
osVersion: 12.0
43+
platformName: android
44+
45+
# =======================
46+
# Parallels per Platform
47+
# =======================
48+
# The number of parallel threads to be used for each platform set.
49+
# BrowserStack's SDK runner will select the best strategy based on the configured value
50+
#
51+
# Example 1 - If you have configured 3 platforms and set `parallelsPerPlatform` as 2, a total of 6 (2 * 3) parallel threads will be used on BrowserStack
52+
#
53+
# Example 2 - If you have configured 1 platform and set `parallelsPerPlatform` as 5, a total of 5 (1 * 5) parallel threads will be used on BrowserStack
54+
parallelsPerPlatform: 1
55+
56+
# ==========================================
57+
# BrowserStack Local
58+
# (For localhost, staging/private websites)
59+
# ==========================================
60+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
61+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
62+
browserstackLocal: true # <boolean> (Default false)
63+
#browserStackLocalOptions:
64+
#Options to be passed to BrowserStack local in-case of advanced configurations
65+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
66+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
67+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
68+
69+
# ===================
70+
# Debugging features
71+
# ===================
72+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
73+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
74+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
75+
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)

android/junit4-examples/pom.xml

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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">
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.browserstack</groupId>
@@ -12,51 +12,70 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<surefire.version>2.19.1</surefire.version>
15+
<surefire.version>3.0.0-M5</surefire.version>
1616

17-
<test.file></test.file>
1817
<config.file>default</config.file>
1918
</properties>
2019

2120
<dependencies>
22-
<dependency>
23-
<groupId>junit</groupId>
24-
<artifactId>junit</artifactId>
25-
<version>4.13.2</version>
26-
</dependency>
27-
<dependency>
28-
<groupId>commons-io</groupId>
29-
<artifactId>commons-io</artifactId>
30-
<version>2.11.0</version>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.seleniumhq.selenium</groupId>
34-
<artifactId>selenium-java</artifactId>
35-
<version>4.4.0</version>
36-
</dependency>
37-
<dependency>
38-
<groupId>io.appium</groupId>
39-
<artifactId>java-client</artifactId>
40-
<version>8.1.1</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>com.browserstack</groupId>
44-
<artifactId>browserstack-local-java</artifactId>
45-
<version>1.0.3</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>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.13.2</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>commons-io</groupId>
28+
<artifactId>commons-io</artifactId>
29+
<version>2.11.0</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.seleniumhq.selenium</groupId>
33+
<artifactId>selenium-java</artifactId>
34+
<version>4.4.0</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.appium</groupId>
38+
<artifactId>java-client</artifactId>
39+
<version>8.1.1</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.googlecode.json-simple</groupId>
43+
<artifactId>json-simple</artifactId>
44+
<version>1.1.1</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.browserstack</groupId>
48+
<artifactId>browserstack-java-sdk</artifactId>
49+
<version>LATEST</version>
50+
<scope>compile</scope>
51+
</dependency>
5252
</dependencies>
5353

5454
<build>
5555
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-dependency-plugin</artifactId>
59+
<version>3.3.0</version>
60+
<executions>
61+
<execution>
62+
<id>getClasspathFilenames</id>
63+
<goals>
64+
<goal>properties</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
5669
<plugin>
5770
<groupId>org.apache.maven.plugins</groupId>
5871
<artifactId>maven-surefire-plugin</artifactId>
59-
<version>2.22.2</version>
72+
<version>${surefire.version}</version>
73+
<configuration>
74+
<includes>
75+
<include>com/browserstack/FirstTest.java</include>
76+
</includes>
77+
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
78+
</configuration>
6079
</plugin>
6180
<plugin>
6281
<groupId>org.apache.maven.plugins</groupId>
@@ -72,61 +91,37 @@
7291

7392
<profiles>
7493
<profile>
75-
<id>first</id>
94+
<id>sample-test</id>
7695
<build>
7796
<plugins>
7897
<plugin>
7998
<groupId>org.apache.maven.plugins</groupId>
8099
<artifactId>maven-surefire-plugin</artifactId>
100+
<version>${surefire.version}</version>
81101
<configuration>
82102
<includes>
83103
<include>com/browserstack/FirstTest.java</include>
84104
</includes>
85-
<systemPropertyVariables>
86-
<config>run_first_test/first.conf.json</config>
87-
</systemPropertyVariables>
105+
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
88106
</configuration>
89107
</plugin>
90108
</plugins>
91109
</build>
92110
</profile>
93111

94112
<profile>
95-
<id>local</id>
113+
<id>sample-local-test</id>
96114
<build>
97115
<plugins>
98116
<plugin>
99117
<groupId>org.apache.maven.plugins</groupId>
100118
<artifactId>maven-surefire-plugin</artifactId>
119+
<version>${surefire.version}</version>
101120
<configuration>
102121
<includes>
103122
<include>com/browserstack/LocalTest.java</include>
104123
</includes>
105-
<systemPropertyVariables>
106-
<config>run_local_test/local.conf.json</config>
107-
</systemPropertyVariables>
108-
</configuration>
109-
</plugin>
110-
</plugins>
111-
</build>
112-
</profile>
113-
114-
<profile>
115-
<id>parallel</id>
116-
<build>
117-
<plugins>
118-
<plugin>
119-
<groupId>org.apache.maven.plugins</groupId>
120-
<artifactId>maven-surefire-plugin</artifactId>
121-
<configuration>
122-
<parallel>classes</parallel>
123-
<threadCount>2</threadCount>
124-
<includes>
125-
<include>com/browserstack/FirstTest.java</include>
126-
</includes>
127-
<systemPropertyVariables>
128-
<config>run_parallel_test/parallel.conf.json</config>
129-
</systemPropertyVariables>
124+
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
130125
</configuration>
131126
</plugin>
132127
</plugins>
Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,24 @@
11
package com.browserstack;
22

3-
import com.browserstack.local.Local;
4-
53
import java.net.URL;
6-
import java.util.Map;
7-
import java.util.List;
8-
import java.util.HashMap;
9-
import java.util.Iterator;
10-
import java.util.ArrayList;
11-
import java.io.FileReader;
12-
13-
import org.json.simple.JSONArray;
14-
import org.json.simple.JSONObject;
15-
import org.json.simple.parser.JSONParser;
16-
174
import io.appium.java_client.android.AndroidDriver;
185
import io.appium.java_client.android.options.UiAutomator2Options;
19-
20-
import org.junit.runners.Parameterized;
21-
22-
import org.openqa.selenium.WebElement;
23-
246
import org.junit.After;
257
import org.junit.Before;
26-
import org.junit.runner.RunWith;
27-
import org.junit.runners.Parameterized.Parameter;
28-
import org.junit.runners.Parameterized.Parameters;
298

30-
@RunWith(Parameterized.class)
319
public class BrowserStackJUnitTest {
3210
public AndroidDriver driver;
33-
private Local local;
34-
35-
private static JSONObject config;
36-
37-
@Parameter(value = 0)
38-
public int taskID;
39-
40-
@Parameters
41-
public static Iterable<? extends Object> data() throws Exception {
42-
List<Integer> taskIDs = new ArrayList<Integer>();
43-
44-
if (System.getProperty("config") != null) {
45-
JSONParser parser = new JSONParser();
46-
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/" + System.getProperty("config")));
47-
int envs = ((JSONArray) config.get("environments")).size();
48-
49-
for (int i = 0; i < envs; i++) {
50-
taskIDs.add(i);
51-
}
52-
}
53-
return taskIDs;
54-
}
5511

5612
@Before
5713
public void setUp() throws Exception {
58-
JSONArray envs = (JSONArray) config.get("environments");
59-
6014
UiAutomator2Options options = new UiAutomator2Options();
61-
62-
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskID);
63-
Iterator it = envCapabilities.entrySet().iterator();
64-
while (it.hasNext()) {
65-
Map.Entry pair = (Map.Entry) it.next();
66-
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
67-
}
68-
69-
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
70-
it = commonCapabilities.entrySet().iterator();
71-
while (it.hasNext()) {
72-
Map.Entry pair = (Map.Entry) it.next();
73-
if (options.getCapability(pair.getKey().toString()) == null) {
74-
options.setCapability(pair.getKey().toString(), pair.getValue());
75-
}else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")){
76-
HashMap bstackOptionsMap = (HashMap) pair.getValue();
77-
bstackOptionsMap.putAll((HashMap) options.getCapability("bstack:options"));
78-
options.setCapability(pair.getKey().toString(), bstackOptionsMap);
79-
}
80-
}
81-
82-
JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options");
83-
84-
String username = System.getenv("BROWSERSTACK_USERNAME");
85-
if (username == null) {
86-
username = (String) browserstackOptions.get("userName");
87-
}
88-
89-
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
90-
if (accessKey == null) {
91-
accessKey = (String) browserstackOptions.get("accessKey");
92-
}
93-
94-
String app = System.getenv("BROWSERSTACK_APP_ID");
95-
if (app != null && !app.isEmpty()) {
96-
options.setCapability("app", app);
97-
}
98-
99-
if (browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true") {
100-
local = new Local();
101-
Map<String, String> LocalOptions = new HashMap<String, String>();
102-
LocalOptions.put("key", accessKey);
103-
local.start(LocalOptions);
104-
}
105-
106-
driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options);
15+
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), options);
10716
}
10817

10918
@After
11019
public void tearDown() throws Exception {
11120
// Invoke driver.quit() to indicate that the test is completed.
11221
// Otherwise, it will appear as timed out on BrowserStack.
11322
driver.quit();
114-
if (local != null) local.stop();
11523
}
11624
}

0 commit comments

Comments
 (0)