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;
}
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());
}
}
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());
}
}
Aggregations