Bring Sample Code into master (#10880)

* Sample code stub (#9887)

* WD tests

* WD sample code (#9918)

* WebdriverIO sample code (#10166)

* Ruby sample code (#10331)

* PHP Sample Code (#10209)

* Basic Android java test

* Java sample code (#10427)

* Sample code (#10834)

* Sample code stub (#9887)

* WD tests

* WD sample code (#9918)

* WebdriverIO sample code (#10166)

* Ruby sample code (#10331)

* PHP Sample Code (#10209)

* Basic Android java test

* Java sample code (#10427)

* fixed WDIO test

* Update .npmignore
This commit is contained in:
Isaac A. Murchie
2018-06-15 08:23:42 -04:00
committed by GitHub
parent aab1649996
commit 04f6543561
68 changed files with 2098 additions and 27 deletions

View File

@@ -0,0 +1,71 @@
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
public class AndroidBasicInteractionsTest extends BaseTest {
private AndroidDriver<WebElement> driver;
private final String SEARCH_ACTIVITY = ".app.SearchInvoke";
private final String ALERT_DIALOG_ACTIVITY = ".app.AlertDialogSamples";
private final String PACKAGE = "io.appium.android.apis";
@BeforeClass
public void setUp() throws IOException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AndroidDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterClass
public void tearDown() {
driver.quit();
}
@Test()
public void testSendKeys() {
driver.startActivity(new Activity(PACKAGE, SEARCH_ACTIVITY));
AndroidElement searchBoxEl = (AndroidElement) driver.findElementById("txt_query_prefill");
searchBoxEl.sendKeys("Hello world!");
AndroidElement onSearchRequestedBtn = (AndroidElement) driver.findElementById("btn_start_search");
onSearchRequestedBtn.click();
AndroidElement searchText = (AndroidElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(By.id("android:id/search_src_text")));
String searchTextValue = searchText.getText();
Assert.assertEquals(searchTextValue, "Hello world!");
}
@Test
public void testOpensAlert() {
// Open the "Alert Dialog" activity of the android app
driver.startActivity(new Activity(PACKAGE, ALERT_DIALOG_ACTIVITY));
// Click button that opens a dialog
AndroidElement openDialogButton = (AndroidElement) driver.findElementById("io.appium.android.apis:id/two_buttons");
openDialogButton.click();
// Check that the dialog is there
AndroidElement alertElement = (AndroidElement) driver.findElementById("android:id/alertTitle");
String alertText = alertElement.getText();
Assert.assertEquals(alertText, "Lorem ipsum dolor sit aie consectetur adipiscing\nPlloaso mako nuto siwuf cakso dodtos anr koop.");
AndroidElement closeDialogButton = (AndroidElement) driver.findElementById("android:id/button1");
// Close the dialog
closeDialogButton.click();
}
}

View File

@@ -0,0 +1,37 @@
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.File;
public class AndroidCreateSessionTest extends BaseTest {
private AndroidDriver<WebElement> driver;
@BeforeClass
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", ".ApiDemos");
driver = new AndroidDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterClass
public void tearDown() {
driver.quit();
}
@Test()
public void testCreateSession() {
String activity = driver.currentActivity();
String pkg = driver.getCurrentPackage();
Assert.assertEquals(activity, ".ApiDemos");
Assert.assertEquals(pkg, "io.appium.android.apis");
}
}

View File

@@ -0,0 +1,32 @@
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.*;
import java.net.URI;
import java.net.URISyntaxException;
public class AndroidCreateWebSessionTest extends BaseTest {
private AndroidDriver<WebElement> driver;
@BeforeClass
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("browserName", "Chrome");
driver = new AndroidDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterClass
public void tearDown() {
driver.quit();
}
@Test()
public void testCreateWebSession() throws URISyntaxException {
driver.get(new URI("http://www.google.com").toString());
String title = driver.getTitle();
Assert.assertEquals(title, "Google");
}
}

View File

@@ -0,0 +1,64 @@
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.io.File;
import java.util.List;
public class AndroidSelectorsTest extends BaseTest {
private AndroidDriver<WebElement> driver;
private static AppiumDriverLocalService service;
private final String PACKAGE = "io.appium.android.apis";
@BeforeSuite
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", ".ApiDemos");
driver = new AndroidDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterSuite
public void tearDown() {
driver.quit();
}
@Test
public void testFindElementsByAccessibilityId () {
// Look for element by accessibility. In Android this is the "content-desc"
List<WebElement> searchParametersElement = (List<WebElement>) driver.findElementsByAccessibilityId("Content");
Assert.assertEquals(searchParametersElement.size(), 1);
}
@Test
public void testFindElementsById () {
// Look for element by ID. In Android this is the "resource-id"
List<WebElement> actionBarContainerElements = (List<WebElement>) driver.findElementsById("android:id/action_bar_container");
Assert.assertEquals(actionBarContainerElements.size(), 1);
}
@Test
public void testFindElementsByClassName () {
// Look for elements by the class name. In Android this is the Java Class Name of the view.
List<WebElement> linearLayoutElements = (List<WebElement>) driver.findElementsByClassName("android.widget.FrameLayout");
Assert.assertTrue(linearLayoutElements.size() > 1);
};
@Test
public void testFindElementsByXPath () {
// Find elements by XPath
List<WebElement> linearLayoutElements = (List<WebElement>) driver.findElementsByXPath("//*[@class=\"android.widget.FrameLayout\"]");
Assert.assertTrue(linearLayoutElements.size() > 1);
};
}

View File

@@ -0,0 +1,27 @@
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.IOException;
import java.net.URL;
public abstract class BaseTest {
private static AppiumDriverLocalService service;
@BeforeSuite
public void globalSetup () throws IOException {
service = AppiumDriverLocalService.buildDefaultService();
service.start();
}
@AfterSuite
public void globalTearDown () {
service.stop();
}
public URL getServiceUrl () {
return service.getUrl();
}
}

View File

@@ -0,0 +1,79 @@
import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
public class IOSBasicInteractionsTest extends BaseTest {
private IOSDriver<WebElement> driver;
@BeforeTest
public void setUp() throws IOException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "TestApp.app.zip");
String deviceName = System.getenv("IOS_DEVICE_NAME");
String platformVersion = System.getenv("IOS_PLATFORM_VERSION");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", deviceName == null ? "iPhone 6s" : deviceName);
capabilities.setCapability("platformVerison", platformVersion == null ? "11.1" : platformVersion);
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("automationName", "XCUITest");
driver = new IOSDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterTest
public void tearDown() {
driver.quit();
}
@Test
public void testSendKeysToInput () {
// Find TextField input element
String textInputId = "TextField1";
IOSElement textViewsEl = (IOSElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.AccessibilityId(textInputId)));
// Check that it doesn"t have a value
String value = textViewsEl.getAttribute("value");
Assert.assertEquals(value, null);
// Send keys to that input
textViewsEl.sendKeys("Hello World!");
// Check that the input has new value
value = textViewsEl.getAttribute("value");
Assert.assertEquals(value, "Hello World!");
}
@Test
public void testOpenAlert () {
// Find Button element and click on it
String buttonElementId = "show alert";
IOSElement buttonElement = (IOSElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.AccessibilityId(buttonElementId)));
buttonElement.click();
// Wait for the alert to show up
String alertTitleId = "Cool title";
IOSElement alertTitleElement = (IOSElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.AccessibilityId(alertTitleId)));
// Check the text
String alertTitle = alertTitleElement.getText();
Assert.assertEquals(alertTitle, "Cool title");
// Dismiss the alert
IOSElement okButtonElement = (IOSElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.AccessibilityId("OK")));
okButtonElement.click();
}
}

View File

@@ -0,0 +1,43 @@
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.io.File;
public class IOSCreateSessionTest extends BaseTest {
private IOSDriver<WebElement> driver;
@BeforeSuite
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "TestApp.app.zip");
String deviceName = System.getenv("IOS_DEVICE_NAME");
String platformVersion = System.getenv("IOS_PLATFORM_VERSION");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", deviceName == null ? "iPhone 6s" : deviceName);
capabilities.setCapability("platformVerison", platformVersion == null ? "11.1" : platformVersion);
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("automationName", "XCUITest");
driver = new IOSDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterSuite
public void tearDown() {
driver.quit();
}
@Test
public void testCreateSession () {
// Check that the XCUIElementTypeApplication was what we expect it to be
IOSElement applicationElement = (IOSElement) driver.findElementByClassName("XCUIElementTypeApplication");
String applicationName = applicationElement.getAttribute("name");
Assert.assertEquals(applicationName, "TestApp");
}
}

View File

@@ -0,0 +1,41 @@
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.io.IOException;
public class IOSCreateWebSessionTest extends BaseTest {
private IOSDriver<WebElement> driver;
@BeforeSuite
public void setUp() throws IOException {
String deviceName = System.getenv("IOS_DEVICE_NAME");
String platformVersion = System.getenv("IOS_PLATFORM_VERSION");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", deviceName == null ? "iPhone 6s" : deviceName);
capabilities.setCapability("platformVerison", platformVersion == null ? "11.1" : platformVersion);
capabilities.setCapability("browserName", "Safari");
capabilities.setCapability("automationName", "XCUITest");
driver = new IOSDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterSuite
public void tearDown() {
driver.quit();
}
@Test()
public void testCreateSafariSession() {
// Navigate to google.com
driver.get("https://www.google.com");
// Test that it was successful by checking the document title
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, "Google");
}
}

View File

@@ -0,0 +1,72 @@
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.io.File;
import java.util.List;
public class IOSSelectorsTest extends BaseTest {
private IOSDriver<WebElement> driver;
@BeforeSuite
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../apps");
File app = new File(appDir.getCanonicalPath(), "TestApp.app.zip");
String deviceName = System.getenv("IOS_DEVICE_NAME");
String platformVersion = System.getenv("IOS_PLATFORM_VERSION");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", deviceName == null ? "iPhone 6s" : deviceName);
capabilities.setCapability("platformVerison", platformVersion == null ? "11.1" : platformVersion);
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("automationName", "XCUITest");
driver = new IOSDriver<WebElement>(getServiceUrl(), capabilities);
}
@AfterSuite
public void tearDown() {
driver.quit();
}
@Test
public void testFindElementsByAccessibilityID () {
// This finds elements by "accessibility id", which in the case of IOS is the "name" attribute of the element
List<WebElement> computeSumButtons = driver.findElementsByAccessibilityId("ComputeSumButton");
Assert.assertEquals(computeSumButtons.size(), 1);
computeSumButtons.get(0).click();
}
@Test
public void testFindElementsByClassName () {
// Find element by name
List<WebElement> windowElements = driver.findElementsByClassName("XCUIElementTypeWindow");
Assert.assertTrue(windowElements.size() > 1);
};
@Test
public void testFindElementsByNSPredicateString () {
// This is an IOS-specific selector strategy. See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html for reference
List<WebElement> allVisibleElements = driver.findElementsByIosNsPredicate("visible = true");
Assert.assertTrue(allVisibleElements.size() > 1);
};
@Test
public void testFindElementsByClassChain () {
// This is also an IOS-specific selector strategy. Similar to XPath. This is recommended over XPath.
List<WebElement> windowElements = driver.findElementsByIosClassChain("XCUIElementTypeWindow[1]/*[2]");
Assert.assertEquals(windowElements.size(), 1);
};
@Test
public void testFindElementsByXPath () {
// Can find source xml by calling "driver.source()"
// Note that XPath is not recommended due to major performance issues
List<WebElement> buttons = driver.findElementsByXPath("//XCUIElementTypeWindow//XCUIElementTypeButton");
Assert.assertTrue(buttons.size() > 1);
};
}