/** * Create mocked {@link WebElement} object. * * @param type element type * @param value element value * @param isCheckbox 'true' is checkbox is desired; otherwise 'false' * @return mocked WebElement object */ private static WebElement mockElement(String type, String value, boolean isCheckbox) { WebElement element = mock(WebElement.class, withSettings().extraInterfaces(WrapsDriver.class)); when(element.getTagName()).thenReturn(type); if (isCheckbox) { when(element.getAttribute("type")).thenReturn("checkbox"); when(element.getAttribute("value")).thenReturn("isSelected: " + value); when(element.isSelected()).thenReturn(Boolean.parseBoolean(value)); } else { when(element.getAttribute("type")).thenReturn("text"); when(element.getAttribute("value")).thenReturn(value); when(element.isSelected()).thenReturn(false); } WebDriver driver = mockDriver(); when(((WrapsDriver) element).getWrappedDriver()).thenReturn(driver); return element; }
@Test public void constructor_WrapsDriver_Test() { // null ManagedWebDriverContext managedWebDriverContext = new ManagedWebDriverContext((WrapsDriver) null); assertEquals(WebDriverManager.getDriverContext(), managedWebDriverContext.getDriverContext()); assertEquals(WebDriverManager.getDriverContext().getWrappedDriver(), managedWebDriverContext.getDriverContext().getWrappedDriver()); // non-null WebDriver webDriver = Mock.getWebDriver(); WrapsDriver wrapsDriver = Mock.wrapsDriver(webDriver); managedWebDriverContext = new ManagedWebDriverContext(wrapsDriver); assertEquals(wrapsDriver, managedWebDriverContext.getDriverContext()); assertEquals(webDriver, managedWebDriverContext.getDriverContext().getWrappedDriver()); }
@Test public void setAndGetDriverContextTest() { ManagedWebDriverContext managedWebDriverContext = new ManagedWebDriverContext(); managedWebDriverContext.setDriverContext(null); assertNull(managedWebDriverContext.getDriverContext()); WebDriver webDriver = Mock.getWebDriver(); WrapsDriver wrapsDriver = Mock.wrapsDriver(webDriver); managedWebDriverContext.setDriverContext(wrapsDriver); assertEquals(wrapsDriver, managedWebDriverContext.getDriverContext()); assertEquals(webDriver, managedWebDriverContext.getDriverContext().getWrappedDriver()); assertEquals(webDriver, managedWebDriverContext.getWrappedDriver()); }
@Test public void toStringTest() { ManagedWebDriverContext managedWebDriverContext = new ManagedWebDriverContext(Mock.wrapsDriver(Mock.getWebDriver())); assertEquals("ManagedWebDriverContext [driverContext=" + managedWebDriverContext.getDriverContext() + "]", managedWebDriverContext.toString()); managedWebDriverContext = new ManagedWebDriverContext((WrapsDriver) null); assertEquals("ManagedWebDriverContext [driverContext=" + managedWebDriverContext.getDriverContext() + "]", managedWebDriverContext.toString()); managedWebDriverContext = new ManagedWebDriverContext(Mock.getWebDriver()); assertEquals("ManagedWebDriverContext [driverContext=" + managedWebDriverContext.getDriverContext() + "]", managedWebDriverContext.toString()); managedWebDriverContext = new ManagedWebDriverContext((WebDriver) null); assertEquals("ManagedWebDriverContext [driverContext=" + managedWebDriverContext.getDriverContext() + "]", managedWebDriverContext.toString()); managedWebDriverContext = new ManagedWebDriverContext(); assertEquals("ManagedWebDriverContext [driverContext=" + managedWebDriverContext.getDriverContext() + "]", managedWebDriverContext.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; }
/** * Obtains executor based on Selenium context. * @param searchContext context in which javascript should be executed. * @return JavascriptExecutor based on search context. * @throws IllegalArgumentException when no executor could be obtained from context. */ public static JavascriptExecutor getJavascriptExecutor(SearchContext searchContext) { JavascriptExecutor executor = null; if (searchContext instanceof JavascriptExecutor) { executor = (JavascriptExecutor) searchContext; } else { if (searchContext instanceof WrapsDriver) { WrapsDriver wraps = (WrapsDriver) searchContext; WebDriver wrapped = wraps.getWrappedDriver(); if (wrapped instanceof JavascriptExecutor) { executor = (JavascriptExecutor) wrapped; } } } if (executor == null) { throw new IllegalArgumentException("Unable to get: " + JavascriptExecutor.class.getName() + " from: " + searchContext.getClass().getName()); } return executor; }
@Override public boolean apply(final WebElement element) { try { WebDriver driver = ((WrapsDriver) element).getWrappedDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; Boolean jQuery = (Boolean) js.executeScript("return (typeof jQuery !== 'undefined');"); if (!jQuery) { logger.warn("Unable to apply the EventListenerRegisteredCondition as jQuery is undefined."); return true; } @SuppressWarnings("unchecked") Map<String, Object> registeredListeners = (Map<String, Object>) js.executeScript("return $._data($(arguments[0])[0], 'events')", element); if (registeredListeners == null) return false; for (Map.Entry<String, Object> listener : registeredListeners.entrySet()) { logger.debug("DomEvent listener [" + listener.getKey() + "] registered."); if (listener.getKey().equals(event.getName())) { return true; } } return false; } catch (NullPointerException | WebDriverException e) { return false; } }
/** * Get the driver associated with the specified search context * * @param context search context * @return search context driver */ public static WebDriver getDriver(SearchContext context) { if (context instanceof WebDriver) { return (WebDriver) context; } else if (context instanceof WrapsDriver) { return ((WrapsDriver) context).getWrappedDriver(); } else { throw new UnsupportedOperationException("Unable to extract the driver from the specified context"); } }
public ManagedWebDriverContext(WrapsDriver driverContext) { if (driverContext == null) { this.driverContext = WebDriverManager.getDriverContext(); } else { this.driverContext = driverContext; } }
public ManagedWebDriverContext(WebDriver webDriver) { if (webDriver == null) { this.driverContext = WebDriverManager.getDriverContext(); } else { this.driverContext = new WrapsDriver() { @Override public WebDriver getWrappedDriver() { return webDriver; } }; } }
@Test public void constructor_WrapsDriver_String_Test() { AbstractPageObjects abstractPageObjects = new AbstractPageObjects((WrapsDriver) null, null); assertEquals(null, abstractPageObjects.getUrl()); abstractPageObjects = new AbstractPageObjects((WrapsDriver) null, "test"); assertEquals("test", abstractPageObjects.getUrl()); }
public static WrapsDriver wrapsDriver(WebDriver webDriver) { return new WrapsDriver() { @Override public WebDriver getWrappedDriver() { return webDriver; } }; }
/** * Look at * {@link net.sf.cglib.proxy.MethodInterceptor#intercept(Object, Method, Object[], MethodProxy)} */ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (Object.class.equals(method.getDeclaringClass())) { return proxy.invokeSuper(obj, args); } if (WrapsDriver.class.isAssignableFrom(method.getDeclaringClass()) && method.getName() .equals("getWrappedDriver")) { return driver; } WebElement realElement = locator.findElement(); return getObject(realElement, method, args); }
protected WebDriver getUnwrappedDriver() { WebDriver toReturn = getDriver(); while (toReturn instanceof WrapsDriver) { toReturn = ((WrapsDriver) toReturn).getWrappedDriver(); } return toReturn; }
public static void highlightElement(final WebElement element) { for (int i = 0; i < 5; i++) { final WrapsDriver wrappedElement = (WrapsDriver) element; final JavascriptExecutor driver = (JavascriptExecutor) wrappedElement.getWrappedDriver(); driver.executeScript( "arguments[0].setAttribute('style', arguments[1])", element, "visibility:hidden;"); // driver.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); } }
@Override public boolean apply(final WebElement element) { try { WebDriver driver = ((WrapsDriver) element).getWrappedDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; return (Boolean) js .executeScript( "var rect = arguments[0].getBoundingClientRect();" + "return (rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth));", element); } catch (NullPointerException | WebDriverException e) { return false; } }
private <T> T takeScreenShotOnBrowser(WebDriver driver, OutputType<T> outType) throws CrawljaxException { if (driver instanceof TakesScreenshot) { T screenshot = ((TakesScreenshot) driver).getScreenshotAs(outType); removeCanvasGeneratedByFirefoxDriverForScreenshots(); return screenshot; } else if (driver instanceof RemoteWebDriver) { WebDriver augmentedWebdriver = new Augmenter().augment(driver); return takeScreenShotOnBrowser(augmentedWebdriver, outType); } else if (driver instanceof WrapsDriver) { return takeScreenShotOnBrowser(((WrapsDriver) driver).getWrappedDriver(), outType); } else { throw new CrawljaxException("Your current WebDriver doesn't support screenshots."); } }
@Test public void shouldWrapDriver() { Browser browser = new WebDriverBrowser(mock(TargetedWebDriver.class), new StubWebDriverParentContext(), new StubWebDriverElementContext()); assertThat(browser, instanceOf(WrapsDriver.class)); }
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()]); }
public JSApplicationCache(WrapsDriver driverContext) { super(driverContext); }
public JSLocalStorage(WrapsDriver driverContext) { super(driverContext); }
public JSSessionStorage(WrapsDriver driverContext) { super(driverContext); }
public JSWebStorage(WrapsDriver driverContext) { super(driverContext); }
public static WrapsDriver getDriverContext() { return driverContext; }
protected WrapsDriver getDriverContext() { return driverContext; }
protected void setDriverContext(WrapsDriver driverContext) { this.driverContext = driverContext; }
public AbstractPageObjects(WrapsDriver driverContext, String url) { super(driverContext); this.url = url; }
public AbstractLoginPageObjects(WrapsDriver driverContext, String url) { super(driverContext, url); }
@Test public void toStringTest() { assertEquals("Page(test)", new AbstractPageObjects("test").toString()); assertEquals("Page(test)", new AbstractPageObjects((WebDriver) null, "test").toString()); assertEquals("Page(test)", new AbstractPageObjects((WrapsDriver) null, "test").toString()); }
public static File captureElementBitmap(final WebElement element) throws IOException { final WrapsDriver wrapsDriver = (WrapsDriver) element; final File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE); final BufferedImage img = ImageIO.read(screen); final int width = element.getSize().getWidth(); final int height = element.getSize().getHeight(); final Rectangle rect = new Rectangle(width, height); final Point p = element.getLocation(); final BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height); ImageIO.write(dest, "png", screen); return screen; }
public static WebDriver unwrapDriver(final WebElement element) { return ((WrapsDriver) element).getWrappedDriver(); }
@Override protected TouchActions newActions(WebElement elem) { return new TouchActions(((WrapsDriver) elem).getWrappedDriver()); }
/** * New actions. * * @param elem the elem * @return the actions */ protected Actions newActions(WebElement elem) { return new Actions(((WrapsDriver) elem).getWrappedDriver()); }