Example usage for org.openqa.selenium ContextAware getContext

List of usage examples for org.openqa.selenium ContextAware getContext

Introduction

In this page you can find the example usage for org.openqa.selenium ContextAware getContext.

Prototype

String getContext();

Source Link

Document

Return an opaque handle to this context that uniquely identifies it within this driver instance.

Usage

From source file:com.mengge.pagefactory.utils.WebDriverUnpackUtility.java

License:Apache License

/**
 * @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// w  ww  .  ja va 2s.  c  o  m
 * {@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;
}