private BufferedImage takeScreenshotAsImage(WebDriver webdriver, WebElement element) { if (!(webdriver instanceof TakesScreenshot)) { log.warning("Cannot take screenshot because browser does not support screenshots"); return null; } byte[] screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES); Point elementLocation = element.getLocation(); try { BufferedImage img = ImageIO.read(new ByteArrayInputStream(screen)); int elementWidth = element.getSize().getWidth(); int elementHeight = element.getSize().getHeight(); if (elementWidth > img.getWidth()) { elementWidth = img.getWidth() - elementLocation.getX(); } if (elementHeight > img.getHeight()) { elementHeight = img.getHeight() - elementLocation.getY(); } return img.getSubimage(elementLocation.getX(), elementLocation.getY(), elementWidth, elementHeight); } catch (IOException e) { log.log(SEVERE, "Failed to take screenshot of " + element, e); return null; } catch (RasterFormatException e) { log.warning("Cannot take screenshot because element is not displayed on current screen position"); return null; } }
@Override public WebElement execute(SelenideElement proxy, WebElementSource locator, Object[] args) { Point location = locator.getWebElement().getLocation(); locator.driver().executeJavaScript("window.scrollTo(" + location.getX() + ", " + location.getY() + ')'); return proxy; } }
@Override public Rect findArea(WebPageElement webPageElement) { WebElement webElement = webPageElement.getWebElement(); Point location = webElement.getLocation(); Dimension size = webElement.getSize(); return new Rect(location.getX(), location.getY(), size.getWidth(), size.getHeight()); } }),
Point iframeLocation = iframe.getLocation(); BufferedImage img; try { iframeWidth = img.getWidth() - iframeLocation.getX(); Point elementLocation = element.getLocation(); int elementWidth = element.getSize().getWidth(); int elementHeight = element.getSize().getHeight();
/** * Checks if the midpoint of an element is covered by any other element. * @return true if element is covered, false otherwise. */ public boolean isElementCovered(WebElement element) { int x = element.getLocation().x + element.getSize().width / 2; int y = element.getLocation().y + element.getSize().height / 2; WebElement topElem = (WebElement) executeScript("return document.elementFromPoint(" + x + "," + y + ");"); return !topElem.equals(element); }
@Override public Point getLocation() { if (delegateWebElement != null) { return delegateWebElement.getLocation(); } else { return wrappedWebElement.getLocation(); } }
public void dragdrop(By ByFrom, By ByTo) { WebElement LocatorFrom = driver.findElement(ByFrom); WebElement LocatorTo = driver.findElement(ByTo); String xto=Integer.toString(LocatorTo.getLocation().x); String yto=Integer.toString(LocatorTo.getLocation().y); ((JavascriptExecutor)driver).executeScript("function simulate(f,c,d,e){var b,a=null;for(b in eventMatchers)if(eventMatchers[b].test(c)){a=b;break}if(!a)return!1;document.createEvent?(b=document.createEvent(a),a==\"HTMLEvents\"?b.initEvent(c,!0,!0):b.initMouseEvent(c,!0,!0,document.defaultView,0,d,e,d,e,!1,!1,!1,!1,0,null),f.dispatchEvent(b)):(a=document.createEventObject(),a.detail=0,a.screenX=d,a.screenY=e,a.clientX=d,a.clientY=e,a.ctrlKey=!1,a.altKey=!1,a.shiftKey=!1,a.metaKey=!1,a.button=1,f.fireEvent(\"on\"+c,a));return!0} var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/}; " + "simulate(arguments[0],\"mousedown\",0,0); simulate(arguments[0],\"mousemove\",arguments[1],arguments[2]); simulate(arguments[0],\"mouseup\",arguments[1],arguments[2]); ", LocatorFrom,xto,yto); }
// Find an element and define it WebElement elementToClick = driver.findElement(By.xpath("some xpath")); // Scroll the browser to the element's Y position ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")"); // Click the element elementToClick.click();
private void scrollToElementAndClick(WebElement element) { int yScrollPosition = element.getLocation().getY(); js.executeScript("window.scroll(0, " + yScrollPosition + ");"); element.click(); }
public static final int HEADER_OFFSET = 200; private void scrollToElementAndClick(WebElement element) { int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET; js.executeScript("window.scroll(0, " + yScrollPosition + ");"); element.click(); }
public void scrollAndClick(By by) { WebElement element = driver.findElement(by); int elementPosition = element.getLocation().getY(); String js = String.format("window.scroll(0, %s)", elementPosition); ((JavascriptExecutor)driver).executeScript(js); element.click(); }
@Override public Point getLocation() { waitForVaadin(); return wrappedElement.getLocation(); }
/** * Method to generate rectangle for the element since current version of * appium driver throws unimplemented exception * * @param element WebElement * @return Rectangle */ public Rectangle getRect(WebElement element) { return new Rectangle(element.getLocation(), element.getSize()); }
@Override public int getLeft() { return getElement().getLocation().getX(); }
@Override public Point doGetLocation() { Point point = element.getLocation(); LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Location", point.toString(), getName())); return point; }
private void dragAndDropWithOffset(final WebElement source, final WebElement target, final int offset) { Point currentLocation = source.getLocation(); Point destination = target.getLocation(); // We need to ensure we have the source in view. An egrgarious hack to make sure we can do this. // Assumes that scrolling to the top of the page will have our source AND target in the viewable area driver.findElement(By.tagName("body")).getSize(); int xOffset = destination.x - currentLocation.x + 1; int yOffset = destination.y - currentLocation.y + offset; Actions action = new Actions(driver).dragAndDropBy(source, xOffset, yOffset); action.perform(); }
@Override public int getLocationY() throws WidgetException { try { Point p = findElement().getLocation(); return p.getY(); } catch (Exception e) { throw new WidgetException("Error while fetching Y location", locator, e); } }
public SelenideElement scrollTo() { Point location = getWebElement().getLocation(); jsExecutor().executeScript("window.scrollTo(" + location.getX() + ", " + location.getY() + ')'); return this; }
@Override public Coords ofElement(WebDriver driver, WebElement element) { Point point = element.getLocation(); Dimension dimension = element.getSize(); return new Coords( point.getX(), point.getY(), dimension.getWidth(), dimension.getHeight()); } }
public int getVerticalPosition(String locator) { LOG.keywordAppender().appendLocator(locator); WebElement element = finder.find(locator); Point point = element.getLocation(); LOG.keywordAppender() .appendArgument("X", point.getX()) .appendArgument("Y", point.getY()); return point.getY(); }