org.openqa.selenium.ContextAware Java Examples

The following examples show how to use org.openqa.selenium.ContextAware. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebDriverUnpackUtility.java    From java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Detects content type by the provided search {@code context}.
 *
 * @param context is an instance of {@link SearchContext}. It may be the instance of
 *                {@link 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 ContextAware} or {@link WrapsDriver} or {@link HasSessionDetails}
 * @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 SearchContext} instance doesn't implement {@link ContextAware} and {@link WrapsDriver}
 */
public static ContentType getCurrentContentType(SearchContext context) {
    return ofNullable(unpackWebDriverFromSearchContext(context)).map(driver -> {
        if (HasSessionDetails.class.isAssignableFrom(driver.getClass())) {
            HasSessionDetails hasSessionDetails = (HasSessionDetails) driver;

            if (!hasSessionDetails.isBrowser()) {
                return NATIVE_MOBILE_SPECIFIC;
            }
        }

        if (ContextAware.class.isAssignableFrom(driver.getClass())) { //it is desktop browser
            ContextAware contextAware = (ContextAware) driver;
            String currentContext = contextAware.getContext();
            if (containsIgnoreCase(currentContext, NATIVE_APP_PATTERN)) {
                return NATIVE_MOBILE_SPECIFIC;
            }
        }

        return HTML_OR_DEFAULT;
    }).orElse(HTML_OR_DEFAULT);
}
 
Example #2
Source File: ListenableObjectTest.java    From java-client with Apache License 2.0 6 votes vote down vote up
@Test public void listenableObjectSample() {
    try {
        ContextAware listenableContextAware =
                getEventFiringObject(contextAware, emptyWebDriver, contextListener, alertListener);
        WebDriver webDriver = listenableContextAware.context("NATIVE_APP");
        assertTrue(contextAwarePredicate.test(listenableContextAware));

        Alert alert = webDriver.switchTo().alert();
        assertTrue(alertPredicate.test(alert));

        assertTrue(webDriverPredicate.test(getEventFiringWebDriver(webDriver, searchingListener)));
    } finally {
        listeners.get(ContextListener.class).messages.clear();
        listeners.get(AlertListener.class).messages.clear();
        listeners.get(SearchingListener.class).messages.clear();
    }
}
 
Example #3
Source File: WebDriverManager.java    From vividus with Apache License 2.0 5 votes vote down vote up
private void switchToContext(ContextAware contextAwareDriver, String contextName)
{
    if (contextAwareDriver.getContextHandles().contains(contextName))
    {
        contextAwareDriver.context(contextName);
    }
    else
    {
        throw new IllegalStateException("MobileDriver doesn't have context: " + contextName);
    }
}
 
Example #4
Source File: SwitchContextAction.java    From xframium-java with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: WebDriverWrapper.java    From bobcat with Apache License 2.0 4 votes vote down vote up
@Override
public WebDriver context(String name) {
  return ((ContextAware) super.getWrappedDriver()).context(name);
}
 
Example #6
Source File: WebDriverWrapper.java    From bobcat with Apache License 2.0 4 votes vote down vote up
@Override
public String getContext() {
  return ((ContextAware) super.getWrappedDriver()).getContext();
}
 
Example #7
Source File: WebDriverWrapper.java    From bobcat with Apache License 2.0 4 votes vote down vote up
@Override
public Set<String> getContextHandles() {
  return ((ContextAware) super.getWrappedDriver()).getContextHandles();
}
 
Example #8
Source File: PERFECTOCloudActionProvider.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
@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() )
    {
        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;
}
 
Example #9
Source File: OpenApplicationAction.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example #10
Source File: SeleniumElement.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the element.
 *
 * @return the element
 */
private WebElement getElement()
{

  if (locatedElement != null)
  {
    if (log.isDebugEnabled())
      log.debug(Thread.currentThread().getName() + ": Element " + getKey() + " Read from cache");
    return locatedElement;
  }

  String currentContext = null;
  if (getWebDriver() instanceof ContextAware)
    currentContext = ((ContextAware) getWebDriver()).getContext();

  Element fromContext = getContext();
  if (fromContext == null && getContextElement() != null && !getContextElement().isEmpty())
    fromContext = getElement(getContextElement());

  ReportableBy useBy = null;

  try
  {
    useBy = useBy();
    if (useBy != null)
    {

      WebElement webElement = null;

      if (useVisualDriver && "VISUAL".equals(getBy().getContext()))
      {
        webElement = ((VisualDriverProvider) getWebDriver()).getVisualDriver().findElement((By) useBy);
      }
      else
      {
        List<WebElement> elementList = null;

        if (fromContext != null)
          elementList = ((WebElement) fromContext.getNative()).findElements((By) useBy);
        else
          elementList = ((WebDriver) getWebDriver()).findElements((By) useBy);

        if (elementList == null || elementList.size() == 0)
          throw new ScriptException("Could not locate elements using " + useBy.toString());

        if (elementList.size() == 1)
          webElement = elementList.get(0);
        else if (elementList.size() > 1 && isAllowMultiple())
          webElement = elementList.get(0);
        else if (elementList.size() > 1 && !isAllowMultiple())
          throw new ScriptConfigurationException(getKey() + " returned multiple values while 1 was expected");

      }

      if (log.isDebugEnabled())
      {
        if (webElement == null)
          log.debug(Thread.currentThread().getName() + ": Could not locate by [" + getBy() + "] using [" + getKey() + "]");
        else
          log.debug(Thread.currentThread().getName() + ": Element " + getKey() + " Located");
      }

      if (isCacheNative())
        locatedElement = webElement;
      return webElement;
    }

    return null;
  }
  finally
  {
    try
    {
      if (useBy != null)
        getExecutionContext().getStep().setLocatorResult(useBy.getResults());
    }
    catch (Exception e)
    {

    }

    if (currentContext != null && getWebDriver() instanceof ContextAware)
      ((ContextAware) getWebDriver()).context(currentContext);
  }

}