Skip to content

Commit 67424e5

Browse files
kamal-kaur04francisf
authored andcommitted
update: add RemoteWebdriver in Runner Class
1 parent e1dfde8 commit 67424e5

File tree

6 files changed

+109
-18
lines changed

6 files changed

+109
-18
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,30 @@ Getting Started with Appium tests in Junit4 and Junit5 on BrowserStack couldn't
6868
### **Run first test :**
6969
7070
- Junit4
71-
- Update `browserstack.yml` file at root level of Android Junit4 examples or iOS Junit4 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
71+
- Update `browserstack.yml` file at root level of [Android Junit4 examples](android/junit4-examples) or [iOS Junit4 examples](ios/junit4-examples) with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
7272
- Run `mvn test -P sample-test`
7373
7474
- Junit5
75-
- Update `browserstack.yml` file at root level of Android Junit5 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
75+
- Update `browserstack.yml` file at root level of [Android Junit5 examples](android/junit5-examples) or [iOS Junit5 examples](ios/junit5-examples) with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
7676
- Run `mvn test -P sample-test`
7777
7878
### **Use Local testing for apps that access resources hosted in development or testing environments :**
7979
8080
- Junit4
81-
- Update `browserstack.yml` file at root level of Android Junit4 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
81+
- Update `browserstack.yml` file at root level of [Android Junit4 examples](android/junit4-examples) or [iOS Junit4 examples](ios/junit4-examples) with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
82+
- Simply configure the browserstackLocal parameter in the browserstack.yml file accordingly in [Android Junit4 examples](android/junit4-examples) or [iOS Junit4 examples](ios/junit4-examples).
83+
```
84+
browserstackLocal: true
85+
```
8286
- Run `mvn test -P sample-local-test`
8387
84-
- Junit5
85-
- Update `browserstack.yml` file at root level 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 sample-local-test`
88+
- Junit5
89+
- Update `browserstack.yml` file at root level of Android Junit5 examples or iOS Junit5 examples with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
90+
- Simply configure the browserstackLocal parameter in the browserstack.yml file accordingly in [Android Junit5 examples](android/junit5-examples) or [iOS Junit5 examples](ios/junit5-examples).
91+
```
92+
browserstackLocal: true
93+
```
94+
- Run `mvn test -P sample-local-test`
8795
8896
**Note**: If you are facing any issues, refer [Getting Help section](#Getting-Help)
8997
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
package com.browserstack;
22

3+
import java.io.File;
4+
import java.io.InputStream;
35
import java.net.URL;
6+
import java.nio.file.Files;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
410
import io.appium.java_client.android.AndroidDriver;
511
import io.appium.java_client.android.options.UiAutomator2Options;
612
import org.junit.After;
713
import org.junit.Before;
14+
import org.yaml.snakeyaml.Yaml;
815

916
public class BrowserStackJUnitTest {
1017
public AndroidDriver driver;
18+
public String userName;
19+
public String accessKey;
20+
public UiAutomator2Options options;
21+
public static Map<String, Object> browserStackYamlMap;
22+
public static final String USER_DIR = "user.dir";
23+
24+
public BrowserStackJUnitTest() {
25+
File file = new File(getUserDir() + "/browserstack.yml");
26+
this.browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>());
27+
}
1128

1229
@Before
1330
public void setUp() throws Exception {
14-
UiAutomator2Options options = new UiAutomator2Options();
15-
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), options);
31+
options = new UiAutomator2Options();
32+
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
33+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
34+
options.setCapability("appium:app", "bs://sample.app");
35+
options.setCapability("appium:deviceName", "Samsung Galaxy S22 Ultra");
36+
options.setCapability("appium:platformVersion", "12.0");
37+
38+
driver = new AndroidDriver(new URL(String.format("https://%s:%[email protected]/wd/hub", userName , accessKey)), options);
1639
}
1740

1841
@After
@@ -21,4 +44,20 @@ public void tearDown() throws Exception {
2144
// Otherwise, it will appear as timed out on BrowserStack.
2245
driver.quit();
2346
}
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;
62+
}
2463
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.appium.java_client.android.AndroidDriver;
44
import io.appium.java_client.android.options.UiAutomator2Options;
5-
import org.json.simple.JSONObject;
65
import org.junit.jupiter.api.AfterEach;
76
import org.junit.jupiter.api.BeforeEach;
87
import org.yaml.snakeyaml.Yaml;
@@ -32,8 +31,9 @@ public void setup() throws Exception {
3231
options = new UiAutomator2Options();
3332
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
3433
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);
34+
options.setCapability("appium:app", "bs://sample.app");
35+
options.setCapability("appium:deviceName", "Samsung Galaxy S22 Ultra");
36+
options.setCapability("appium:platformVersion", "12.0");
3737

3838
driver = new AndroidDriver(new URL(String.format("https://%s:%[email protected]/wd/hub", userName , accessKey)), options);
3939
}

ios/junit4-examples/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@
7070
<groupId>org.apache.maven.plugins</groupId>
7171
<artifactId>maven-surefire-plugin</artifactId>
7272
<version>${surefire.version}</version>
73+
<dependencies>
74+
<dependency>
75+
<groupId>org.apache.maven.surefire</groupId>
76+
<artifactId>surefire-junit47</artifactId>
77+
<version>${surefire.version}</version>
78+
</dependency>
79+
</dependencies>
7380
<configuration>
7481
<includes>
7582
<include>com/browserstack/FirstTest.java</include>
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
package com.browserstack;
22

3+
import java.io.File;
4+
import java.io.InputStream;
35
import java.net.URL;
6+
import java.nio.file.Files;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
410
import io.appium.java_client.ios.IOSDriver;
511
import io.appium.java_client.ios.options.XCUITestOptions;
612
import org.junit.After;
713
import org.junit.Before;
14+
import org.yaml.snakeyaml.Yaml;
815

916
public class BrowserStackJUnitTest {
1017
public IOSDriver driver;
18+
public String userName;
19+
public String accessKey;
20+
public XCUITestOptions options;
21+
public static Map<String, Object> browserStackYamlMap;
22+
public static final String USER_DIR = "user.dir";
23+
24+
public BrowserStackJUnitTest() {
25+
File file = new File(getUserDir() + "/browserstack.yml");
26+
this.browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>());
27+
}
1128

1229
@Before
1330
public void setUp() throws Exception {
14-
XCUITestOptions options = new XCUITestOptions();
15-
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), options);
31+
options = new XCUITestOptions();
32+
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
33+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
34+
options.setCapability("appium:app", "bs://sample.app");
35+
options.setCapability("appium:deviceName", "iPhone 14 Pro");
36+
options.setCapability("appium:platformVersion", "16");
37+
38+
driver = new IOSDriver(new URL(String.format("https://%s:%[email protected]/wd/hub", userName , accessKey)), options);
1639
}
1740

1841
@After
@@ -21,4 +44,20 @@ public void tearDown() throws Exception {
2144
// Otherwise, it will appear as timed out on BrowserStack.
2245
driver.quit();
2346
}
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;
62+
}
2463
}

ios/junit5-examples/src/test/java/com/browserstack/BrowserStackJUnitTest.java

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

3-
import io.appium.java_client.android.AndroidDriver;
4-
import io.appium.java_client.android.options.UiAutomator2Options;
53
import io.appium.java_client.ios.IOSDriver;
64
import io.appium.java_client.ios.options.XCUITestOptions;
7-
import org.json.simple.JSONObject;
85
import org.junit.jupiter.api.AfterEach;
96
import org.junit.jupiter.api.BeforeEach;
107
import org.yaml.snakeyaml.Yaml;
@@ -34,8 +31,9 @@ public void setup() throws Exception {
3431
options = new XCUITestOptions();
3532
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
3633
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
37-
HashMap<String, Object> bStackOptions = new HashMap<>();
38-
options.setCapability("bstack:options", bStackOptions);
34+
options.setCapability("appium:app", "bs://sample.app");
35+
options.setCapability("appium:deviceName", "iPhone 14 Pro");
36+
options.setCapability("appium:platformVersion", "16");
3937

4038
driver = new IOSDriver(new URL(String.format("https://%s:%[email protected]/wd/hub", userName , accessKey)), options);
4139
}

0 commit comments

Comments
 (0)