/** * Returns a 'wait' proxy that determines if the specified element reference has gone stale. * * @param element the element to wait for * @return 'false' if the element reference is still valid; otherwise 'true' */ public static Coordinator<Boolean> stalenessOf(final WebElement element) { return new Coordinator<Boolean>() { private final ExpectedCondition<Boolean> condition = conditionInitializer(); // initializer for [condition] field private final ExpectedCondition<Boolean> conditionInitializer() { if (element instanceof WrapsElement) { return ExpectedConditions.stalenessOf(((WrapsElement) element).getWrappedElement()); } else { return ExpectedConditions.stalenessOf(element); } } @Override public Boolean apply(SearchContext ignored) { return condition.apply(null); } @Override public String toString() { return condition.toString(); } }; }
/** * This method extract an instance of {@link org.openqa.selenium.WebDriver} from the given * {@link org.openqa.selenium.SearchContext}. * @param searchContext 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.internal.WrapsDriver} or * {@link org.openqa.selenium.internal.WrapsElement} * @return the instance of {@link org.openqa.selenium.WebDriver}. * Note: if the given {@link org.openqa.selenium.SearchContext} is not * {@link org.openqa.selenium.WebDriver} and it doesn't implement * {@link org.openqa.selenium.internal.WrapsDriver} or * {@link org.openqa.selenium.internal.WrapsElement} then this method returns * null. * */ public static WebDriver unpackWebDriverFromSearchContext(SearchContext searchContext) { if (searchContext instanceof WebDriver) { return (WebDriver) searchContext; } if (searchContext instanceof WrapsDriver) { return unpackWebDriverFromSearchContext( ((WrapsDriver) searchContext).getWrappedDriver()); } // Search context it is not only Webdriver. Webelement is search context // too. // RemoteWebElement and MobileElement implement WrapsDriver if (searchContext instanceof WrapsElement) { return unpackWebDriverFromSearchContext( ((WrapsElement) searchContext).getWrappedElement()); } return null; }
@Override public Boolean call() throws Exception { WebElement one = getElement(); WebElement two = getKnownElements().get(otherId); // Unwrap the elements, if necessary if (one instanceof WrapsElement) { one = ((WrapsElement) one).getWrappedElement(); } if (two instanceof KnownElements.ProxiedElement) { two = ((KnownElements.ProxiedElement) two).getWrappedElement(); } return one.equals(two); }
@Override public WebElement apply(final WebElement element) { return (WebElement) newProxyInstance( getClass().getClassLoader(), new Class[]{WebElement.class, WrapsElement.class, Locatable.class, HasIdentity.class}, invocationHandlerFor(element) ); }
@Test(enabled = false) public void testGetWrappedElementOfTransformedElement() { final WebElement element = givenElement(); final WebElement transformedElement = transformer.apply(element); assertThat(((WrapsElement) transformedElement).getWrappedElement(), sameInstance(element)); }
protected ExtendedWebElement proxyForLocator(ClassLoader loader, Field field, ElementLocator locator) { InvocationHandler handler = new LocatingElementHandler(locator); WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class }, handler); return new ExtendedWebElement(proxy, field.getName(), field.isAnnotationPresent(FindBy.class) ? new LocalizedAnnotations(field).buildBy() : null, webDriver); }
protected Class<?>[] extractInterfaces(Object object) { Set<Class<?>> allInterfaces = new HashSet<Class<?>>(); allInterfaces.add(WrapsDriver.class); if (object instanceof WebElement) { allInterfaces.add(WrapsElement.class); } extractInterfaces(allInterfaces, object.getClass()); return allInterfaces.toArray(new Class<?>[allInterfaces.size()]); }
private WebElement getWebElementFromFactory(ElementLocatorFactory factory) { InvocationHandler handler = new LocatingElementHandler( ((ParentElementLocatorProvider) factory).getCurrentScope()); return (WebElement) Proxy.newProxyInstance(WebElement.class.getClassLoader(), new Class[] {WebElement.class, WrapsElement.class, Locatable.class}, handler); }