/** * _get context. * * @return the string */ private String _getContext() { if ( webDriver != null ) { try { if ( webDriver instanceof RemoteWebDriver ) { RemoteExecuteMethod executeMethod = new RemoteExecuteMethod( (RemoteWebDriver) webDriver ); return (String) executeMethod.execute( DriverCommand.GET_CURRENT_CONTEXT_HANDLE, null ); } else if ( webDriver instanceof AppiumDriver ) { return ((AppiumDriver) webDriver).getContext(); } } catch ( Exception e ) { log.warn( "Context Switches are not supported - " + e.getMessage() ); contextSwitchSupported = false; } } return null; }
@Override public Object executeScript(String script, Object... args) { if (!getCapabilities().isJavascriptEnabled()) { throw new UnsupportedOperationException( "You must be using an underlying instance of WebDriver that supports executing javascript"); } // Escape the quote marks script = script.replaceAll("\"", "\\\""); Iterable<Object> convertedArgs = Iterables.transform(Lists.newArrayList(args), new WebElementToJsonConverter()); Map<String, ?> params = ImmutableMap.of("script", script, "args", Lists.newArrayList(convertedArgs)); return execute(DriverCommand.EXECUTE_SCRIPT, params).getValue(); }
@Override public Object executeAsyncScript(String script, Object... args) { if (!getCapabilities().isJavascriptEnabled()) { throw new UnsupportedOperationException( "You must be using an underlying instance of " + "WebDriver that supports executing javascript"); } // Escape the quote marks script = script.replaceAll("\"", "\\\""); Iterable<Object> convertedArgs = Iterables.transform(Lists.newArrayList(args), new WebElementToJsonConverter()); Map<String, ?> params = ImmutableMap.of("script", script, "args", Lists.newArrayList(convertedArgs)); return execute(DriverCommand.EXECUTE_ASYNC_SCRIPT, params).getValue(); }
@Override public Response execute(Command command) throws IOException { Response response; if (DriverCommand.QUIT.equals(command.getName())) { response = grid.execute(command); } else { response = node.execute(command); } return response; }
public WebDriver context( String newContext ) { setLastAction(); if ( !contextSwitchSupported ) return webDriver; if ( newContext == null || newContext.equals( currentContext ) ) return webDriver; if ( webDriver != null ) { if ( webDriver instanceof RemoteWebDriver ) { log.info( "Switching context to " + newContext ); RemoteExecuteMethod executeMethod = new RemoteExecuteMethod( (RemoteWebDriver) webDriver ); Map<String, String> params = new HashMap<String, String>( 5 ); params.put( "name", newContext ); executeMethod.execute( DriverCommand.SWITCH_TO_CONTEXT, params ); } else if ( webDriver instanceof AppiumDriver ) { log.info( "Switching context to " + newContext ); ((AppiumDriver) webDriver).context( newContext ); } else return null; if ( newContext.equals( _getContext() ) ) currentContext = newContext; else throw new IllegalStateException( "Could not change context to " + newContext + " against " + webDriver ); } return webDriver; }
public Set<String> getContextHandles() { setLastAction(); if ( contextHandles != null ) return contextHandles; RemoteExecuteMethod executeMethod = new RemoteExecuteMethod( (RemoteWebDriver) webDriver ); List<String> handleList = (List<String>) executeMethod.execute( DriverCommand.GET_CONTEXT_HANDLES, null ); contextHandles = new HashSet<String>( 10 ); contextHandles.addAll( handleList ); return contextHandles; }
private static void switchToContext(RemoteWebDriver driver, String context) { RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(driver); Map<String,String> params = new HashMap<>(); params.put("name", context); executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params); }
private static String getCurrentContextHandle(RemoteWebDriver driver) { RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(driver); String context = (String) executeMethod.execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE, null); return context; }
@Override public Set<String> getContextHandles() { Response response = execute(DriverCommand.GET_CONTEXT_HANDLES); Object value = response.getValue(); try { List<String> returnedValues = (List<String>) value; return new LinkedHashSet<>(returnedValues); } catch (ClassCastException ex) { throw new WebDriverException( "Returned value cannot be converted to List<String>: " + value, ex); } }
@Override public String getContext() { String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); if (contextName.equals("null")) { return null; } return contextName; }
@Override public ScreenOrientation getOrientation() { Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION); String orientation = response.getValue().toString().toLowerCase(); if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) { return ScreenOrientation.LANDSCAPE; } else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) { return ScreenOrientation.PORTRAIT; } else { throw new WebDriverException("Unexpected orientation returned: " + orientation); } }
@Override public Response execute(Command command) throws IOException, WebDriverException { if (DriverCommand.NEW_SESSION.equals(command.getName()) && service != null) { service.start(); } try { return super.execute(command); } catch (Throwable t) { Throwable rootCause = Throwables.getRootCause(t); if (rootCause instanceof ConnectException && rootCause.getMessage().contains("Connection refused") && service != null) { if (service.isRunning()) { throw new WebDriverException("The session is closed!", t); } if (!service.isRunning()) { throw new WebDriverException("The appium server has accidentally died!", t); } } Throwables.propagateIfPossible(t); throw new WebDriverException(t); } finally { if (DriverCommand.QUIT.equals(command.getName()) && service != null) { service.stop(); } } }
@Override protected Response execute(String driverCommand, Map<String, ?> parameters) { if (driverCommand.equalsIgnoreCase(DriverCommand.QUIT)) { return new Response(); } return super.execute(driverCommand, parameters); }
@Override public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { Object takeScreenshot = getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT); if (null == takeScreenshot || (Boolean) takeScreenshot) { String base64Str = execute(DriverCommand.SCREENSHOT).getValue().toString(); return target.convertFromBase64Png(base64Str); } return null; }
public <X> X getScreenshotAs(final OutputType<X> target) throws WebDriverException { if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) { String output = execute(DriverCommand.SCREENSHOT).getValue().toString(); return target.convertFromBase64Png(output); } return null; }
public boolean tapById(String id) { try{ Map<String, ?> params = ImmutableMap.of("element", ((RemoteWebElement)driver.findElement(By.id(id))).getId()); ((RobotRemoteWebDriver)this.driver).execute(DriverCommand.TOUCH_SINGLE_TAP, params); }catch(Exception ex) { return false; } return true; }
public boolean tapByXPath(String xpath) { try { Map<String, ?> params = ImmutableMap.of("element", ((RemoteWebElement)driver.findElement(By.xpath(xpath))).getId()); ((RobotRemoteWebDriver)this.driver).execute(DriverCommand.TOUCH_SINGLE_TAP, params); }catch(Exception ex) { return false; } return true; }
public boolean tap(By by) { try { Map<String, ?> params = ImmutableMap.of("element", ((RemoteWebElement)driver.findElement(by)).getId()); ((RobotRemoteWebDriver)this.driver).execute(DriverCommand.TOUCH_SINGLE_TAP, params); }catch(Exception ex) { return false; } return true; }
@Override public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { try { if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) { String base64Str = execute(DriverCommand.SCREENSHOT).getValue().toString(); return target.convertFromBase64Png(base64Str); } return null; } catch (Exception e) { return null; } }
public Response sendGet(String url) { return execute(DriverCommand.GET, ImmutableMap.of("url", url)); }
@Override public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { // Get the screenshot as base64. String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue(); // ... and convert it. return target.convertFromBase64Png(base64); }
public static void switchToContext(String context) { RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(getQAFDriver()); Map<String, String> params = new HashMap<String, String>(); params.put("name", context); executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params); }
/** * @return the current context - "NATIVE_APP", "WEBVIEW", "VISUAL" */ public static String getCurrentContext() { RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(getQAFDriver()); return (String) executeMethod.execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE, null); }
@Override public WebDriver context(String name) { checkNotNull(name, "Must supply a context name"); execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name)); return this; }
@Override public void rotate(ScreenOrientation orientation) { execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation.value().toUpperCase())); }
public void beforeEvent(Command command) { if (command.getName().equalsIgnoreCase(DriverCommand.CLICK)) { System.out.println("Before click"); } }
public void afterEvent(Command command) { if (command.getName().equalsIgnoreCase(DriverCommand.CLICK)) { System.out.println("After click"); } }
/** * Get screenshot from device. Will store a copy of the screenshot in parent working directory and in test-cloud * it will be inserted into test report. * @param outputType output format of screenshot * @param <X> output format of screenshot * @return screenshot * @throws WebDriverException if an error occurs * @see #label(String) for the perfered way to insert screenshots in test reports. */ @Override public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException { Object value = execute(DriverCommand.SCREENSHOT).getValue(); return DriverHelper.getScreenshotToWorkspace(value, outputType, path -> eventReporter.reportScreenshot(path.toAbsolutePath().toString(), 0, false)); }
/** * Get screenshot from device. Will store a copy of the screenshot in parent working directory and in test-cloud * it will be inserted into test report. * @param outputType output format of screenshot * @param <X> output format of screenshot * @return screenshot * @throws WebDriverException if an error occurs * @see #label(String) for the perfered way to insert screenshots in test reports. */ @Override public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException { return DriverHelper.getScreenshotToWorkspace(execute(DriverCommand.SCREENSHOT).getValue(), outputType, path -> eventReporter.reportScreenshot(path.toAbsolutePath().toString(), 0, false)); }