Skip to content

Functions #543

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 7 commits into from
Dec 22, 2016
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
6 changes: 3 additions & 3 deletions src/main/java/io/appium/java_client/MultiTouchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Calling perform() sends the action command to the Mobile Driver. Otherwise, more and
* more actions can be chained.
*/
public class MultiTouchAction {
public class MultiTouchAction implements PerformsActions<MultiTouchAction> {

private ImmutableList.Builder<TouchAction> actions;
private PerformsTouchActions performsTouchActions;
Expand All @@ -61,7 +61,7 @@ public MultiTouchAction add(TouchAction action) {
/**
* Perform the multi-touch action on the mobile performsTouchActions.
*/
public void perform() {
public MultiTouchAction perform() {
int size = actions.build().size();
if (size > 1) {
performsTouchActions.performMultiTouchAction(this);
Expand All @@ -73,7 +73,7 @@ public void perform() {
"MultiTouch action must have at least one TouchAction "
+ "added before it can be performed");
}

return this;
}

protected ImmutableMap<String, ImmutableList<Object>> getParameters() {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/appium/java_client/PerformsActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client;

public interface PerformsActions<T extends PerformsActions> {

T perform();
}
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/TouchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Calling perform() sends the action command to the Mobile Driver. Otherwise,
* more and more actions can be chained.
*/
public class TouchAction {
public class TouchAction implements PerformsActions<TouchAction> {

protected ImmutableList.Builder<ActionParameter> parameterBuilder;
private PerformsTouchActions performsTouchActions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.functions;

import io.appium.java_client.PerformsActions;

import java.util.function.Supplier;

@FunctionalInterface
public interface ActionSupplier<T extends PerformsActions<?>> extends Supplier<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.functions;

import com.google.common.base.Function;

import java.util.Objects;
import java.util.Optional;

/**
* This is extended version of {@link com.google.common.base.Function}. It is combined
* with {@link java.util.function.Function}. It was designed in order to provide compatibility
* with the {@link org.openqa.selenium.support.ui.Wait}.
*
* @param <F> The input type
* @param <T> The return type
*/
@FunctionalInterface
public interface AppiumFunction<F, T> extends Function<F, T>, java.util.function.Function<F, T> {

@Override default <V> AppiumFunction<V, T> compose(java.util.function.Function<? super V, ? extends F> before) {
Objects.requireNonNull(before);
return (V v) -> {
F f = before.apply(v);
return Optional.ofNullable(f != null ? apply(f) : null).orElse(null);
};
}

@Override default <V> AppiumFunction<F, V> andThen(java.util.function.Function<? super T, ? extends V> after) {
Objects.requireNonNull(after);
return (F f) -> {
T result = apply(f);
return Optional.ofNullable(result != null ? after.apply(result) : null).orElse(null);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.functions;

import org.openqa.selenium.WebDriver;

/**
* This is extended version of {@link org.openqa.selenium.support.ui.ExpectedCondition}. It is combined
* with {@link java.util.function.Function}.
*
* @param <T> The return type
*/
@FunctionalInterface
public interface ExpectedCondition<T> extends org.openqa.selenium.support.ui.ExpectedCondition<T>,
AppiumFunction<WebDriver, T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.appium.java_client.android;

import static org.junit.Assert.assertNotEquals;

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.functions.ActionSupplier;
import org.junit.Test;
import org.openqa.selenium.Point;

import java.util.List;

public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {

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 horizontalSwipingWithSupplier() throws Exception {
driver.startActivity("io.appium.android.apis", ".view.Gallery1");
AndroidElement gallery = driver.findElementById("io.appium.android.apis:id/gallery");
List<MobileElement> images = gallery
.findElementsByClassName("android.widget.ImageView");
int originalImageCount = images.size();

horizontalSwipe.get().perform();

assertNotEquals(originalImageCount, gallery
.findElementsByClassName("android.widget.ImageView").size());
}

@Test public void verticalSwipingWithSupplier() throws Exception {
driver.resetApp();
driver.findElementByAccessibilityId("Views").click();

Point originalLocation = driver.findElementByAccessibilityId("Gallery").getLocation();
verticalSwiping.get().perform();
Thread.sleep(5000);
assertNotEquals(originalLocation, driver.findElementByAccessibilityId("Gallery").getLocation());
}
}
135 changes: 135 additions & 0 deletions src/test/java/io/appium/java_client/android/AndroidFunctionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.appium.java_client.android;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import io.appium.java_client.functions.AppiumFunction;
import io.appium.java_client.functions.ExpectedCondition;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AndroidFunctionTest extends BaseAndroidTest {

private final AppiumFunction<WebDriver, List<WebElement>> searchingFunction = input -> {
List<WebElement> result = input.findElements(By.tagName("a"));

if (result.size() > 0) {
return result;
}
return null;
};

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;
};

private final AppiumFunction<List<WebElement>, List<WebElement>> filteringFunction = input -> {
final List<WebElement> result = new ArrayList<>();
input.forEach(element -> {
if (element.getText().equals("Hello World! - 1")) {
result.add(element);
}
});

if (result.size() > 0) {
return result;
}
return null;
};

@BeforeClass public static void startWebViewActivity() throws Exception {
if (driver != null) {
driver.startActivity("io.appium.android.apis", ".view.WebView1");
}
}

@Before public void setUp() {
driver.context("NATIVE_APP");
}

@Test public void complexWaitingTestWithPreCondition() {
AppiumFunction<Pattern, List<WebElement>> compositeFunction =
searchingFunction.compose(contextFunction);

Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(30, TimeUnit.SECONDS);
List<WebElement> elements = wait.until(compositeFunction);

assertThat("Element size should be 1", elements.size(), is(1));
assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW"));
}

@Test public void complexWaitingTestWithPostConditions() {
final List<Boolean> calls = new ArrayList<>();

AppiumFunction<Pattern, WebDriver> waitingForContext = input -> {
WebDriver result = contextFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
};

AppiumFunction<Pattern, List<WebElement>> compositeFunction = waitingForContext
.andThen((ExpectedCondition<List<WebElement>>) input -> {
List<WebElement> result = searchingFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
})
.andThen((AppiumFunction<List<WebElement>, List<WebElement>>) input -> {
List<WebElement> result = filteringFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
});

Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(30, TimeUnit.SECONDS);
List<WebElement> elements = wait.until(compositeFunction);
assertThat("Element size should be 1", elements.size(), is(1));
assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW"));
assertThat("There should be 3 calls", calls.size(), is(3));
}

@Test(expected = TimeoutException.class) public void nullPointerExceptionSafetyTestWithPrecondition() {
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context"))
.withTimeout(30, TimeUnit.SECONDS).pollingEvery(500, TimeUnit.MILLISECONDS);
assertTrue(wait.until(searchingFunction.compose(contextFunction)).size() > 0);
}

@Test(expected = TimeoutException.class) public void nullPointerExceptionSafetyTestWithPostConditions() {
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context"))
.withTimeout(30, TimeUnit.SECONDS).pollingEvery(500, TimeUnit.MILLISECONDS);
assertTrue(wait.until(contextFunction.andThen(searchingFunction).andThen(filteringFunction)).size() > 0);
}
}
3 changes: 2 additions & 1 deletion src/test/java/io/appium/java_client/ios/AppIOSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
import org.junit.BeforeClass;
import org.openqa.selenium.remote.DesiredCapabilities;

Expand All @@ -16,7 +17,7 @@ public static void beforeClass() throws Exception {
service.start();

if (service == null || !service.isRunning()) {
throw new RuntimeException("An appium server node is not started!");
throw new AppiumServerHasNotBeenStartedLocallyException("An appium server node is not started!");
}

File appDir = new File("src/test/java/io/appium/java_client");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
import org.junit.BeforeClass;
import org.openqa.selenium.remote.DesiredCapabilities;

Expand All @@ -31,7 +32,7 @@ public class BaseIOSWebViewTest extends BaseIOSTest {
service.start();

if (service == null || !service.isRunning()) {
throw new RuntimeException("An appium server node is not started!");
throw new AppiumServerHasNotBeenStartedLocallyException("An appium server node is not started!");
}

File appDir = new File("src/test/java/io/appium/java_client");
Expand Down
Loading