Skip to content

Commit be9b773

Browse files
[iOS][Junit5] Add w3c support with parametrized base class
1 parent 6c2fa6a commit be9b773

File tree

12 files changed

+147
-330
lines changed

12 files changed

+147
-330
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ This repository demonstrates how to run Appium tests in [JUnit4](http://junit.or
44

55
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
66

7+
## Based on
8+
9+
These code samples are currently based on:
10+
11+
- **Appium-Java-Client:** `8.1.1`
12+
- **Protocol:** `W3C`
13+
714
## Setup
815

916
### Requirements
@@ -61,32 +68,32 @@ Getting Started with Appium tests in Junit4 and Junit5 on BrowserStack couldn't
6168
### **Run first test :**
6269
6370
- Junit4
64-
- Switch to `run_first_test` directory under [Android Junit4 examples](android/junit4-examples) or [iOS Junit4 examples](ios/junit4-examples)
65-
- Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit)
71+
- Update `first.conf.json` file inside the `src/test/resources/com/browserstack/run_first_test` of Android Junit4 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
72+
- Run `mvn test -P first`
6673
6774
- Junit5
68-
- Switch to `run_first_test` directory under [Android Junit5 examples](android/junit5-examples) or [iOS Junit5 examples](ios/junit5-examples)
69-
- Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit)
75+
- Update `first.conf.json` file inside the `src/test/resources/com/browserstack/run_first_test` of Android Junit5 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
76+
- Run `mvn test -P first`
7077
7178
### **Speed up test execution with parallel testing :**
7279
7380
- Junit4
74-
- Switch to `run_parallel_test` directory under [Android Junit4 examples](android/junit4-examples/) or [iOS Junit4 examples](ios/junit4-examples/)
75-
- Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/parallelize-tests)
81+
- Update `parallel.conf.json` file inside the `src/test/resources/com/browserstack/run_parallel_test` of Android Junit4 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
82+
- Run `mvn test -P parallel`
7683
7784
- Junit5
78-
- Switch to `run_parallel_test` directory under [Android Junit5 examples](android/junit5-examples/) or [iOS Junit5 examples](ios/junit5-examples/)
79-
- Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/parallelize-tests)
85+
- Update `parallel.conf.json` file inside the `src/test/resources/com/browserstack/run_parallel_test` of Android Junit5 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
86+
- Run `mvn test -P parallel`
8087
8188
### **Use Local testing for apps that access resources hosted in development or testing environments :**
8289
8390
- Junit4
84-
- Switch to `run_local_test` directory under [Android Junit4 examples](android/junit4-examples/) or [iOS Junit4 examples](ios/junit4-examples/)
85-
- Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/local-testing)
91+
- Update `local.conf.json` file inside the `src/test/resources/com/browserstack/run_local_test` of Android Junit4 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
92+
- Run `mvn test -P local`
8693
8794
- Junit5
88-
- Switch to `run_local_test` directory under [Android Junit5 examples](android/junit5-examples/) or [iOS Junit5 examples](ios/junit5-examples/)
89-
- Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/junit/local-testing)
95+
- Update `local.conf.json` file inside the `src/test/resources/com/browserstack/run_local_test` of Android Junit5 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
96+
- Run `mvn test -P local`
9097
9198
**Note**: If you are facing any issues, refer [Getting Help section](#Getting-Help)
9299

ios/junit4-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
"platformName": "ios"
2222
}
2323
]
24-
}
24+
}

ios/junit5-examples/pom.xml

Lines changed: 21 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>
@@ -110,9 +113,18 @@
110113
<groupId>org.apache.maven.plugins</groupId>
111114
<artifactId>maven-surefire-plugin</artifactId>
112115
<configuration>
116+
<parallel>
117+
classes
118+
</parallel>
119+
<threadCount>
120+
2
121+
</threadCount>
113122
<includes>
114-
<include>ParallelTest.java</include>
123+
<include>FirstTest.java</include>
115124
</includes>
125+
<systemPropertyVariables>
126+
<config>run_parallel_test/parallel.conf.json</config>
127+
</systemPropertyVariables>
116128
</configuration>
117129
</plugin>
118130
</plugins>
@@ -130,6 +142,9 @@
130142
<includes>
131143
<include>LocalTest.java</include>
132144
</includes>
145+
<systemPropertyVariables>
146+
<config>run_local_test/local.conf.json</config>
147+
</systemPropertyVariables>
133148
</configuration>
134149
</plugin>
135150
</plugins>
Lines changed: 28 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.ios.IOSDriver;
5-
import io.appium.java_client.ios.IOSElement;
5+
import io.appium.java_client.ios.options.XCUITestOptions;
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,24 +23,26 @@
2423
@Execution(ExecutionMode.CONCURRENT)
2524
public class BrowserStackJUnitTest {
2625

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

3332
public static JSONObject config;
3433

3534
private static Stream<Integer> devices() throws IOException, ParseException {
3635

3736
List<Integer> taskIDs = new ArrayList<Integer>();
3837

39-
JSONParser parser = new JSONParser();
40-
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json"));
41-
int envs = ((JSONArray) config.get("environments")).size();
38+
if (System.getProperty("config") != null) {
39+
JSONParser parser = new JSONParser();
40+
config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/" + System.getProperty("config")));
41+
int envs = ((JSONArray) config.get("environments")).size();
4242

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

4748
return taskIDs.stream();
@@ -54,45 +55,50 @@ public void createConnection(int taskId) throws MalformedURLException {
5455
Iterator it = envCapabilities.entrySet().iterator();
5556
while (it.hasNext()) {
5657
Map.Entry pair = (Map.Entry) it.next();
57-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
58+
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
5859
}
5960

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

6364
@BeforeEach
6465
public void setup() throws Exception {
65-
capabilities = new DesiredCapabilities();
66+
options = new XCUITestOptions();
6667

6768
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
6869
Iterator it = commonCapabilities.entrySet().iterator();
6970
while (it.hasNext()) {
7071
Map.Entry pair = (Map.Entry) it.next();
71-
if (capabilities.getCapability(pair.getKey().toString()) == null) {
72-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
72+
if (options.getCapability(pair.getKey().toString()) == null) {
73+
options.setCapability(pair.getKey().toString(), pair.getValue());
74+
}else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")){
75+
HashMap bstackOptionsMap = (HashMap) pair.getValue();
76+
bstackOptionsMap.putAll((HashMap) options.getCapability("bstack:options"));
77+
options.setCapability(pair.getKey().toString(), bstackOptionsMap);
7378
}
7479
}
7580

81+
JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options");
7682
username = System.getenv("BROWSERSTACK_USERNAME");
7783
if (username == null) {
78-
username = (String) config.get("username");
84+
username = (String) browserstackOptions.get("userName");
7985
}
8086

8187
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
8288
if (accessKey == null) {
83-
accessKey = (String) config.get("access_key");
89+
accessKey = (String) browserstackOptions.get("accessKey");
8490
}
8591

8692
String app = System.getenv("BROWSERSTACK_APP_ID");
8793
if (app != null && !app.isEmpty()) {
88-
capabilities.setCapability("app", app);
94+
options.setCapability("app", app);
8995
}
9096

91-
if (capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true") {
97+
if (browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true") {
9298
local = new Local();
93-
Map<String, String> options = new HashMap<String, String>();
94-
options.put("key", accessKey);
95-
local.start(options);
99+
Map<String, String> LocalOptions = new HashMap<String, String>();
100+
LocalOptions.put("key", accessKey);
101+
local.start(LocalOptions);
96102
}
97103
}
98104

ios/junit5-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java renamed to ios/junit5-examples/src/test/java/com/browserstack/FirstTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
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.ios.IOSElement;
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;
9+
10+
import java.time.Duration;
911

1012
import java.io.IOException;
1113

@@ -19,17 +21,17 @@ void testCalcOne(int taskId) throws IOException, InterruptedException {
1921

2022
createConnection(taskId);
2123

22-
IOSElement textButton = (IOSElement) new WebDriverWait(driver, 30).until(
23-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Button")));
24+
WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
25+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button")));
2426
textButton.click();
25-
IOSElement textInput = (IOSElement) new WebDriverWait(driver, 30).until(
26-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Input")));
27+
WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
28+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input")));
2729
textInput.sendKeys("[email protected]"+"\n");
2830

2931
Thread.sleep(5000);
3032

31-
IOSElement textOutput = (IOSElement) new WebDriverWait(driver, 30).until(
32-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Output")));
33+
WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
34+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output")));
3335

3436
assertEquals(textOutput.getText(),"[email protected]");
3537
}

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

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

3-
import io.appium.java_client.MobileBy;
4-
import io.appium.java_client.ios.IOSElement;
3+
import io.appium.java_client.AppiumBy;
54
import org.apache.commons.io.FileUtils;
65
import org.junit.jupiter.params.ParameterizedTest;
76
import org.junit.jupiter.params.provider.MethodSource;
@@ -11,6 +10,9 @@
1110
import org.openqa.selenium.support.ui.ExpectedCondition;
1211
import org.openqa.selenium.support.ui.ExpectedConditions;
1312
import org.openqa.selenium.support.ui.WebDriverWait;
13+
import org.openqa.selenium.WebElement;
14+
15+
import java.time.Duration;
1416

1517
import java.io.File;
1618
import java.io.IOException;
@@ -25,19 +27,19 @@ void testCalcOne(int taskId) throws IOException {
2527

2628
createConnection(taskId);
2729

28-
IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until(
29-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal")));
30+
WebElement testButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
31+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("TestBrowserStackLocal")));
3032
testButton.click();
3133

32-
WebDriverWait wait = new WebDriverWait(driver, 30);
34+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
3335
wait.until(new ExpectedCondition<Boolean>() {
3436
@Override
3537
public Boolean apply(WebDriver d) {
36-
String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value");
38+
String result = d.findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal")).getAttribute("value");
3739
return result != null && result.length() > 0;
3840
}
3941
});
40-
IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal"));
42+
WebElement resultElement = (WebElement) driver.findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal"));
4143

4244
String resultString = resultElement.getText().toLowerCase();
4345
System.out.println(resultString);

0 commit comments

Comments
 (0)