diff --git a/android/LocalSampleAndroid.java b/android/LocalSampleAndroid.java new file mode 100644 index 0000000..af28764 --- /dev/null +++ b/android/LocalSampleAndroid.java @@ -0,0 +1,73 @@ +import com.browserstack.local.Local; + +import java.net.URL; +import java.util.Map; +import java.util.List; +import java.util.HashMap; + +import io.appium.java_client.MobileBy; +import io.appium.java_client.android.AndroidDriver; +import io.appium.java_client.android.AndroidElement; + +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.openqa.selenium.remote.DesiredCapabilities; + + +public class LocalSampleAndroid { + private static Local localInstance; + public static String accessKey = "BROWSERSTACK_USERNAME"; + public static String userName = "BROWSERSTACK_ACCESS_KEY"; + + + public static void setupLocal() throws Exception { + localInstance = new Local(); + Map options = new HashMap(); + options.put("key", accessKey); + localInstance.start(options); + } + + public static void tearDownLocal() throws Exception { + localInstance.stop(); + } + + public static void main(String[] args) throws Exception { + setupLocal(); + + DesiredCapabilities capabilities = new DesiredCapabilities(); + + capabilities.setCapability("browserstack.local", true); + capabilities.setCapability("realMobile", true); + capabilities.setCapability("device", "Samsung Galaxy S7"); + capabilities.setCapability("app", "bs://"); + + AndroidDriver driver = new AndroidDriver(new URL("https://"+userName+":"+accessKey+"@hub.browserstack.com/wd/hub"), capabilities); + + AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action"))); + searchElement.click(); + AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView"))); + + AndroidElement testElement = null; + List allTextViewElements = driver.findElementsByClassName("android.widget.TextView"); + Thread.sleep(10); + for(AndroidElement 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")); + + driver.quit(); + + tearDownLocal(); + } +} diff --git a/android/README.md b/android/README.md index 56d99bb..76ca27c 100644 --- a/android/README.md +++ b/android/README.md @@ -10,6 +10,8 @@ to the BrowserStack servers using the above API. - Update the desired capability "app" with the App URL returned from the above API call +For running LocalSample tests, you can download `browserstack-local-java.jar` from [here](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22browserstack-local-java%22). + For frameworks integration with BrowserStack, refer to their individual repositories - - [JUnit](https://github.com/browserstack/junit-appium-app-browserstack) diff --git a/ios/LocalSampleIOS.java b/ios/LocalSampleIOS.java new file mode 100644 index 0000000..75ff044 --- /dev/null +++ b/ios/LocalSampleIOS.java @@ -0,0 +1,82 @@ +import com.browserstack.local.Local; + +import java.net.URL; +import java.io.File; +import java.util.Map; +import java.util.HashMap; +import org.apache.commons.io.FileUtils; + +import io.appium.java_client.MobileBy; +import io.appium.java_client.ios.IOSDriver; +import io.appium.java_client.ios.IOSElement; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; + + +public class LocalSampleIOS { + private static Local localInstance; + public static String accessKey = "BROWSERSTACK_USERNAME"; + public static String userName = "BROWSERSTACK_ACCESS_KEY"; + + + public static void setupLocal() throws Exception { + localInstance = new Local(); + Map options = new HashMap(); + options.put("key", accessKey); + localInstance.start(options); + } + + public static void tearDownLocal() throws Exception { + localInstance.stop(); + } + + public static void main(String[] args) throws Exception { + setupLocal(); + + DesiredCapabilities capabilities = new DesiredCapabilities(); + + capabilities.setCapability("browserstack.local", true); + capabilities.setCapability("realMobile", true); + capabilities.setCapability("device", "iPhone 7"); + capabilities.setCapability("app", "bs://"); + capabilities.setCapability("automationName", "XCUITest"); + + IOSDriver driver = new IOSDriver(new URL("http://"+userName+":"+accessKey+"@hub.browserstack.com/wd/hub"), capabilities); + + IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal"))); + testButton.click(); + + WebDriverWait wait = new WebDriverWait(driver, 30); + wait.until(new ExpectedCondition() { + @Override + public Boolean apply(WebDriver d) { + String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value"); + return result != null && result.length() > 0; + } + }); + IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")); + + String resultString = resultElement.getText().toLowerCase(); + System.out.println(resultString); + if(resultString.contains("not working")) { + File scrFile = (File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); + FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png")); + System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png"); + throw new Error("Unexpected BrowserStackLocal test result"); + } + + String expectedString = "Up and running"; + assert(resultString.contains(expectedString.toLowerCase())); + + driver.quit(); + + tearDownLocal(); + } +} diff --git a/ios/README.md b/ios/README.md index dd76f1e..7124353 100644 --- a/ios/README.md +++ b/ios/README.md @@ -10,6 +10,8 @@ to the BrowserStack servers using the above API. - Update the desired capability "app" with the App URL returned from the above API call +For running LocalSample tests, you can download `browserstack-local-java.jar` from [here](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22browserstack-local-java%22). + For frameworks integration with BrowserStack, refer to their individual repositories - - [JUnit](https://github.com/browserstack/junit-appium-app-browserstack)