/** * This method decorates the field with the generated value. It should not be used directly. * Selenium's * PageFactory calls this method. If the field has Inject annotation, Selenium will ignore it. * Otherwise * Selenium will try to generate the value for this field. * * @param loader ClassLoader * @param field Field to be initialized * @return decorated field Object */ @Override public Object decorate(ClassLoader loader, Field field) { if (field.isAnnotationPresent(Inject.class)) { return null; } else { Object decoratedField = super.decorate(loader, field); if (decoratedField instanceof WebElement) { WebElement element = (WebElement) decoratedField; Locatable locatable = (Locatable) decoratedField; List<ConditionContext> fieldConditionContext = LoadableComponentsUtil.getConditionsFormField(field); BobcatWebElementContext context = new BobcatWebElementContext(element, locatable, fieldConditionContext); return bobcatWebElementFactory.create(context); } return decoratedField; } }
/** * @return Point in the middle of the drop area. */ @Override public Point getCurrentLocation() { Point inViewPort = null; switcher.switchTo(getFramePath()); try { Dimension size = dropArea.getSize(); inViewPort = ((Locatable) dropArea).getCoordinates().inViewPort() .moveBy(size.getWidth() / 2, size.getHeight() / 2); } finally { switcher.switchBack(); } return inViewPort; }
@Override public Void call() throws Exception { Mouse mouse = ((HasInputDevices) getDriver()).getMouse(); Coordinates elementLocation = null; if (elementProvided) { WebElement element = getKnownElements().get(elementId); elementLocation = ((Locatable) element).getCoordinates(); } if (offsetsProvided) { mouse.mouseMove(elementLocation, xOffset, yOffset); } else { mouse.mouseMove(elementLocation); } return null; }
public Tracker center() { return new Tracker() { private Point center; public Point target(Point currentLocation) { if (center == null) { prober.check(new ElementProbe(AsyncElementDriver.this) { @Override protected void probe(WebElement e) { Point p = ((Locatable)e).getLocationOnScreenOnceScrolledIntoView(); Dimension d = ((RenderedWebElement)e).getSize(); center = new Point(p.x + d.width/2, p.y + d.height/2); } }); } return center; } }; }
/** * Scroll to element (applied only for desktop). */ @Deprecated public void scrollTo() { if (Configuration.getDriverType().equals(SpecialKeywords.MOBILE)) { LOGGER.debug("scrollTo javascript is unsupported for mobile devices!"); return; } try { Locatable locatableElement = (Locatable) findElement(EXPLICIT_TIMEOUT); //[VD] onScreen should be updated onto onPage as only 2nd one returns real coordinates without scrolling... read below material for details //https://groups.google.com/d/msg/selenium-developers/nJR5VnL-3Qs/uqUkXFw4FSwJ //[CB] onPage -> inViewPort //https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/remote/RemoteWebElement.java?r=abc64b1df10d5f5d72d11fba37fabf5e85644081 int y = locatableElement.getCoordinates().inViewPort().getY(); int offset = R.CONFIG.getInt("scroll_to_element_y_offset"); ((JavascriptExecutor) getDriver()).executeScript("window.scrollBy(0," + (y - offset) + ");"); } catch (Exception e) { // TODO: calm error logging as it is too noisy //LOGGER.debug("Scroll to element: " + getName() + " not performed!" + e.getMessage()); } }
@Override public WebElement apply(final WebElement element) { return (WebElement) newProxyInstance( getClass().getClassLoader(), new Class[]{WebElement.class, WrapsElement.class, Locatable.class, HasIdentity.class}, invocationHandlerFor(element) ); }
/** * * @param webElement web element * @param locatable locatable * @param contextList internal context */ public BobcatWebElementContext(WebElement webElement, Locatable locatable, List<ConditionContext> contextList) { this.webElement = webElement; this.locatable = locatable; this.contextList = contextList; }
private Point getCurrentLocation() { Point inViewPort = null; switcher.switchTo(getFramePath()); try { Dimension size = webElement.getSize(); inViewPort = ((Locatable) webElement).getCoordinates().inViewPort() .moveBy(size.getWidth() / 2, size.getHeight() / 2); } finally { switcher.switchBack(); } return inViewPort; }
@Override public Coordinates getCoordinates() { if ( webElement instanceof Locatable ) { if ( cachedCoordinates == null ) cachedCoordinates = ( (Locatable) webElement ).getCoordinates(); return cachedCoordinates; } else return null; }
@Override public Coordinates getCoordinates() { try { return ((Locatable)delegate).getCoordinates(); } catch (StaleElementReferenceException e) { reLocateElement(); return getCoordinates(); } }
/** * Forces a mouseOver event on the WebElement. */ public void mouseOver() { TestLogging.log("MouseOver " + this.toString()); findElement(); // build and perform the mouseOver with Advanced User Interactions API // Actions builder = new Actions(driver); // builder.moveToElement(element).build().perform(); final Locatable hoverItem = (Locatable) element; final Mouse mouse = ((HasInputDevices) driver).getMouse(); mouse.mouseMove(hoverItem.getCoordinates()); }
@Override public Void call() throws Exception { TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch(); WebElement element = getKnownElements().get(elementId); Coordinates elementLocation = ((Locatable) element).getCoordinates(); touchScreen.singleTap(elementLocation); return null; }
@Override public Void call() throws Exception { TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch(); if (elementId != null) { WebElement element = getKnownElements().get(elementId); Coordinates elementLocation = ((Locatable) element).getCoordinates(); touchScreen.flick(elementLocation, xOffset, yOffset, speed); } else { touchScreen.flick(xSpeed, ySpeed); } return null; }
@Override public Void call() throws Exception { TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch(); WebElement element = getKnownElements().get(elementId); Coordinates elementLocation = ((Locatable) element).getCoordinates(); touchScreen.doubleTap(elementLocation); return null; }
@Override public Void call() throws Exception { TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch(); WebElement element = getKnownElements().get(elementId); Coordinates elementLocation = ((Locatable) element).getCoordinates(); touchScreen.longPress(elementLocation); return null; }
@Override public Void call() throws Exception { TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch(); if (elementId != null) { WebElement element = getKnownElements().get(elementId); Coordinates elementLocation = ((Locatable) element).getCoordinates(); touchScreen.scroll(elementLocation, xOffset, yOffset); } else { touchScreen.scroll(xOffset, yOffset); } return null; }
private WebElement proxyElement(final WebElement element, final String id) { InvocationHandler handler = new InvocationHandler() { public Object invoke(Object object, Method method, Object[] objects) throws Throwable { if ("getId".equals(method.getName())) { return id; } else if ("getWrappedElement".equals(method.getName())) { return element; } else { try { return method.invoke(element, objects); } catch (InvocationTargetException e){ throw e.getTargetException(); } } } }; Class<?>[] proxyThese; if (element instanceof Locatable) { proxyThese = new Class[] {WebElement.class, ProxiedElement.class, Locatable.class}; } else { proxyThese = new Class[] {WebElement.class, ProxiedElement.class}; } return (WebElement) Proxy.newProxyInstance(element.getClass().getClassLoader(), proxyThese, handler); }
public void hoverOverGraphPointAtXAxisPosition(int pointNumber) { int xRect = ((Locatable) rectElement).getCoordinates().inViewPort().getX(); int yRect = ((Locatable) rectElement).getCoordinates().inViewPort().getY(); int xHoverPoint = xRect + getPlotOffset().getX() + getPlotPoint(pointNumber).getX(); int yHoverPoint = yRect + getPlotOffset().getY() + getPlotPoint(pointNumber).getY(); //For browsers not supporting native events javascript.callEmbeddedSelenium(driver, "triggerEvent", elementToHoverOver, "mouseover"); //For browsers supporting native events xHoverPoint = xHoverPoint - ((Locatable) elementToHoverOver).getCoordinates().inViewPort().getX(); yHoverPoint = yHoverPoint - ((Locatable) elementToHoverOver).getCoordinates().inViewPort().getY(); performAction.moveToElement(plotLine).moveToElement(elementToHoverOver, xHoverPoint, yHoverPoint).perform(); }
/** * * @return */ @Override public Coordinates getCoordinates() { return new RetryUntilTimeout<Coordinates>() { @Override Coordinates commandsToRun() { return ((Locatable) element).getCoordinates(); } }.run(); }
@Test public void shouldMatchConditionWhenElementPositionUnchanged() { when(((Locatable) mockElement).getCoordinates()).thenReturn(mockCoordinates); when(((Locatable) mockElement).getCoordinates().inViewPort()).thenReturn(new Point(100, 100), new Point(100, 100)); PositionUnchangedCondition condition = new PositionUnchangedCondition(100); assertThat(condition.apply(mockElement), equalTo(true)); }
@Test public void shouldNotMatchConditionWhenElementPositionHasChanged() { when(((Locatable) mockElement).getCoordinates()).thenReturn(mockCoordinates); when(((Locatable) mockElement).getCoordinates().inViewPort()).thenReturn(new Point(100, 100), new Point(150, 100)); PositionUnchangedCondition condition = new PositionUnchangedCondition(100); assertThat(condition.apply(mockElement), equalTo(false)); }
/*** * Determine if the element is within the bounds of the window * * @return true or false * @throws WidgetException */ public boolean isWithinBoundsOfWindow() throws WidgetException { JavascriptExecutor js = ((JavascriptExecutor) getGUIDriver().getWrappedDriver()); // execute javascript to get scroll values Object top = js.executeScript("return document.body.scrollTop;"); Object left = js.executeScript("return document.body.scrollLeft;"); int scrollTop; int scrollLeft; try { scrollTop = Integer.parseInt(top+""); scrollLeft = Integer.parseInt(left+""); } catch(NumberFormatException e) { throw new WidgetException("There was an error parsing the scroll values from the page", getByLocator()); } // calculate bounds Dimension dim = getGUIDriver().getWrappedDriver().manage().window().getSize(); int windowWidth = dim.getWidth(); int windowHeight = dim.getHeight(); int x = ((Locatable)getWebElement()).getCoordinates().onPage().getX(); int y = ((Locatable)getWebElement()).getCoordinates().onPage().getY(); int relX = x - scrollLeft; int relY = y - scrollTop; return relX + findElement().getSize().getWidth() <= windowWidth && relY + findElement().getSize().getHeight() <= windowHeight && relX >= 0 && relY >= 0; }
/*** * Scroll to this element */ @Override public void scrollTo() throws WidgetException { WebElement we = findElement(); Locatable l = ((Locatable)we); l.getCoordinates().inViewPort(); }
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); }
@Override public Coordinates getCoordinates() { return ((Locatable) getWrappedWebElement()).getCoordinates(); }
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); }
protected void probe(WebElement found) { if (found instanceof Locatable) { coordinates = ((Locatable) found).getCoordinates(); } }
public void ScrollIntoView() { Coordinates thisElementCor = ((Locatable) element).getCoordinates(); thisElementCor.inViewPort(); }
@Override public Actions sendKeys(WebElement element, CharSequence... keysToSend) { action.addAction( new SendKeysAction(keyboard, mouse, (Locatable) element, modifyCharSequence(keysToSend))); return this; }
public Coordinates getCoordinates() { return ((Locatable)getWebElement()).getCoordinates(); }
@Override public Point call() throws Exception { Locatable element = (Locatable) getElement(); return element.getCoordinates().inViewPort(); }
@Override public Coordinates getCoordinates() { return ((Locatable) getWrappedElement()).getCoordinates(); }
/** * Scrolls the browser to the element x and y position perform the current page. * @param element the element to scroll to */ public void scrollToElement(WebElement element) { Point elementLocationOnPage = ((Locatable) element).getCoordinates().onPage(); js.executeScript("window.scrollBy(" + elementLocationOnPage.x + "," + elementLocationOnPage.y + ");"); }
@Override public void perform(final WebElement target) { final Locatable hoverItem = (Locatable) target; final Mouse mouse = ((HasInputDevices) webDriver).getMouse(); mouse.mouseMove(hoverItem.getCoordinates()); }
@Override public Coordinates getCoordinates() { return ((Locatable) element).getCoordinates(); }
/*** * You can use this method to take your control pictures. Note that the file should be a png. * * @param element * @param toSaveAs * @throws IOException * @throws WidgetException */ public static void takeScreenshotOfElement(IElement element, File toSaveAs) throws IOException, WidgetException { for(int i = 0; i < 10; i++) { //Loop up to 10x to ensure a clean screenshot was taken log.info("Taking screen shot of locator " + element.getByLocator() + " ... attempt #" + (i+1)); //Scroll to element element.scrollTo(); //Take picture of the page WebDriver wd = SessionManager.getInstance().getCurrentSession().getWrappedDriver(); File screenshot; boolean isRemote = false; if(!(wd instanceof RemoteWebDriver)) { screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE); } else { Augmenter augmenter = new Augmenter(); screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE); isRemote = true; } BufferedImage fullImage = ImageIO.read(screenshot); WebElement ele = element.getWebElement(); //Parse out the picture of the element Point point = ele.getLocation(); int eleWidth = ele.getSize().getWidth(); int eleHeight = ele.getSize().getHeight(); int x; int y; if(isRemote) { x = ((Locatable)ele).getCoordinates().inViewPort().getX(); y = ((Locatable)ele).getCoordinates().inViewPort().getY(); } else { x = point.getX(); y = point.getY(); } log.debug("Screenshot coordinates x: "+x+", y: "+y); BufferedImage eleScreenshot = fullImage.getSubimage(x, y, eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); //Ensure clean snapshot (sometimes WebDriver takes bad pictures and they turn out all black) if(!isBlack(ImageIO.read(screenshot))) { FileUtils.copyFile(screenshot, toSaveAs); break; } } }
public void mouseOver(WebElement element) { Locatable hoverItem = (Locatable) element; Mouse mouse = ((HasInputDevices) driver).getMouse(); mouse.mouseMove(hoverItem.getCoordinates()); }
/** * * @return locatable */ public Locatable getLocatable() { return locatable; }