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