Search in sources :

Example 1 with InvalidStrategyException

use of io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException in project appium-espresso-driver by appium.

the class Finder method findBy.

///Find By different strategies
private ViewInteraction findBy(Strategy strategy, String selector) throws InvalidStrategyException {
    ViewInteraction matcher;
    switch(strategy) {
        case // with ID
        ID:
            // find id from target context
            Context context = InstrumentationRegistry.getTargetContext();
            int id = context.getResources().getIdentifier(selector, "Id", InstrumentationRegistry.getTargetContext().getPackageName());
            matcher = onView(withId(id));
            break;
        case CLASS_NAME:
            // with class name
            // TODO: improve this finder with instanceOf
            matcher = onView(withClassName(endsWith(selector)));
            break;
        case TEXT:
            // with text
            matcher = onView(withText(selector));
            break;
        case ACCESSIBILITY_ID:
            // with content description
            matcher = onView(withContentDescription(selector));
            break;
        default:
            throw new InvalidStrategyException("Strategy is not implemented: " + strategy.getStrategyName());
    }
    return matcher;
}
Also used : Context(android.content.Context) ViewInteraction(android.support.test.espresso.ViewInteraction) InvalidStrategyException(io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException)

Example 2 with InvalidStrategyException

use of io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException in project appium-espresso-driver by appium.

the class Finder method handle.

@Override
public Element handle(Locator locator) throws AppiumException {
    try {
        if (locator.getUsing() == null) {
            throw new InvalidStrategyException("Locator strategy cannot be empty");
        } else if (locator.getValue() == null) {
            throw new MissingCommandsException("No locator provided");
        }
        // Test the selector
        ViewInteraction matcher = findBy(locator.getUsing(), locator.getValue());
        matcher.check(matches(isDisplayed()));
        // If we have a match, return success
        return new Element(matcher);
    } catch (NoMatchingViewException e) {
        throw new NoSuchElementException("Could not find element with strategy " + locator.getUsing() + " and selector " + locator.getValue());
    }
}
Also used : MissingCommandsException(io.appium.espressoserver.lib.handlers.exceptions.MissingCommandsException) Element(io.appium.espressoserver.lib.model.Element) ViewInteraction(android.support.test.espresso.ViewInteraction) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) InvalidStrategyException(io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException) NoSuchElementException(io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException)

Example 3 with InvalidStrategyException

use of io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException in project appium-espresso-driver by appium.

the class Router method route.

@SuppressWarnings("unchecked")
public BaseResponse route(IHTTPSession session) {
    String uri = session.getUri();
    Method method = session.getMethod();
    // Look for a route that matches this URL
    RouteDefinition matchingRoute = routeMap.findMatchingRoute(method, uri);
    // If no route found, return a 404 Error Response
    if (matchingRoute == null) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_FOUND, String.format("No such route %s", uri));
    }
    // Get the handler, parameter class and URI parameters
    RequestHandler handler = matchingRoute.getHandler();
    Class<? extends AppiumParams> paramClass = matchingRoute.getParamClass();
    Map<String, String> uriParams = matchingRoute.getUriParams(uri);
    // Parse the appium params
    String postJson;
    try {
        postJson = parseBody(session);
    } catch (IOException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    } catch (NanoHTTPD.ResponseException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    }
    AppiumParams appiumParams;
    if (postJson == null) {
        appiumParams = new AppiumParams();
    } else {
        appiumParams = paramClass.cast((new Gson()).fromJson(postJson, paramClass));
    }
    appiumParams.setSessionId(uriParams.get("sessionId"));
    appiumParams.setElementId(uriParams.get("elementId"));
    // Validate the sessionId
    if (appiumParams.getSessionId() != null && !appiumParams.getSessionId().equals(Session.getGlobalSession().getId())) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, "Invalid session ID " + appiumParams.getSessionId());
    }
    // Create the result
    try {
        Object handlerResult = handler.handle(appiumParams);
        String sessionId = appiumParams.getSessionId();
        // If it's a new session, pull out the newly created Session ID
        if (handlerResult != null && handlerResult.getClass() == Session.class) {
            sessionId = ((Session) handlerResult).getId();
        }
        AppiumResponse appiumResponse = new AppiumResponse<>(AppiumStatus.SUCCESS, handlerResult, sessionId);
        System.out.println("Finished processing " + method + " request for '" + uri + "'");
        return appiumResponse;
    } catch (NoSuchElementException e) {
        return new AppiumResponse<>(AppiumStatus.NO_SUCH_ELEMENT, e.getMessage());
    } catch (SessionNotCreatedException e) {
        return new AppiumResponse<>(AppiumStatus.SESSION_NOT_CREATED_EXCEPTION, e.getMessage());
    } catch (InvalidStrategyException e) {
        return new AppiumResponse<>(AppiumStatus.INVALID_SELECTOR, e.getMessage());
    } catch (MissingCommandsException e) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_FOUND, e.getMessage());
    } catch (NotYetImplementedException e) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_IMPLEMENTED, e.getMessage());
    } catch (AppiumException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    }
}
Also used : MissingCommandsException(io.appium.espressoserver.lib.handlers.exceptions.MissingCommandsException) SessionNotCreatedException(io.appium.espressoserver.lib.handlers.exceptions.SessionNotCreatedException) NanoHTTPD(fi.iki.elonen.NanoHTTPD) Gson(com.google.gson.Gson) Method(fi.iki.elonen.NanoHTTPD.Method) IOException(java.io.IOException) ErrorResponse(io.appium.espressoserver.lib.http.response.ErrorResponse) AppiumResponse(io.appium.espressoserver.lib.http.response.AppiumResponse) NotYetImplementedException(io.appium.espressoserver.lib.handlers.exceptions.NotYetImplementedException) RequestHandler(io.appium.espressoserver.lib.handlers.RequestHandler) AppiumException(io.appium.espressoserver.lib.handlers.exceptions.AppiumException) AppiumParams(io.appium.espressoserver.lib.model.AppiumParams) NoSuchElementException(io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException) InvalidStrategyException(io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException) CreateSession(io.appium.espressoserver.lib.handlers.CreateSession) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) DeleteSession(io.appium.espressoserver.lib.handlers.DeleteSession) Session(io.appium.espressoserver.lib.model.Session) GetSession(io.appium.espressoserver.lib.handlers.GetSession)

Aggregations

InvalidStrategyException (io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException)3 ViewInteraction (android.support.test.espresso.ViewInteraction)2 MissingCommandsException (io.appium.espressoserver.lib.handlers.exceptions.MissingCommandsException)2 NoSuchElementException (io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException)2 Context (android.content.Context)1 NoMatchingViewException (android.support.test.espresso.NoMatchingViewException)1 Gson (com.google.gson.Gson)1 NanoHTTPD (fi.iki.elonen.NanoHTTPD)1 IHTTPSession (fi.iki.elonen.NanoHTTPD.IHTTPSession)1 Method (fi.iki.elonen.NanoHTTPD.Method)1 CreateSession (io.appium.espressoserver.lib.handlers.CreateSession)1 DeleteSession (io.appium.espressoserver.lib.handlers.DeleteSession)1 GetSession (io.appium.espressoserver.lib.handlers.GetSession)1 RequestHandler (io.appium.espressoserver.lib.handlers.RequestHandler)1 AppiumException (io.appium.espressoserver.lib.handlers.exceptions.AppiumException)1 NotYetImplementedException (io.appium.espressoserver.lib.handlers.exceptions.NotYetImplementedException)1 SessionNotCreatedException (io.appium.espressoserver.lib.handlers.exceptions.SessionNotCreatedException)1 AppiumResponse (io.appium.espressoserver.lib.http.response.AppiumResponse)1 ErrorResponse (io.appium.espressoserver.lib.http.response.ErrorResponse)1 AppiumParams (io.appium.espressoserver.lib.model.AppiumParams)1