-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathBrowserStackSampleLocal.java
More file actions
108 lines (88 loc) · 3.8 KB
/
BrowserStackSampleLocal.java
File metadata and controls
108 lines (88 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package android;
import com.browserstack.local.Local;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.net.URL;
import java.time.Duration;
import java.util.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.*;
public class BrowserStackSampleLocal {
private static Local localInstance;
public static String userName = "YOUR_USERNAME";
public static String accessKey = "YOUR_ACCESS_KEY";
public static void setupLocal() throws Exception {
localInstance = new Local();
HashMap<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
options.put("local", "true");
localInstance.start(options);
}
public static void tearDownLocal() throws Exception {
localInstance.stop();
}
public static void main(String[] args) throws Exception {
// Start the BrowserStack Local binary
setupLocal();
UiAutomator2Options options = new UiAutomator2Options();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
// Set your access credentials
browserstackOptions.put("userName", userName);
browserstackOptions.put("accessKey", accessKey);
// Set other BrowserStack capabilities
browserstackOptions.put("appiumVersion", "1.22.0");
browserstackOptions.put("projectName", "First Java Project");
browserstackOptions.put("buildName", "browserstack-build-1");
browserstackOptions.put("sessionName", "BStack local_test");
// Set the browserstack.local capability to true
browserstackOptions.put("local", "true");
// Passing browserstack capabilities inside bstack:options
options.setCapability("bstack:options", browserstackOptions);
// Set URL of the application under test
options.setCapability("app", "bs://<app-id>");
// Specify device and os_version for testing
options.setCapability("deviceName", "Google Pixel 3");
options.setCapability("platformName", "android");
options.setCapability("platformVersion", "9.0");
// Initialise the remote Webdriver using BrowserStack remote URL
// and desired capabilities defined above
AndroidDriver driver = new AndroidDriver(
new URL("http://hub.browserstack.com/wd/hub"),
options);
// Test case for the BrowserStack sample Android Local app.
// If you have uploaded your app, update the test case here.
WebElement searchElement = new WebDriverWait(driver, Duration.ofSeconds(30))
.until(
ExpectedConditions.elementToBeClickable(
AppiumBy.id("com.example.android.basicnetworking:id/test_action")));
searchElement.click();
WebElement insertTextElement = (WebElement) new WebDriverWait(
driver,
Duration.ofSeconds(30))
.until(
ExpectedConditions.elementToBeClickable(
AppiumBy.className("android.widget.TextView")));
WebElement testElement = null;
List<WebElement> allTextViewElements = driver.findElements(
AppiumBy.className("android.widget.TextView"));
Thread.sleep(10);
for (WebElement textElement : allTextViewElements) {
if (textElement.getText().contains("The active connection is")) {
testElement = textElement;
}
}
if (testElement == null) {
throw new Error("Cannot find the needed TextView element from app");
}
String matchedString = testElement.getText();
System.out.println(matchedString);
assert (matchedString.contains("The active connection is wifi"));
assert (matchedString.contains("Up and running"));
// Invoke driver.quit() after the test is done to indicate that the test is
// completed.
driver.quit();
// Stop the BrowserStack Local binary
tearDownLocal();
}
}