Skip to content

Commit b1836e0

Browse files
kamal-kaur04francisf
authored andcommitted
add: sdk samples for JUnit 4, 5
1 parent 6c7ad37 commit b1836e0

File tree

19 files changed

+309
-370
lines changed

19 files changed

+309
-370
lines changed

android/LocalSample.apk

3.84 MB
Binary file not shown.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and acccessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: BROWSERSTACK_USERNAME
7+
accessKey: BROWSERSTACK_ACCESS_KEY
8+
9+
# ======================
10+
# BrowserStack Reporting
11+
# ======================
12+
# The following capabilities are used to set up reporting on BrowserStack:
13+
# Set 'projectName' to the name of your project. Example, Marketing Website
14+
projectName: BrowserStack Samples
15+
# Set `buildName` as the name of the job / testsuite being run
16+
buildName: browserstack build
17+
# `buildIdentifier` is a unique id to differentiate every execution that gets appended to
18+
# buildName. Choose your buildIdentifier format from the available expressions:
19+
# ${BUILD_NUMBER} (Default): Generates an incremental counter with every execution
20+
# ${DATE_TIME}: Generates a Timestamp with every execution. Eg. 05-Nov-19:30
21+
# Read more about buildIdentifiers here -> https://www.browserstack.com/docs/automate/selenium/organize-tests
22+
buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${expression}
23+
# Set `framework` of your test suite. Example, `testng`, `cucumber`, `cucumber-testng`
24+
# This property is needed to send test context to BrowserStack (test name, status)
25+
framework: junit5
26+
27+
source: junit5:appium-sample-sdk:v1.0
28+
29+
app: ../WikipediaSample.apk
30+
#app: ../LocalSample.apk #For running local tests
31+
32+
# =======================================
33+
# Platforms (Browsers / Devices to test)
34+
# =======================================
35+
# Platforms object contains all the browser / device combinations you want to test on.
36+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
37+
38+
platforms:
39+
- deviceName: Samsung Galaxy S22 Ultra
40+
osVersion: 12.0
41+
platformName: android
42+
- deviceName: Samsung Galaxy S21
43+
osVersion: 11.0
44+
platformName: android
45+
- deviceName: Google Pixel 6 Pro
46+
osVersion: 12.0
47+
platformName: android
48+
49+
# =======================
50+
# Parallels per Platform
51+
# =======================
52+
# The number of parallel threads to be used for each platform set.
53+
# BrowserStack's SDK runner will select the best strategy based on the configured value
54+
#
55+
# 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
56+
#
57+
# 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
58+
parallelsPerPlatform: 2
59+
60+
# ==========================================
61+
# BrowserStack Local
62+
# (For localhost, staging/private websites)
63+
# ==========================================
64+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
65+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
66+
browserstackLocal: true # <boolean> (Default false)
67+
#browserStackLocalOptions:
68+
#Options to be passed to BrowserStack local in-case of advanced configurations
69+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
70+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
71+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
72+
73+
# ===================
74+
# Debugging features
75+
# ===================
76+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
77+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
78+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
79+
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)

android/junit5-examples/pom.xml

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212

1313
<properties>
1414
<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>
15+
<surefire.version>3.0.0-M5</surefire.version>
16+
<config.file>com/browserstack/FirstTest.java</config.file>
1917
</properties>
2018

2119
<dependencies>
2220
<dependency>
2321
<groupId>org.junit.jupiter</groupId>
24-
<artifactId>junit-jupiter-engine</artifactId>
22+
<artifactId>junit-jupiter-api</artifactId>
2523
<version>5.9.0</version>
2624
<scope>test</scope>
2725
</dependency>
2826
<dependency>
2927
<groupId>org.junit.jupiter</groupId>
30-
<artifactId>junit-jupiter-params</artifactId>
28+
<artifactId>junit-jupiter-engine</artifactId>
3129
<version>5.9.0</version>
3230
<scope>test</scope>
3331
</dependency>
@@ -46,30 +44,44 @@
4644
<artifactId>java-client</artifactId>
4745
<version>8.1.1</version>
4846
</dependency>
49-
<dependency>
50-
<groupId>com.browserstack</groupId>
51-
<artifactId>browserstack-local-java</artifactId>
52-
<version>1.0.3</version>
53-
</dependency>
5447
<dependency>
5548
<groupId>com.googlecode.json-simple</groupId>
5649
<artifactId>json-simple</artifactId>
5750
<version>1.1.1</version>
5851
</dependency>
52+
<dependency>
53+
<groupId>com.browserstack</groupId>
54+
<artifactId>browserstack-java-sdk</artifactId>
55+
<version>LATEST</version>
56+
<scope>compile</scope>
57+
</dependency>
5958
</dependencies>
6059

6160
<build>
6261
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-dependency-plugin</artifactId>
65+
<executions>
66+
<execution>
67+
<id>getClasspathFilenames</id>
68+
<goals>
69+
<goal>properties</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
6374
<plugin>
6475
<groupId>org.apache.maven.plugins</groupId>
6576
<artifactId>maven-surefire-plugin</artifactId>
66-
<version>2.22.2</version>
77+
<version>${surefire.version}</version>
6778
<configuration>
68-
<properties>
69-
<configurationParameters>
70-
junit.jupiter.execution.parallel.enabled=true
71-
</configurationParameters>
72-
</properties>
79+
<includes>
80+
<include>${config.file}</include>
81+
</includes>
82+
<argLine>
83+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
84+
</argLine>
7385
</configuration>
7486
</plugin>
7587
<plugin>
@@ -86,61 +98,39 @@
8698

8799
<profiles>
88100
<profile>
89-
<id>first</id>
90-
<build>
91-
<plugins>
92-
<plugin>
93-
<groupId>org.apache.maven.plugins</groupId>
94-
<artifactId>maven-surefire-plugin</artifactId>
95-
<configuration>
96-
<includes>
97-
<include>FirstTest.java</include>
98-
</includes>
99-
<systemPropertyVariables>
100-
<config>run_first_test/first.conf.json</config>
101-
</systemPropertyVariables>
102-
</configuration>
103-
</plugin>
104-
</plugins>
105-
</build>
106-
</profile>
107-
108-
<profile>
109-
<id>parallel</id>
101+
<id>sample-test</id>
110102
<build>
111103
<plugins>
112104
<plugin>
113105
<groupId>org.apache.maven.plugins</groupId>
114106
<artifactId>maven-surefire-plugin</artifactId>
115107
<configuration>
116-
<parallel>classes</parallel>
117-
<threadCount>2</threadCount>
118108
<includes>
119-
<include>FirstTest.java</include>
109+
<include>com/browserstack/FirstTest.java</include>
120110
</includes>
121-
<systemPropertyVariables>
122-
<config>run_parallel_test/parallel.conf.json</config>
123-
</systemPropertyVariables>
111+
<argLine>
112+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
113+
</argLine>
124114
</configuration>
125115
</plugin>
126116
</plugins>
127117
</build>
128118
</profile>
129119

130120
<profile>
131-
<id>local</id>
121+
<id>sample-local-test</id>
132122
<build>
133123
<plugins>
134124
<plugin>
135125
<groupId>org.apache.maven.plugins</groupId>
136126
<artifactId>maven-surefire-plugin</artifactId>
137127
<configuration>
138128
<includes>
139-
<include>LocalTest.java</include>
129+
<include>com/browserstack/LocalTest.java</include>
140130
</includes>
141-
<systemPropertyVariables>
142-
<config>run_local_test/local.conf.json</config>
143-
</systemPropertyVariables>
131+
<argLine>
132+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
133+
</argLine>
144134
</configuration>
145135
</plugin>
146136
</plugins>
Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,63 @@
11
package com.browserstack;
22

3-
import com.browserstack.local.Local;
43
import io.appium.java_client.android.AndroidDriver;
54
import io.appium.java_client.android.options.UiAutomator2Options;
6-
import org.json.simple.JSONArray;
75
import org.json.simple.JSONObject;
8-
import org.json.simple.parser.JSONParser;
9-
import org.json.simple.parser.ParseException;
106
import org.junit.jupiter.api.AfterEach;
117
import org.junit.jupiter.api.BeforeEach;
8+
import org.yaml.snakeyaml.Yaml;
129

13-
import org.junit.jupiter.api.parallel.Execution;
14-
import org.junit.jupiter.api.parallel.ExecutionMode;
15-
16-
import java.io.FileReader;
17-
import java.io.IOException;
18-
import java.net.MalformedURLException;
10+
import java.io.File;
11+
import java.io.InputStream;
1912
import java.net.URL;
13+
import java.nio.file.Files;
2014
import java.util.*;
21-
import java.util.stream.Stream;
2215

23-
@Execution(ExecutionMode.CONCURRENT)
2416
public class BrowserStackJUnitTest {
2517

2618
public AndroidDriver driver;
27-
private Local local;
28-
public String username;
19+
public String userName;
2920
public String accessKey;
3021
public UiAutomator2Options options;
22+
public static Map<String, Object> browserStackYamlMap;
23+
public static final String USER_DIR = "user.dir";
3124

32-
public static JSONObject config;
33-
34-
private static Stream<Integer> devices() throws IOException, ParseException {
35-
List<Integer> taskIDs = new ArrayList<Integer>();
36-
37-
if (System.getProperty("config") != null) {
38-
JSONParser parser = new JSONParser();
39-
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/" + System.getProperty("config")));
40-
int envs = ((JSONArray) config.get("environments")).size();
41-
42-
for (int i = 0; i < envs; i++) {
43-
taskIDs.add(i);
44-
}
45-
}
46-
47-
return taskIDs.stream();
48-
}
49-
50-
public void createConnection(int taskId) throws MalformedURLException {
51-
JSONArray envs = (JSONArray) config.get("environments");
52-
53-
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskId);
54-
Iterator it = envCapabilities.entrySet().iterator();
55-
while (it.hasNext()) {
56-
Map.Entry pair = (Map.Entry)it.next();
57-
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
58-
}
59-
60-
driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options);
25+
public BrowserStackJUnitTest() {
26+
File file = new File(getUserDir() + "/browserstack.yml");
27+
this.browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>());
6128
}
6229

6330
@BeforeEach
6431
public void setup() throws Exception {
6532
options = new UiAutomator2Options();
33+
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
34+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
35+
HashMap<String, Object> bStackOptions = new HashMap<>();
36+
options.setCapability("bstack:options", bStackOptions);
6637

67-
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
68-
Iterator it = commonCapabilities.entrySet().iterator();
69-
while (it.hasNext()) {
70-
Map.Entry pair = (Map.Entry)it.next();
71-
if(options.getCapability(pair.getKey().toString()) == null){
72-
options.setCapability(pair.getKey().toString(), pair.getValue());
73-
}else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")){
74-
HashMap bstackOptionsMap = (HashMap) pair.getValue();
75-
bstackOptionsMap.putAll((HashMap) options.getCapability("bstack:options"));
76-
options.setCapability(pair.getKey().toString(), bstackOptionsMap);
77-
}
78-
}
79-
80-
JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options");
81-
82-
username = System.getenv("BROWSERSTACK_USERNAME");
83-
if(username == null) {
84-
username = (String) browserstackOptions.get("userName");
85-
}
86-
87-
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
88-
if(accessKey == null) {
89-
accessKey = (String) browserstackOptions.get("accessKey");
90-
}
91-
92-
String app = System.getenv("BROWSERSTACK_APP_ID");
93-
if(app != null && !app.isEmpty()) {
94-
options.setCapability("app", app);
95-
}
96-
97-
if(browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true"){
98-
local = new Local();
99-
Map<String, String> LocalOptions = new HashMap<String, String>();
100-
LocalOptions.put("key", accessKey);
101-
local.start(LocalOptions);
102-
}
38+
driver = new AndroidDriver(new URL(String.format("https://%s:%[email protected]/wd/hub", userName , accessKey)), options);
10339
}
10440

10541
@AfterEach
10642
public void tearDown() throws Exception {
10743
// Invoke driver.quit() to indicate that the test is completed.
10844
// Otherwise, it will appear as timed out on BrowserStack.
10945
driver.quit();
110-
if(local != null) local.stop();
46+
}
47+
48+
private String getUserDir() {
49+
return System.getProperty(USER_DIR);
50+
}
51+
52+
private Map<String, Object> convertYamlFileToMap(File yamlFile, Map<String, Object> map) {
53+
try {
54+
InputStream inputStream = Files.newInputStream(yamlFile.toPath());
55+
Yaml yaml = new Yaml();
56+
Map<String, Object> config = yaml.load(inputStream);
57+
map.putAll(config);
58+
} catch (Exception e) {
59+
throw new RuntimeException(String.format("Malformed browserstack.yml file - %s.", e));
60+
}
61+
return map;
11162
}
11263
}

0 commit comments

Comments
 (0)