@Override public boolean _executeAction( WebDriver webDriver, List<Object> parameterList ) { String contextName = (String) parameterList.get( 0 ); if ( webDriver instanceof ContextAware ) { ( (ContextAware) webDriver ).context( contextName ); } return true; }
@Override public WebDriver context(String name) { return ((ContextAware) super.getWrappedDriver()).context(name); }
@Override public String getContext() { return ((ContextAware) super.getWrappedDriver()).getContext(); }
@Override public Set<String> getContextHandles() { return ((ContextAware) super.getWrappedDriver()).getContextHandles(); }
@Override public boolean openApplication( String applicationName, DeviceWebDriver webDriver, String xFID ) { ApplicationDescriptor appDesc = ApplicationRegistry.instance(xFID).getApplication( applicationName ); if ( appDesc == null ) throw new ScriptConfigurationException( "The Application " + applicationName + " does not exist" ); if ( appDesc.isWeb() ) { //String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t"); //if ( webDriver.getWindowHandles() != null && webDriver.getWindowHandles().size() > 0 ) // webDriver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab); webDriver.get( appDesc.getUrl() ); } else { Handset localDevice = PerfectoMobile.instance( xFID ).devices().getDevice( webDriver.getPopulatedDevice().getDeviceName() ); Execution appExec = null; String[] applicationArray = null; if ( localDevice.getOs().toLowerCase().equals( "ios" ) ) applicationArray = appDesc.getAppleIdentifier().split( ";" ); else if ( localDevice.getOs().toLowerCase().equals( "android" ) ) applicationArray = appDesc.getAndroidIdentifier().split( ";" ); else throw new ScriptException( "Could not OPEN application to " + localDevice.getOs() ); for ( String appId : applicationArray ) { log.info( "Attempting to launch [" + appId + "] on [" + localDevice.getOs() + "]" ); appExec = PerfectoMobile.instance( xFID).application().open( webDriver.getExecutionId(), webDriver.getPopulatedDevice().getDeviceName(), appDesc.getName(), appId ); if ( appExec != null ) { if ( appExec.getStatus().toLowerCase().equals( "success" ) ) { if ( webDriver instanceof ContextAware ) ( ( ContextAware ) webDriver ).context( "NATIVE_APP" ); return true; } } } throw new ScriptException( "Failed to launch application " + appDesc.getName() ); } return true; }
@Override public boolean _executeAction( WebDriver webDriver, List<Object> parameterList ) { String executionId = getExecutionId( webDriver ); String deviceName = getDeviceName( webDriver ); String applicationName = (String) parameterList.get( 0 ); ApplicationDescriptor appDesc = ApplicationRegistry.instance( ( (DeviceWebDriver) webDriver).getxFID() ).getApplication( applicationName ); if ( appDesc == null ) throw new ScriptConfigurationException( "The Application " + applicationName + " does not exist" ); if ( appDesc.isWeb() ) { String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t"); if ( webDriver.getWindowHandles() != null && webDriver.getWindowHandles().size() > 0 ) webDriver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab); webDriver.get( appDesc.getUrl() ); ( (DeviceWebDriver) webDriver ).setAut( appDesc, ( (DeviceWebDriver) webDriver ).getxFID() ); } else { Handset localDevice = PerfectoMobile.instance( ( (DeviceWebDriver) webDriver ).getxFID() ).devices().getDevice( deviceName ); Execution appExec = null; if ( localDevice.getOs().toLowerCase().equals( "ios" ) ) appExec = PerfectoMobile.instance( ( (DeviceWebDriver) webDriver ).getxFID() ).application().open( executionId, deviceName, appDesc.getName(), appDesc.getAppleIdentifier() ); else if ( localDevice.getOs().toLowerCase().equals( "android" ) ) appExec = PerfectoMobile.instance( ( (DeviceWebDriver) webDriver ).getxFID() ).application().open( executionId, deviceName, appDesc.getName(), appDesc.getAndroidIdentifier() ); else throw new IllegalArgumentException( "Could not install application to " + localDevice.getOs() ); if ( appExec != null ) { if ( appExec.getStatus().toLowerCase().equals( "success" ) ) { if ( webDriver instanceof ContextAware ) ( ( ContextAware ) webDriver ).context( "NATIVE_APP" ); ( (DeviceWebDriver) webDriver ).setAut( appDesc, ( (DeviceWebDriver) webDriver ).getxFID() ); return true; } else throw new ScriptException( "Failed to launch application " + appDesc.getName() ); } else throw new ScriptException( "Failed to launch application " + appDesc.getName() ); } return true; }
/** * @param context is an instance of {@link org.openqa.selenium.SearchContext} * It may be the instance of {@link org.openqa.selenium.WebDriver} * or {@link org.openqa.selenium.WebElement} or some other user's * extension/implementation. * Note: if you want to use your own implementation then it should * implement {@link org.openqa.selenium.ContextAware} or * {@link org.openqa.selenium.internal.WrapsDriver} * @return current content type. It depends on current context. If current context is * NATIVE_APP it will return * {@link ContentType#NATIVE_MOBILE_SPECIFIC}. * {@link ContentType#HTML_OR_DEFAULT} will be returned * if the current context is WEB_VIEW. * {@link ContentType#HTML_OR_DEFAULT} also will be * returned if the given {@link org.openqa.selenium.SearchContext} * instance doesn't implement * {@link org.openqa.selenium.ContextAware} and {@link org.openqa.selenium.internal.WrapsDriver} */ public static ContentType getCurrentContentType(SearchContext context) { WebDriver driver = WebDriverUnpackUtility.unpackWebDriverFromSearchContext(context); if (!ContextAware.class.isAssignableFrom(driver.getClass())) { //it is desktop browser return ContentType.HTML_OR_DEFAULT; } ContextAware contextAware = ContextAware.class.cast(driver); String currentContext = contextAware.getContext(); if (currentContext.contains(NATIVE_APP_PATTERN)) { return ContentType.NATIVE_MOBILE_SPECIFIC; } return ContentType.HTML_OR_DEFAULT; }