Appium java client has some features based on Java 8 Functional interfaces.
io.appium.java_client.functions.AppiumFunction
It extends
java.util.function.Function
and
com.google.common.base.Function
to make end user available to use org.openqa.selenium.support.ui.Wait. There is additional interface
io.appium.java_client.functions.ExpectedCondition
which extends
io.appium.java_client.functions.AppiumFunction
and
org.openqa.selenium.support.ui.ExpectedCondition
This feature provides the ability to create complex condition of the waiting for something.
//waiting for elements
private final AppiumFunction<WebDriver, List<WebElement>> searchingFunction = input -> {
List<WebElement> result = input.findElements(By.tagName("a"));
if (result.size() > 0) {
return result;
}
return null;
};
//waiting for some context using regular expression pattern
private final AppiumFunction<Pattern, WebDriver> contextFunction = input -> {
Set<String> contexts = driver.getContextHandles();
String current = driver.getContext();
contexts.forEach(context -> {
Matcher m = input.matcher(context);
if (m.find()) {
driver.context(context);
}
});
if (!current.equals(driver.getContext())) {
return driver;
}
return null;
};
@Test public void tezt() {
....
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(30, TimeUnit.SECONDS);
List<WebElement> elements = wait.until(searchingFunction.compose(contextFunction));
....
}
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
@Test public void tezt() {
....
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(30, TimeUnit.SECONDS);
List<WebElement> elements = wait.until(contextFunction.andThen(searchingFunction));
....
}
You can use suppliers to declare touch/multitouch actions for some screens/tests. Also it is possible to create gesture libraries/utils using suppliers. Appium java client provides this interface
io.appium.java_client.functions.ActionSupplier
private final ActionSupplier<TouchAction> horizontalSwipe = () -> {
driver.findElementById("io.appium.android.apis:id/gallery");
AndroidElement gallery = driver.findElementById("io.appium.android.apis:id/gallery");
List<MobileElement> images = gallery
.findElementsByClassName("android.widget.ImageView");
Point location = gallery.getLocation();
Point center = gallery.getCenter();
return new TouchAction(driver).press(images.get(2), -10, center.y - location.y)
.waitAction(2000).moveTo(gallery, 10, center.y - location.y).release();
};
private final ActionSupplier<TouchAction> verticalSwiping = () ->
new TouchAction(driver).press(driver.findElementByAccessibilityId("Gallery"))
.waitAction(2000).moveTo(driver.findElementByAccessibilityId("Auto Complete")).release();
@Test public void tezt() {
...
horizontalSwipe.get().perform();
...
verticalSwiping.get().perform();
...
}
public class GestureUtils {
public static ActionSupplier<TouchAction> swipe(final AppiumDriver<?> driver, final params) {
return () -> {
new TouchAction(driver).press(params)
.waitAction(params).moveTo(params).release();
};
}
}
public class SomeTest {
@Test public void tezt() {
...
GestureUtils.swipe(driver, params).get().perform();
...
}
}