Skip to content

re-add local samples | update readme #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions android/LocalSampleAndroid.java
Original file line number Diff line number Diff line change
@@ -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<String, String> options = new HashMap<String, String>();
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://<hashed app-id>");

AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(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<AndroidElement> 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();
}
}
2 changes: 2 additions & 0 deletions android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
82 changes: 82 additions & 0 deletions ios/LocalSampleIOS.java
Original file line number Diff line number Diff line change
@@ -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<String, String> options = new HashMap<String, String>();
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://<hashed app-id>");
capabilities.setCapability("automationName", "XCUITest");

IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(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<Boolean>() {
@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();
}
}
2 changes: 2 additions & 0 deletions ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down