Skip to content

Commit 6c2fa6a

Browse files
[iOS][Junit4] w3c support with parametrized base class
1 parent 9c2e52a commit 6c2fa6a

File tree

12 files changed

+162
-379
lines changed

12 files changed

+162
-379
lines changed

ios/junit4-examples/pom.xml

Lines changed: 22 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>
@@ -113,9 +119,18 @@
113119
<groupId>org.apache.maven.plugins</groupId>
114120
<artifactId>maven-surefire-plugin</artifactId>
115121
<configuration>
122+
<parallel>
123+
classes
124+
</parallel>
125+
<threadCount>
126+
2
127+
</threadCount>
116128
<includes>
117-
<include>com/browserstack/run_parallel_test/ParallelTest.java</include>
129+
<include>com/browserstack/FirstTest.java</include>
118130
</includes>
131+
<systemPropertyVariables>
132+
<config>run_parallel_test/parallel.conf.json</config>
133+
</systemPropertyVariables>
119134
</configuration>
120135
</plugin>
121136
</plugins>
Lines changed: 28 additions & 21 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

@@ -11,13 +11,12 @@
1111
import java.io.FileReader;
1212

1313
import io.appium.java_client.ios.IOSDriver;
14-
import io.appium.java_client.ios.IOSElement;
14+
import io.appium.java_client.ios.options.XCUITestOptions;
1515
import org.json.simple.JSONArray;
1616
import org.json.simple.JSONObject;
1717
import org.json.simple.parser.JSONParser;
1818

1919
import org.junit.runners.Parameterized;
20-
import org.openqa.selenium.remote.DesiredCapabilities;
2120

2221
import org.junit.After;
2322
import org.junit.Before;
@@ -27,7 +26,7 @@
2726

2827
@RunWith(Parameterized.class)
2928
public class BrowserStackJUnitTest {
30-
public IOSDriver<IOSElement> driver;
29+
public IOSDriver driver;
3130
private Local local;
3231

3332
private static JSONObject config;
@@ -39,12 +38,14 @@ public class BrowserStackJUnitTest {
3938
public static Iterable<? extends Object> data() throws Exception {
4039
List<Integer> taskIDs = new ArrayList<Integer>();
4140

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

46-
for (int i = 0; i < envs; i++) {
47-
taskIDs.add(i);
46+
for (int i = 0; i < envs; i++) {
47+
taskIDs.add(i);
48+
}
4849
}
4950

5051
return taskIDs;
@@ -54,47 +55,53 @@ public static Iterable<? extends Object> data() throws Exception {
5455
public void setUp() throws Exception {
5556
JSONArray envs = (JSONArray) config.get("environments");
5657

57-
DesiredCapabilities capabilities = new DesiredCapabilities();
58+
XCUITestOptions options = new XCUITestOptions();
5859

5960
Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskID);
6061
Iterator it = envCapabilities.entrySet().iterator();
6162
while (it.hasNext()) {
6263
Map.Entry pair = (Map.Entry) it.next();
63-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
64+
options.setCapability(pair.getKey().toString(), pair.getValue().toString());
6465
}
6566

6667
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
6768
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
String username = System.getenv("BROWSERSTACK_USERNAME");
7683
if (username == null) {
77-
username = (String) config.get("username");
84+
username = (String) browserstackOptions.get("userName");
7885
}
7986

8087
String 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

97-
driver = new IOSDriver(new URL("http://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities);
104+
driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options);
98105
}
99106

100107
@After
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.browserstack;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
import io.appium.java_client.AppiumBy;
8+
9+
import org.openqa.selenium.support.ui.WebDriverWait;
10+
import org.openqa.selenium.support.ui.ExpectedConditions;
11+
import org.openqa.selenium.WebElement;
12+
13+
import java.time.Duration;
14+
15+
public class FirstTest extends BrowserStackJUnitTest {
16+
17+
@Test
18+
public void test() throws Exception {
19+
WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
20+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button")));
21+
textButton.click();
22+
WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
23+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input")));
24+
textInput.sendKeys("[email protected]" + "\n");
25+
26+
Thread.sleep(5000);
27+
28+
WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
29+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output")));
30+
31+
assertEquals(textOutput.getText(),"[email protected]");
32+
}
33+
}

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

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

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

55
import java.io.File;
66

7-
import io.appium.java_client.ios.IOSElement;
87
import org.junit.Test;
98
import org.apache.commons.io.FileUtils;
109

11-
import io.appium.java_client.MobileBy;
10+
import io.appium.java_client.AppiumBy;
1211

1312
import org.openqa.selenium.OutputType;
1413
import org.openqa.selenium.TakesScreenshot;
1514
import org.openqa.selenium.WebDriver;
1615
import org.openqa.selenium.support.ui.ExpectedCondition;
1716
import org.openqa.selenium.support.ui.WebDriverWait;
1817
import org.openqa.selenium.support.ui.ExpectedConditions;
18+
import org.openqa.selenium.WebElement;
19+
20+
import java.time.Duration;
1921

2022
public class LocalTest extends BrowserStackJUnitTest {
2123

2224
@Test
2325
public void test() throws Exception {
24-
IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until(
25-
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal")));
26+
WebElement testButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until(
27+
ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("TestBrowserStackLocal")));
2628
testButton.click();
2729

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

3840
String resultString = resultElement.getText().toLowerCase();
3941
System.out.println(resultString);

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

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

0 commit comments

Comments
 (0)