|
1 | 1 | package ios; |
2 | 2 |
|
3 | 3 | import com.browserstack.local.Local; |
4 | | -import java.net.URL; import java.io.File; import java.util.*; |
| 4 | +import io.appium.java_client.AppiumBy; |
| 5 | +import java.io.File; |
| 6 | +import java.net.URL; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.*; |
5 | 9 | import org.apache.commons.io.FileUtils; |
6 | | -import io.appium.java_client.ios.*; |
7 | 10 | import org.openqa.selenium.*; |
8 | | -import org.openqa.selenium.support.ui.*;import org.openqa.selenium.remote.*; |
9 | | -import org.openqa.selenium.By; |
10 | 11 | import org.openqa.selenium.WebDriver; |
11 | 12 | import org.openqa.selenium.WebElement; |
| 13 | +import org.openqa.selenium.remote.*; |
12 | 14 | import org.openqa.selenium.remote.RemoteWebDriver; |
| 15 | +import org.openqa.selenium.support.ui.*; |
13 | 16 |
|
14 | 17 | public class BrowserStackSampleLocal { |
15 | | - |
16 | | - private static Local localInstance; |
17 | | - public static String userName = "YOUR_USERNAME"; |
18 | | - public static String accessKey = "YOUR_ACCESS_KEY"; |
19 | | - |
20 | | - |
21 | | - public static void setupLocal() throws Exception { |
22 | | - localInstance = new Local(); |
23 | | - Map<String, String> options = new HashMap<String, String>(); |
24 | | - options.put("key", accessKey); |
25 | | - localInstance.start(options); |
26 | | - } |
27 | 18 |
|
28 | | - public static void tearDownLocal() throws Exception { |
29 | | - localInstance.stop(); |
30 | | - } |
| 19 | + private static Local localInstance; |
| 20 | + public static String userName = "YOUR_USERNAME"; |
| 21 | + public static String accessKey = "YOUR_ACCESS_KEY"; |
| 22 | + |
| 23 | + public static void setupLocal() throws Exception { |
| 24 | + localInstance = new Local(); |
| 25 | + Map<String, String> options = new HashMap<String, String>(); |
| 26 | + options.put("key", accessKey); |
| 27 | + options.put("local", "true"); |
| 28 | + localInstance.start(options); |
| 29 | + } |
| 30 | + |
| 31 | + public static void tearDownLocal() throws Exception { |
| 32 | + localInstance.stop(); |
| 33 | + } |
| 34 | + |
| 35 | + public static void main(String[] args) throws Exception { |
| 36 | + // Start the BrowserStack Local binary |
| 37 | + setupLocal(); |
| 38 | + |
| 39 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 40 | + |
| 41 | + // Set your access credentials |
| 42 | + capabilities.setCapability("browserstack.user", userName); |
| 43 | + capabilities.setCapability("browserstack.key", accessKey); |
31 | 44 |
|
| 45 | + // Set URL of the application under test |
| 46 | + capabilities.setCapability("app", "<app-id>"); |
32 | 47 |
|
33 | | - public static void main(String[] args) throws Exception { |
34 | | - // Start the BrowserStack Local binary |
35 | | - setupLocal(); |
36 | | - |
37 | | - DesiredCapabilities capabilities = new DesiredCapabilities(); |
38 | | - |
39 | | - // Set your access credentials |
40 | | - capabilities.setCapability("browserstack.user", userName); |
41 | | - capabilities.setCapability("browserstack.key", accessKey); |
42 | | - |
43 | | - // Set URL of the application under test |
44 | | - capabilities.setCapability("app", "bs://<app-id>"); |
45 | | - |
46 | | - // Specify device and os_version for testing |
47 | | - capabilities.setCapability("device", "iPhone 11 Pro"); |
48 | | - capabilities.setCapability("os_version", "13"); |
49 | | - |
50 | | - // Set the browserstack.local capability to true |
51 | | - capabilities.setCapability("browserstack.local", true); |
52 | | - |
53 | | - // Set other BrowserStack capabilities |
54 | | - capabilities.setCapability("project", "First Java Project"); |
55 | | - capabilities.setCapability("build", "browserstack-build-1"); |
56 | | - capabilities.setCapability("name", "local_test"); |
57 | | - |
58 | | - // Initialise the remote Webdriver using BrowserStack remote URL |
59 | | - // and desired capabilities defined above |
60 | | - RemoteWebDriver driver = new RemoteWebDriver( |
61 | | - new URL("http://hub.browserstack.com/wd/hub"), capabilities); |
62 | | - |
63 | | - // Test case for the BrowserStack sample iOS Local app. |
64 | | - // If you have uploaded your app, update the test case here. |
65 | | - WebElement testButton = (WebElement) new WebDriverWait(driver, 30).until( |
66 | | - ExpectedConditions.elementToBeClickable(By.xpath("//*[@content-desc='TestBrowserStackLocal']"))); |
67 | | - testButton.click(); |
68 | | - |
69 | | - WebDriverWait wait = new WebDriverWait(driver, 30); |
70 | | - wait.until(new ExpectedCondition<Boolean>() { |
71 | | - @Override |
72 | | - public Boolean apply(WebDriver d) { |
73 | | - String result = d.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")).getAttribute("value"); |
74 | | - return result != null && result.length() > 0; |
75 | | - } |
76 | | - }); |
77 | | - WebElement resultElement = (WebElement) driver.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")); |
78 | | - |
79 | | - String resultString = resultElement.getText().toLowerCase(); |
80 | | - System.out.println(resultString); |
81 | | - if(resultString.contains("not working")) { |
82 | | - File scrFile = (File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
83 | | - FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png")); |
84 | | - System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png"); |
85 | | - throw new Error("Unexpected BrowserStackLocal test result"); |
| 48 | + // Specify device and os_version for testing |
| 49 | + capabilities.setCapability("deviceName", "iPhone 11 Pro"); |
| 50 | + capabilities.setCapability("platformName", "ios"); |
| 51 | + capabilities.setCapability("platformVersion", "13"); |
| 52 | + |
| 53 | + // Set the browserstack.local capability to true |
| 54 | + capabilities.setCapability("browserstack.local", true); |
| 55 | + |
| 56 | + // Set other BrowserStack capabilities |
| 57 | + capabilities.setCapability("project", "First Java Project"); |
| 58 | + capabilities.setCapability("build", "browserstack-build-1"); |
| 59 | + capabilities.setCapability("name", "local_test"); |
| 60 | + |
| 61 | + // Initialise the remote Webdriver using BrowserStack remote URL |
| 62 | + // and desired capabilities defined above |
| 63 | + RemoteWebDriver driver = new RemoteWebDriver( |
| 64 | + new URL("http://hub.browserstack.com/wd/hub"), |
| 65 | + capabilities |
| 66 | + ); |
| 67 | + |
| 68 | + // Test case for the BrowserStack sample iOS Local app. |
| 69 | + // If you have uploaded your app, update the test case here. |
| 70 | + WebElement testButton = (WebElement) new WebDriverWait( |
| 71 | + driver, |
| 72 | + Duration.ofSeconds(30) |
| 73 | + ) |
| 74 | + .until( |
| 75 | + ExpectedConditions.elementToBeClickable( |
| 76 | + AppiumBy.accessibilityId("TestBrowserStackLocal") |
| 77 | + ) |
| 78 | + ); |
| 79 | + testButton.click(); |
| 80 | + |
| 81 | + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); |
| 82 | + wait.until( |
| 83 | + new ExpectedCondition<Boolean>() { |
| 84 | + @Override |
| 85 | + public Boolean apply(WebDriver d) { |
| 86 | + String result = d |
| 87 | + .findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal")) |
| 88 | + .getAttribute("value"); |
| 89 | + return result != null && result.length() > 0; |
86 | 90 | } |
| 91 | + } |
| 92 | + ); |
| 93 | + WebElement resultElement = (WebElement) driver.findElement( |
| 94 | + AppiumBy.accessibilityId("ResultBrowserStackLocal") |
| 95 | + ); |
| 96 | + |
| 97 | + String resultString = resultElement.getText().toLowerCase(); |
| 98 | + System.out.println(resultString); |
| 99 | + if (resultString.contains("not working")) { |
| 100 | + File scrFile = (File) ((TakesScreenshot) driver).getScreenshotAs( |
| 101 | + OutputType.FILE |
| 102 | + ); |
| 103 | + FileUtils.copyFile( |
| 104 | + scrFile, |
| 105 | + new File(System.getProperty("user.dir") + "/screenshot.png") |
| 106 | + ); |
| 107 | + System.out.println( |
| 108 | + "Screenshot stored at " + |
| 109 | + System.getProperty("user.dir") + |
| 110 | + "/screenshot.png" |
| 111 | + ); |
| 112 | + throw new Error("Unexpected BrowserStackLocal test result"); |
| 113 | + } |
87 | 114 |
|
88 | | - String expectedString = "Up and running"; |
89 | | - assert(resultString.contains(expectedString.toLowerCase())); |
| 115 | + String expectedString = "Up and running"; |
| 116 | + assert (resultString.contains(expectedString.toLowerCase())); |
90 | 117 |
|
91 | | - // Invoke driver.quit() after the test is done to indicate that the test is completed. |
92 | | - driver.quit(); |
93 | | - |
94 | | - // Stop the BrowserStack Local binary |
95 | | - tearDownLocal(); |
96 | | - |
97 | | - } |
| 118 | + // Invoke driver.quit() after the test is done to indicate that the test is completed. |
| 119 | + driver.quit(); |
98 | 120 |
|
| 121 | + // Stop the BrowserStack Local binary |
| 122 | + tearDownLocal(); |
| 123 | + } |
99 | 124 | } |
0 commit comments