Skip to content

Commit 3fbdfb5

Browse files
[Adnroid][Junit4]W3c support with parametrised base class
1 parent 8b8980a commit 3fbdfb5

File tree

12 files changed

+158
-342
lines changed

12 files changed

+158
-342
lines changed

android/junit4-examples/pom.xml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
<dependency>
2323
<groupId>junit</groupId>
2424
<artifactId>junit</artifactId>
25-
<version>4.12</version>
25+
<version>4.13.2</version>
2626
</dependency>
2727
<dependency>
2828
<groupId>commons-io</groupId>
2929
<artifactId>commons-io</artifactId>
30-
<version>1.3.2</version>
30+
<version>2.11.0</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.seleniumhq.selenium</groupId>
3434
<artifactId>selenium-java</artifactId>
35-
<version>3.141.59</version>
35+
<version>4.4.0</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>io.appium</groupId>
3939
<artifactId>java-client</artifactId>
40-
<version>7.0.0</version>
40+
<version>8.1.1</version>
4141
</dependency>
4242
<dependency>
4343
<groupId>com.browserstack</groupId>
@@ -80,8 +80,11 @@
8080
<artifactId>maven-surefire-plugin</artifactId>
8181
<configuration>
8282
<includes>
83-
<include>com/browserstack/run_first_test/FirstTest.java</include>
83+
<include>com/browserstack/FirstTest.java</include>
8484
</includes>
85+
<systemPropertyVariables>
86+
<config>run_first_test/first.conf.json</config>
87+
</systemPropertyVariables>
8588
</configuration>
8689
</plugin>
8790
</plugins>
@@ -97,8 +100,11 @@
97100
<artifactId>maven-surefire-plugin</artifactId>
98101
<configuration>
99102
<includes>
100-
<include>com/browserstack/run_local_test/LocalTest.java</include>
103+
<include>com/browserstack/LocalTest.java</include>
101104
</includes>
105+
<systemPropertyVariables>
106+
<config>run_local_test/local.conf.json</config>
107+
</systemPropertyVariables>
102108
</configuration>
103109
</plugin>
104110
</plugins>
@@ -114,8 +120,11 @@
114120
<artifactId>maven-surefire-plugin</artifactId>
115121
<configuration>
116122
<includes>
117-
<include>com/browserstack/run_parallel_test/ParallelTest.java</include>
123+
<include>com/browserstack/FirstTest.java</include>
118124
</includes>
125+
<systemPropertyVariables>
126+
<config>run_parallel_test/parallel.conf.json</config>
127+
</systemPropertyVariables>
119128
</configuration>
120129
</plugin>
121130
</plugins>
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.browserstack.run_local_test;
1+
package com.browserstack;
22

33
import com.browserstack.local.Local;
44

@@ -15,10 +15,11 @@
1515
import org.json.simple.parser.JSONParser;
1616

1717
import io.appium.java_client.android.AndroidDriver;
18-
import io.appium.java_client.android.AndroidElement;
18+
import io.appium.java_client.android.options.UiAutomator2Options;
1919

2020
import org.junit.runners.Parameterized;
21-
import org.openqa.selenium.remote.DesiredCapabilities;
21+
22+
import org.openqa.selenium.WebElement;
2223

2324
import org.junit.After;
2425
import org.junit.Before;
@@ -28,7 +29,7 @@
2829

2930
@RunWith(Parameterized.class)
3031
public class BrowserStackJUnitTest {
31-
public AndroidDriver<AndroidElement> driver;
32+
public AndroidDriver driver;
3233
private Local local;
3334

3435
private static JSONObject config;
@@ -40,62 +41,69 @@ public class BrowserStackJUnitTest {
4041
public static Iterable<? extends Object> data() throws Exception {
4142
List<Integer> taskIDs = new ArrayList<Integer>();
4243

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

47-
for (int i = 0; i < envs; i++) {
48-
taskIDs.add(i);
49+
for (int i = 0; i < envs; i++) {
50+
taskIDs.add(i);
51+
}
4952
}
50-
5153
return taskIDs;
5254
}
5355

5456
@Before
5557
public void setUp() throws Exception {
5658
JSONArray envs = (JSONArray) config.get("environments");
5759

58-
DesiredCapabilities capabilities = new DesiredCapabilities();
60+
UiAutomator2Options options = new UiAutomator2Options();
5961

6062
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskID);
6163
Iterator it = envCapabilities.entrySet().iterator();
6264
while (it.hasNext()) {
6365
Map.Entry pair = (Map.Entry) it.next();
64-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
66+
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
6567
}
6668

6769
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
6870
it = commonCapabilities.entrySet().iterator();
6971
while (it.hasNext()) {
7072
Map.Entry pair = (Map.Entry) it.next();
71-
if (capabilities.getCapability(pair.getKey().toString()) == null) {
72-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
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);
7379
}
7480
}
7581

82+
JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options");
83+
7684
String username = System.getenv("BROWSERSTACK_USERNAME");
7785
if (username == null) {
78-
username = (String) config.get("username");
86+
username = (String) browserstackOptions.get("userName");
7987
}
8088

8189
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
8290
if (accessKey == null) {
83-
accessKey = (String) config.get("access_key");
91+
accessKey = (String) browserstackOptions.get("accessKey");
8492
}
8593

8694
String app = System.getenv("BROWSERSTACK_APP_ID");
8795
if (app != null && !app.isEmpty()) {
88-
capabilities.setCapability("app", app);
96+
options.setCapability("app", app);
8997
}
9098

91-
if (capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true") {
99+
if (browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true") {
92100
local = new Local();
93-
Map<String, String> options = new HashMap<String, String>();
94-
options.put("key", accessKey);
95-
local.start(options);
101+
Map<String, String> LocalOptions = new HashMap<String, String>();
102+
LocalOptions.put("key", accessKey);
103+
local.start(LocalOptions);
96104
}
97105

98-
driver = new AndroidDriver(new URL("http://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities);
106+
driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options);
99107
}
100108

101109
@After
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.browserstack;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import java.util.List;
7+
import java.time.Duration;
8+
9+
import io.appium.java_client.AppiumBy;
10+
11+
import org.openqa.selenium.support.ui.WebDriverWait;
12+
import org.openqa.selenium.support.ui.ExpectedConditions;
13+
import org.openqa.selenium.WebElement;
14+
15+
public class FirstTest extends BrowserStackJUnitTest {
16+
17+
@Test
18+
public void test() throws Exception {
19+
WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
20+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Search Wikipedia")));
21+
searchElement.click();
22+
WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
23+
ExpectedConditions.elementToBeClickable(AppiumBy.id("org.wikipedia.alpha:id/search_src_text")));
24+
insertTextElement.sendKeys("BrowserStack");
25+
Thread.sleep(5000);
26+
27+
List<WebElement> allProductsName = driver.findElements(AppiumBy.className("android.widget.TextView"));
28+
assertTrue(allProductsName.size() > 0);
29+
}
30+
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package com.browserstack.run_local_test;
1+
package com.browserstack;
22

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

55
import java.io.File;
6+
import java.time.Duration;
67
import org.junit.Test;
78
import java.util.List;
89
import org.apache.commons.io.FileUtils;
910

10-
import io.appium.java_client.MobileBy;
11-
import io.appium.java_client.android.AndroidElement;
11+
import io.appium.java_client.AppiumBy;
1212

13+
import org.openqa.selenium.WebElement;
1314
import org.openqa.selenium.OutputType;
1415
import org.openqa.selenium.TakesScreenshot;
1516
import org.openqa.selenium.support.ui.WebDriverWait;
@@ -19,16 +20,16 @@ public class LocalTest extends BrowserStackJUnitTest {
1920

2021
@Test
2122
public void test() throws Exception {
22-
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
23-
ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action")));
23+
WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
24+
ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action")));
2425
searchElement.click();
25-
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
26-
ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView")));
26+
WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
27+
ExpectedConditions.elementToBeClickable(AppiumBy.className("android.widget.TextView")));
2728

28-
AndroidElement testElement = null;
29-
List<AndroidElement> allTextViewElements = driver.findElementsByClassName("android.widget.TextView");
29+
WebElement testElement = null;
30+
List<WebElement> allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView"));
3031
Thread.sleep(10);
31-
for(AndroidElement textElement : allTextViewElements) {
32+
for(WebElement textElement : allTextViewElements) {
3233
if(textElement.getText().contains("The active connection is")) {
3334
testElement = textElement;
3435
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.browserstack.run_parallel_test;
1+
package com.browserstack;
22

33
import org.junit.runners.Parameterized;
44
import org.junit.runners.model.RunnerScheduler;

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

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

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

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

0 commit comments

Comments
 (0)