|
| 1 | +import com.browserstack.local.Local; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.io.File; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.HashMap; |
| 7 | +import org.apache.commons.io.FileUtils; |
| 8 | + |
| 9 | +import io.appium.java_client.MobileBy; |
| 10 | +import io.appium.java_client.ios.IOSDriver; |
| 11 | +import io.appium.java_client.ios.IOSElement; |
| 12 | + |
| 13 | +import org.openqa.selenium.WebDriver; |
| 14 | +import org.openqa.selenium.OutputType; |
| 15 | +import org.openqa.selenium.TakesScreenshot; |
| 16 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 17 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 18 | +import org.openqa.selenium.support.ui.ExpectedCondition; |
| 19 | +import org.openqa.selenium.support.ui.ExpectedConditions; |
| 20 | + |
| 21 | + |
| 22 | +public class LocalSampleIOS { |
| 23 | + private static Local localInstance; |
| 24 | + public static String accessKey = "BROWSERSTACK_USERNAME"; |
| 25 | + public static String userName = "BROWSERSTACK_ACCESS_KEY"; |
| 26 | + |
| 27 | + |
| 28 | + public static void setupLocal() throws Exception { |
| 29 | + localInstance = new Local(); |
| 30 | + Map<String, String> options = new HashMap<String, String>(); |
| 31 | + options.put("key", accessKey); |
| 32 | + localInstance.start(options); |
| 33 | + } |
| 34 | + |
| 35 | + public static void tearDownLocal() throws Exception { |
| 36 | + localInstance.stop(); |
| 37 | + } |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + setupLocal(); |
| 41 | + |
| 42 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 43 | + |
| 44 | + capabilities.setCapability("browserstack.local", true); |
| 45 | + capabilities.setCapability("realMobile", true); |
| 46 | + capabilities.setCapability("device", "iPhone 7"); |
| 47 | + capabilities.setCapability("app", "bs://<hashed app-id>"); |
| 48 | + capabilities.setCapability("automationName", "XCUITest"); |
| 49 | + |
| 50 | + IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(new URL("http://"+userName+":"+accessKey+"@hub.browserstack.com/wd/hub"), capabilities); |
| 51 | + |
| 52 | + IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until( |
| 53 | + ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal"))); |
| 54 | + testButton.click(); |
| 55 | + |
| 56 | + WebDriverWait wait = new WebDriverWait(driver, 30); |
| 57 | + wait.until(new ExpectedCondition<Boolean>() { |
| 58 | + @Override |
| 59 | + public Boolean apply(WebDriver d) { |
| 60 | + String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value"); |
| 61 | + return result != null && result.length() > 0; |
| 62 | + } |
| 63 | + }); |
| 64 | + IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")); |
| 65 | + |
| 66 | + String resultString = resultElement.getText().toLowerCase(); |
| 67 | + System.out.println(resultString); |
| 68 | + if(resultString.contains("not working")) { |
| 69 | + File scrFile = (File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
| 70 | + FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png")); |
| 71 | + System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png"); |
| 72 | + throw new Error("Unexpected BrowserStackLocal test result"); |
| 73 | + } |
| 74 | + |
| 75 | + String expectedString = "Up and running"; |
| 76 | + assert(resultString.contains(expectedString.toLowerCase())); |
| 77 | + |
| 78 | + driver.quit(); |
| 79 | + |
| 80 | + tearDownLocal(); |
| 81 | + } |
| 82 | +} |
0 commit comments