Search in sources :

Example 1 with DefaultElementLocator

use of org.openqa.selenium.support.pagefactory.DefaultElementLocator in project seleniumRobot by bhecquet.

the class SeleniumNativeActions method interceptPageFactoryElementsLocation.

/**
 * Intercept "findElements" action done in LocatingElementListHandler class so that we can override returned element and replace it by HtmlElement
 * This helps handling PageObjectFactory method
 * @param joinPoint
 * @return
 * @throws Throwable
 */
@Around(" this(org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler) && call(public *  org.openqa.selenium.support.pagefactory.ElementLocator+.findElements (..))")
public Object interceptPageFactoryElementsLocation(ProceedingJoinPoint joinPoint) throws Throwable {
    if (doOverride()) {
        DefaultElementLocator locator = ((DefaultElementLocator) joinPoint.getTarget());
        Field byField = DefaultElementLocator.class.getDeclaredField("by");
        byField.setAccessible(true);
        return new HtmlElement("", (By) byField.get(locator), getCurrentFrame()).findElements();
    } else {
        return joinPoint.proceed(joinPoint.getArgs());
    }
}
Also used : Field(java.lang.reflect.Field) DefaultElementLocator(org.openqa.selenium.support.pagefactory.DefaultElementLocator) HtmlElement(com.seleniumtests.uipage.htmlelements.HtmlElement) By(org.openqa.selenium.By) Around(org.aspectj.lang.annotation.Around)

Example 2 with DefaultElementLocator

use of org.openqa.selenium.support.pagefactory.DefaultElementLocator in project seleniumRobot by bhecquet.

the class SeleniumNativeActions method interceptPageFactoryElementLocation.

/**
 * Intercept "findElement" action done in LocatingElementHandler class so that we can override returned element and replace it by HtmlElement
 * This helps handling PageObjectFactory method
 * @param joinPoint
 * @return
 * @throws Throwable
 */
@Around(" this(org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler) && call(public *  org.openqa.selenium.support.pagefactory.ElementLocator+.findElement (..))")
public Object interceptPageFactoryElementLocation(ProceedingJoinPoint joinPoint) throws Throwable {
    if (Boolean.TRUE.equals(doOverride())) {
        DefaultElementLocator locator = ((DefaultElementLocator) joinPoint.getTarget());
        Field byField = DefaultElementLocator.class.getDeclaredField("by");
        byField.setAccessible(true);
        return new HtmlElement("", (By) byField.get(locator), getCurrentFrame());
    } else {
        return joinPoint.proceed(joinPoint.getArgs());
    }
}
Also used : Field(java.lang.reflect.Field) DefaultElementLocator(org.openqa.selenium.support.pagefactory.DefaultElementLocator) HtmlElement(com.seleniumtests.uipage.htmlelements.HtmlElement) Around(org.aspectj.lang.annotation.Around)

Example 3 with DefaultElementLocator

use of org.openqa.selenium.support.pagefactory.DefaultElementLocator in project seleniumRobot by bhecquet.

the class SeleniumNativeActions method getFrameElement.

/**
 * Returns a FrameElement based on the object passed in argument
 * @param frameArg
 * @return
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
private FrameElement getFrameElement(Object frameArg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    FrameElement frameEl = null;
    if (frameArg instanceof HtmlElement) {
        frameEl = new FrameElement("", ((HtmlElement) frameArg).getBy());
    } else if (frameArg instanceof WebElement && frameArg.getClass().getName().contains("Proxy")) {
        LocatingElementHandler locatingEh = (LocatingElementHandler) Proxy.getInvocationHandler(frameArg);
        Field locatorField = LocatingElementHandler.class.getDeclaredField("locator");
        locatorField.setAccessible(true);
        DefaultElementLocator locator = ((DefaultElementLocator) locatorField.get(locatingEh));
        Field byField = DefaultElementLocator.class.getDeclaredField("by");
        byField.setAccessible(true);
        frameEl = new FrameElement("", (By) byField.get(locator));
    } else if (frameArg instanceof By) {
        frameEl = new FrameElement("", (By) frameArg);
    } else if (frameArg instanceof Integer) {
        frameEl = new FrameElement("", By.tagName("iframe"), (Integer) frameArg);
    } else if (frameArg instanceof String) {
        String name = ((String) frameArg).replaceAll("(['\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-/\\[\\]\\(\\)])", "\\\\$1");
        frameEl = new FrameElement("", By.cssSelector("frame[name='" + name + "'],iframe[name='" + name + "'],frame#" + name + ",iframe#" + name));
    }
    return frameEl;
}
Also used : Field(java.lang.reflect.Field) DefaultElementLocator(org.openqa.selenium.support.pagefactory.DefaultElementLocator) LocatingElementHandler(org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler) HtmlElement(com.seleniumtests.uipage.htmlelements.HtmlElement) By(org.openqa.selenium.By) FrameElement(com.seleniumtests.uipage.htmlelements.FrameElement) WebElement(org.openqa.selenium.WebElement)

Aggregations

HtmlElement (com.seleniumtests.uipage.htmlelements.HtmlElement)3 Field (java.lang.reflect.Field)3 DefaultElementLocator (org.openqa.selenium.support.pagefactory.DefaultElementLocator)3 Around (org.aspectj.lang.annotation.Around)2 By (org.openqa.selenium.By)2 FrameElement (com.seleniumtests.uipage.htmlelements.FrameElement)1 WebElement (org.openqa.selenium.WebElement)1 LocatingElementHandler (org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler)1