Skip to content

Commit c35c588

Browse files
[Android][Junit5] Add w3c support with parametrised base class
1 parent 3fbdfb5 commit c35c588

File tree

10 files changed

+135
-326
lines changed

10 files changed

+135
-326
lines changed

android/junit5-examples/pom.xml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@
2222
<dependency>
2323
<groupId>org.junit.jupiter</groupId>
2424
<artifactId>junit-jupiter-engine</artifactId>
25-
<version>5.5.2</version>
25+
<version>5.9.0</version>
2626
<scope>test</scope>
2727
</dependency>
2828
<dependency>
2929
<groupId>org.junit.jupiter</groupId>
3030
<artifactId>junit-jupiter-params</artifactId>
31-
<version>5.5.2</version>
31+
<version>5.9.0</version>
3232
<scope>test</scope>
3333
</dependency>
3434
<dependency>
3535
<groupId>commons-io</groupId>
3636
<artifactId>commons-io</artifactId>
37-
<version>1.3.2</version>
37+
<version>2.11.0</version>
3838
</dependency>
3939
<dependency>
4040
<groupId>org.seleniumhq.selenium</groupId>
4141
<artifactId>selenium-java</artifactId>
42-
<version>3.141.59</version>
42+
<version>4.4.0</version>
4343
</dependency>
4444
<dependency>
4545
<groupId>io.appium</groupId>
4646
<artifactId>java-client</artifactId>
47-
<version>7.0.0</version>
47+
<version>8.1.1</version>
4848
</dependency>
4949
<dependency>
5050
<groupId>com.browserstack</groupId>
@@ -96,6 +96,9 @@
9696
<includes>
9797
<include>FirstTest.java</include>
9898
</includes>
99+
<systemPropertyVariables>
100+
<config>run_first_test/first.conf.json</config>
101+
</systemPropertyVariables>
99102
</configuration>
100103
</plugin>
101104
</plugins>
@@ -111,8 +114,11 @@
111114
<artifactId>maven-surefire-plugin</artifactId>
112115
<configuration>
113116
<includes>
114-
<include>ParallelTest.java</include>
117+
<include>FirstTest.java</include>
115118
</includes>
119+
<systemPropertyVariables>
120+
<config>run_parallel_test/parallel.conf.json</config>
121+
</systemPropertyVariables>
116122
</configuration>
117123
</plugin>
118124
</plugins>
@@ -130,6 +136,9 @@
130136
<includes>
131137
<include>LocalTest.java</include>
132138
</includes>
139+
<systemPropertyVariables>
140+
<config>run_local_test/local.conf.json</config>
141+
</systemPropertyVariables>
133142
</configuration>
134143
</plugin>
135144
</plugins>
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package com.browserstack.run_local_test;
1+
package com.browserstack;
22

33
import com.browserstack.local.Local;
44
import io.appium.java_client.android.AndroidDriver;
5-
import io.appium.java_client.android.AndroidElement;
5+
import io.appium.java_client.android.options.UiAutomator2Options;
66
import org.json.simple.JSONArray;
77
import org.json.simple.JSONObject;
88
import org.json.simple.parser.JSONParser;
@@ -12,7 +12,6 @@
1212

1313
import org.junit.jupiter.api.parallel.Execution;
1414
import org.junit.jupiter.api.parallel.ExecutionMode;
15-
import org.openqa.selenium.remote.DesiredCapabilities;
1615

1716
import java.io.FileReader;
1817
import java.io.IOException;
@@ -24,23 +23,25 @@
2423
@Execution(ExecutionMode.CONCURRENT)
2524
public class BrowserStackJUnitTest {
2625

27-
public AndroidDriver<AndroidElement> driver;
26+
public AndroidDriver driver;
2827
private Local local;
2928
public String username;
3029
public String accessKey;
31-
public DesiredCapabilities capabilities;
30+
public UiAutomator2Options options;
3231

3332
public static JSONObject config;
3433

3534
private static Stream<Integer> devices() throws IOException, ParseException {
3635
List<Integer> taskIDs = new ArrayList<Integer>();
3736

38-
JSONParser parser = new JSONParser();
39-
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json"));
40-
int envs = ((JSONArray) config.get("environments")).size();
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();
4141

42-
for (int i = 0; i < envs; i++) {
43-
taskIDs.add(i);
42+
for (int i = 0; i < envs; i++) {
43+
taskIDs.add(i);
44+
}
4445
}
4546

4647
return taskIDs.stream();
@@ -53,45 +54,51 @@ public void createConnection(int taskId) throws MalformedURLException {
5354
Iterator it = envCapabilities.entrySet().iterator();
5455
while (it.hasNext()) {
5556
Map.Entry pair = (Map.Entry)it.next();
56-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
57+
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
5758
}
5859

59-
driver = new AndroidDriver(new URL("http://" + username + ":" + accessKey + "@" + config.get("server")+"/wd/hub"), capabilities);
60+
driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options);
6061
}
6162

6263
@BeforeEach
6364
public void setup() throws Exception {
64-
capabilities = new DesiredCapabilities();
65+
options = new UiAutomator2Options();
6566

6667
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
6768
Iterator it = commonCapabilities.entrySet().iterator();
6869
while (it.hasNext()) {
6970
Map.Entry pair = (Map.Entry)it.next();
70-
if(capabilities.getCapability(pair.getKey().toString()) == null){
71-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
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);
7277
}
7378
}
7479

80+
JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options");
81+
7582
username = System.getenv("BROWSERSTACK_USERNAME");
7683
if(username == null) {
77-
username = (String) config.get("username");
84+
username = (String) browserstackOptions.get("userName");
7885
}
7986

8087
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
8188
if(accessKey == null) {
82-
accessKey = (String) config.get("access_key");
89+
accessKey = (String) browserstackOptions.get("accessKey");
8390
}
8491

8592
String app = System.getenv("BROWSERSTACK_APP_ID");
8693
if(app != null && !app.isEmpty()) {
87-
capabilities.setCapability("app", app);
94+
options.setCapability("app", app);
8895
}
8996

90-
if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
97+
if(browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true"){
9198
local = new Local();
92-
Map<String, String> options = new HashMap<String, String>();
93-
options.put("key", accessKey);
94-
local.start(options);
99+
Map<String, String> LocalOptions = new HashMap<String, String>();
100+
LocalOptions.put("key", accessKey);
101+
local.start(LocalOptions);
95102
}
96103
}
97104

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package com.browserstack.run_first_test;
1+
package com.browserstack;
22

3-
import io.appium.java_client.MobileBy;
4-
import io.appium.java_client.android.AndroidElement;
3+
import io.appium.java_client.AppiumBy;
54
import org.junit.jupiter.params.ParameterizedTest;
65
import org.junit.jupiter.params.provider.MethodSource;
76
import org.openqa.selenium.support.ui.ExpectedConditions;
87
import org.openqa.selenium.support.ui.WebDriverWait;
8+
import org.openqa.selenium.WebElement;
99

1010
import java.io.IOException;
1111
import java.util.List;
12+
import java.time.Duration;
1213

1314
import static org.junit.jupiter.api.Assertions.assertTrue;
1415

@@ -20,17 +21,17 @@ void test(int taskId) throws IOException, InterruptedException {
2021

2122
createConnection(taskId);
2223

23-
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
24-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));
24+
WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
25+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Search Wikipedia")));
2526
searchElement.click();
2627

27-
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
28-
ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text")));
28+
WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
29+
ExpectedConditions.elementToBeClickable(AppiumBy.id("org.wikipedia.alpha:id/search_src_text")));
2930
insertTextElement.sendKeys("BrowserStack");
3031

3132
Thread.sleep(5000);
3233

33-
List<AndroidElement> allProductsName = driver.findElementsByClassName("android.widget.TextView");
34+
List<WebElement> allProductsName = driver.findElements(AppiumBy.className("android.widget.TextView"));
3435
assertTrue(allProductsName.size() > 0);
3536
}
3637
}

android/junit5-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java renamed to android/junit5-examples/src/test/java/com/browserstack/LocalTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
package com.browserstack.run_local_test;
1+
package com.browserstack;
2+
3+
import io.appium.java_client.AppiumBy;
24

3-
import io.appium.java_client.MobileBy;
4-
import io.appium.java_client.android.AndroidElement;
55
import org.apache.commons.io.FileUtils;
66
import org.junit.jupiter.params.ParameterizedTest;
77
import org.junit.jupiter.params.provider.MethodSource;
88
import org.openqa.selenium.OutputType;
99
import org.openqa.selenium.TakesScreenshot;
1010
import org.openqa.selenium.support.ui.ExpectedConditions;
1111
import org.openqa.selenium.support.ui.WebDriverWait;
12+
import org.openqa.selenium.WebElement;
1213

1314
import java.io.File;
1415
import java.io.IOException;
1516
import java.util.List;
17+
import java.time.Duration;
1618

1719
import static org.junit.jupiter.api.Assertions.assertTrue;
1820

@@ -24,16 +26,16 @@ void test(int taskId) throws IOException, InterruptedException {
2426

2527
createConnection(taskId);
2628

27-
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
28-
ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action")));
29+
WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
30+
ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action")));
2931
searchElement.click();
30-
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
31-
ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView")));
32+
WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
33+
ExpectedConditions.elementToBeClickable(AppiumBy.className("android.widget.TextView")));
3234

33-
AndroidElement testElement = null;
34-
List<AndroidElement> allTextViewElements = driver.findElementsByClassName("android.widget.TextView");
35+
WebElement testElement = null;
36+
List<WebElement> allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView"));
3537
Thread.sleep(10);
36-
for(AndroidElement textElement : allTextViewElements) {
38+
for(WebElement textElement : allTextViewElements) {
3739
if(textElement.getText().contains("The active connection is")) {
3840
testElement = textElement;
3941
}

android/junit5-examples/src/test/java/com/browserstack/run_first_test/BrowserStackJUnitTest.java

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)