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