Skip to content

Commit 9279b00

Browse files
author
Tejas Shah
committed
Add AppAutomate ios sample tests
1 parent ae869b4 commit 9279b00

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

ios/BrowserStack.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.net.URL;
2+
import java.util.List;
3+
import java.net.MalformedURLException;
4+
5+
import org.openqa.selenium.support.ui.WebDriverWait;
6+
import org.openqa.selenium.remote.DesiredCapabilities;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
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+
14+
public class BrowserStack {
15+
public static String accessKey = "BROWSERSTACK_USERNAME";
16+
public static String userName = "BROWSERSTACK_ACCESS_KEY";
17+
18+
public static void main(String args[]) throws MalformedURLException, InterruptedException {
19+
DesiredCapabilities capabilities = new DesiredCapabilities();
20+
21+
capabilities.setCapability("realMobile", true);
22+
capabilities.setCapability("device", "iPhone 7");
23+
capabilities.setCapability("app", "bs://<hashed app-id>");
24+
capabilities.setCapability("automationName", "XCUITest");
25+
26+
IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(new URL("http://"+userName+":"+accessKey+"@hub.browserstack.com/wd/hub"), capabilities);
27+
28+
IOSElement loginButton = (IOSElement) new WebDriverWait(driver, 30).until(
29+
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Log In")));
30+
loginButton.click();
31+
IOSElement emailTextField = (IOSElement) new WebDriverWait(driver, 30).until(
32+
ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Email address")));
33+
34+
// element.sendKeys() method is not supported in Appium 1.6.3
35+
// Workaround for sendKeys() method:
36+
emailTextField.click();
37+
String email = "hello@browserstack.com";
38+
for (int i = 0; i < email.length(); i++) {
39+
driver.findElementByXPath("//XCUIElementTypeKey[@name='" + email.charAt(i) + "']").click();
40+
}
41+
42+
driver.findElementByAccessibilityId("Next").click();
43+
Thread.sleep(5000);
44+
45+
List<IOSElement> textElements = driver.findElementsByXPath("//XCUIElementTypeStaticText");
46+
assert(textElements.size() > 0);
47+
String matchedString = "";
48+
for(IOSElement textElement : textElements) {
49+
String textContent = textElement.getText();
50+
if(textContent.contains("not registered")) {
51+
matchedString = textContent;
52+
}
53+
}
54+
55+
System.out.println(matchedString);
56+
assert(matchedString.contains("not registered on WordPress.com"));
57+
58+
driver.quit();
59+
}
60+
}

ios/LocalSample.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 LocalSample {
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

Comments
 (0)