Skip to content
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

Implement Espresso contrib mobile commands #324

Merged
merged 12 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.espressoserver.lib.handlers;

import java.util.regex.Pattern;

import androidx.test.espresso.EspressoException;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.contrib.DrawerActions;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.model.DrawerActionParams;
import io.appium.espressoserver.lib.model.Element;
import io.appium.espressoserver.lib.model.ToastLookupParams;
import io.appium.espressoserver.lib.viewmatcher.ToastMatcher;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static io.appium.espressoserver.lib.viewmatcher.RegexpTextMatcher.withRegexp;

public class DrawerActionHandler implements RequestHandler<DrawerActionParams, Void> {
private final boolean isOpenAction;

public DrawerActionHandler(boolean isOpenAction) {
this.isOpenAction = isOpenAction;
}

@Override
public Void handle(DrawerActionParams params) throws AppiumException {
ViewInteraction viewInteraction = Element.getViewInteractionById(params.getElementId());
Integer gravity = params.getGravity();
try {
if (isOpenAction) {
if (gravity == null) {
viewInteraction.perform(DrawerActions.open());
} else {
viewInteraction.perform(DrawerActions.open(gravity));
}
} else {
if (gravity == null) {
viewInteraction.perform(DrawerActions.close());
} else {
viewInteraction.perform(DrawerActions.close(gravity));
}
}
} catch (Exception e) {
if (e instanceof EspressoException) {
throw new AppiumException(String.format("Could not %s drawer. Reason: %s", isOpenAction ? "open" : "close" ,e));
}
throw e;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.espressoserver.lib.handlers;

import androidx.test.espresso.EspressoException;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.contrib.NavigationViewActions;
import androidx.test.espresso.contrib.PickerActions;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.helpers.AndroidLogger;
import io.appium.espressoserver.lib.model.Element;
import io.appium.espressoserver.lib.model.NavigateToParams;
import io.appium.espressoserver.lib.model.ScrollToPageParams;
import io.appium.espressoserver.lib.model.SetTimeParams;

public class NavigateTo implements RequestHandler<NavigateToParams, Void> {

@Override
public Void handle(NavigateToParams params) throws AppiumException {
ViewInteraction viewInteraction = Element.getViewInteractionById(params.getElementId());
Integer menuItemId = params.getMenuItemId();
try {
viewInteraction.perform(NavigationViewActions.navigateTo(menuItemId));
} catch (Exception e) {
if (e instanceof EspressoException) {
throw new AppiumException(String.format("Could not navigate to menu item %s. Reason: %s", menuItemId, e));
}
throw e;
}
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package io.appium.espressoserver.lib.handlers;

import android.graphics.Bitmap;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

import androidx.test.runner.screenshot.ScreenCapture;
import androidx.test.runner.screenshot.Screenshot;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.helpers.ScreenshotsHelper;
import io.appium.espressoserver.lib.model.AppiumParams;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.espressoserver.lib.handlers;

import android.support.v4.view.ViewPager;

import androidx.test.espresso.EspressoException;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.action.ScrollToAction;
import androidx.test.espresso.contrib.PickerActions;
import androidx.test.espresso.contrib.ViewPagerActions;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.handlers.exceptions.InvalidArgumentException;
import io.appium.espressoserver.lib.helpers.AndroidLogger;
import io.appium.espressoserver.lib.model.Element;
import io.appium.espressoserver.lib.model.ScrollToPageParams;
import io.appium.espressoserver.lib.model.SetTimeParams;

public class ScrollToPage implements RequestHandler<ScrollToPageParams, Void> {

@Override
public Void handle(ScrollToPageParams params) throws AppiumException {
ViewInteraction viewInteraction = Element.getViewInteractionById(params.getElementId());
try {
ViewAction scrollAction;
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
boolean smoothScroll = params.getSmoothScroll();
if (params.getScrollTo() != null) {
switch (params.getScrollTo()) {
case FIRST:
scrollAction = ViewPagerActions.scrollToFirst(smoothScroll);
break;
case LAST:
scrollAction = ViewPagerActions.scrollToLast(smoothScroll);
break;
case LEFT:
scrollAction = ViewPagerActions.scrollLeft(smoothScroll);
break;
case RIGHT:
scrollAction = ViewPagerActions.scrollRight(smoothScroll);
break;
default:
throw new AppiumException(String.format("Invalid scrollTo param '%s'", params.getScrollTo().name()));
}
} else if (params.getScrollToPage() != null) {
scrollAction = ViewPagerActions.scrollToPage(params.getScrollToPage(), smoothScroll);
} else {
throw new InvalidArgumentException("Could not complete scrollToPage action. Must provide either 'scrollTo' or 'scrollToPage'");
}
viewInteraction.perform(scrollAction);

} catch (ClassCastException e) {
throw new AppiumException(String.format("Could not perform scroll to on element %s. Reason: %s", params.getElementId(), e));
} catch (Exception e) {
if (e instanceof EspressoException) {
throw new AppiumException(String.format("Could not scroll to page. Reason: %s", e));
}
throw e;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.espressoserver.lib.handlers;

import androidx.test.espresso.EspressoException;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.contrib.DrawerActions;
import androidx.test.espresso.contrib.PickerActions;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.model.DrawerActionParams;
import io.appium.espressoserver.lib.model.Element;
import io.appium.espressoserver.lib.model.SetDateParams;

public class SetDate implements RequestHandler<SetDateParams, Void> {

@Override
public Void handle(SetDateParams params) throws AppiumException {
ViewInteraction viewInteraction = Element.getViewInteractionById(params.getElementId());
try {
viewInteraction.perform(PickerActions.setDate(params.getYear(), params.getMonthOfYear(), params.getDayOfMonth()));
} catch (Exception e) {
if (e instanceof EspressoException) {
throw new AppiumException(String.format("Could not set date on element. Reason: %s", e));
}
throw e;
}
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.espressoserver.lib.handlers;

import androidx.test.espresso.EspressoException;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.contrib.PickerActions;
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException;
import io.appium.espressoserver.lib.model.Element;
import io.appium.espressoserver.lib.model.SetTimeParams;

public class SetTime implements RequestHandler<SetTimeParams, Void> {

@Override
public Void handle(SetTimeParams params) throws AppiumException {
ViewInteraction viewInteraction = Element.getViewInteractionById(params.getElementId());
try {
viewInteraction.perform(PickerActions.setTime(params.getHours(), params.getMinutes()));
} catch (Exception e) {
if (e instanceof EspressoException) {
throw new AppiumException(String.format("Could not set time on element. Reason: %s", e));
}
throw e;
}
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.appium.espressoserver.lib.handlers.CreateSession;
import io.appium.espressoserver.lib.handlers.DeleteSession;
import io.appium.espressoserver.lib.handlers.DismissAlert;
import io.appium.espressoserver.lib.handlers.DrawerActionHandler;
import io.appium.espressoserver.lib.handlers.ElementEquals;
import io.appium.espressoserver.lib.handlers.ElementScreenshot;
import io.appium.espressoserver.lib.handlers.ElementValue;
Expand Down Expand Up @@ -58,6 +59,7 @@
import io.appium.espressoserver.lib.handlers.MobileSwipe;
import io.appium.espressoserver.lib.handlers.MultiTouchAction;
import io.appium.espressoserver.lib.handlers.MultiTouchActionsParams;
import io.appium.espressoserver.lib.handlers.NavigateTo;
import io.appium.espressoserver.lib.handlers.NotYetImplemented;
import io.appium.espressoserver.lib.handlers.PerformAction;
import io.appium.espressoserver.lib.handlers.PerformEditorAction;
Expand All @@ -66,8 +68,11 @@
import io.appium.espressoserver.lib.handlers.ReleaseActions;
import io.appium.espressoserver.lib.handlers.RequestHandler;
import io.appium.espressoserver.lib.handlers.ScreenshotHandler;
import io.appium.espressoserver.lib.handlers.ScrollToPage;
import io.appium.espressoserver.lib.handlers.SendKeys;
import io.appium.espressoserver.lib.handlers.SetDate;
import io.appium.espressoserver.lib.handlers.SetOrientation;
import io.appium.espressoserver.lib.handlers.SetTime;
import io.appium.espressoserver.lib.handlers.Source;
import io.appium.espressoserver.lib.handlers.StartActivity;
import io.appium.espressoserver.lib.handlers.Status;
Expand All @@ -91,15 +96,20 @@
import io.appium.espressoserver.lib.http.response.BaseResponse;
import io.appium.espressoserver.lib.model.AppiumParams;
import io.appium.espressoserver.lib.model.AppiumStatus;
import io.appium.espressoserver.lib.model.DrawerActionParams;
import io.appium.espressoserver.lib.model.EditorActionParams;
import io.appium.espressoserver.lib.model.ElementValueParams;
import io.appium.espressoserver.lib.model.KeyEventParams;
import io.appium.espressoserver.lib.model.Locator;
import io.appium.espressoserver.lib.model.MobileSwipeParams;
import io.appium.espressoserver.lib.model.MotionEventParams;
import io.appium.espressoserver.lib.model.NavigateToParams;
import io.appium.espressoserver.lib.model.OrientationParams;
import io.appium.espressoserver.lib.model.ScrollToPageParams;
import io.appium.espressoserver.lib.model.Session;
import io.appium.espressoserver.lib.model.SessionParams;
import io.appium.espressoserver.lib.model.SetDateParams;
import io.appium.espressoserver.lib.model.SetTimeParams;
import io.appium.espressoserver.lib.model.StartActivityParams;
import io.appium.espressoserver.lib.model.TextParams;
import io.appium.espressoserver.lib.model.ToastLookupParams;
Expand Down Expand Up @@ -195,6 +205,12 @@ class Router {
// 'execute mobile' commands
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/swipe", new MobileSwipe(), MobileSwipeParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/is_toast_displayed", new GetToastVisibility(), ToastLookupParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/open_drawer", new DrawerActionHandler(true), DrawerActionParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/close_drawer", new DrawerActionHandler(false), DrawerActionParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/set_date", new SetDate(), SetDateParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/set_time", new SetTime(), SetTimeParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/scroll_to_page", new ScrollToPage(), ScrollToPageParams.class));
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/navigate_to", new NavigateTo(), NavigateToParams.class));

// Not implemented
routeMap.addRoute(new RouteDefinition(Method.POST, "/session/:sessionId/touch/flick", new NotYetImplemented(), AppiumParams.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.espressoserver.lib.model;

import com.google.gson.annotations.SerializedName;

import javax.annotation.Nullable;

@SuppressWarnings("unused")
public class DrawerActionParams extends AppiumParams {
private Integer gravity;

@Nullable
public Integer getGravity() {
return gravity;
}

public void setGravity(Integer gravity) {
this.gravity = gravity;
}

}
Loading