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; } }
iframeWidth = img.getWidth() - iframeLocation.getX(); int elementHeight = element.getSize().getHeight(); if (elementWidth > iframeWidth) { elementWidth = iframeWidth - elementLocation.getX(); img = img.getSubimage(iframeLocation.getX() + elementLocation.getX(), iframeLocation.getY() + elementLocation.getY(), elementWidth, elementHeight);
@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()); } }),
@Override public void beforeWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) { messages.add("Attempt to change position of the window. The X is " + targetPoint.getX() + " the Y is " + targetPoint.getY()); }
@Override public void beforeWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) { messages.add("Externally defined listener: Attempt to change position of the window. " + "The X is " + targetPoint.getX() + " the Y is " + targetPoint.getY()); }
@Override public void afterWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) { messages.add("Externally defined listener: The position the window has been changed. " + "The X is " + targetPoint.getX() + " the Y is " + targetPoint.getY()); }
@Override public void afterWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) { messages.add("The position the window has been changed. The X is " + targetPoint.getX() + " the Y is " + targetPoint.getY()); }
/** * Method returns central coordinates of an element. * @return The instance of the {@link org.openqa.selenium.Point} */ public Point getCenter() { Point upperLeft = this.getLocation(); Dimension dimensions = this.getSize(); return new Point(upperLeft.getX() + dimensions.getWidth() / 2, upperLeft.getY() + dimensions.getHeight() / 2); }
prefix + "Size of the window has been changed. The height is " + d.getHeight() + " the width is " + d.getWidth(), prefix + "Attempt to change position of the window. The X is " + p.getX() + " the Y is " + p.getY(), prefix + "The position the window has been changed. The X is " + p.getX() + " the Y is " + p.getY(), prefix + "Attempt to maximize the window.",
@Override public String toString() { return format("By point in viewport %d,%d", coordinates.getX(), coordinates.getY()); } };
@Override public ICapturable highlight(Point point) { try { Graphics2D g2d = screenshot.createGraphics(); g2d.setColor(Color.red); g2d.draw(new Ellipse2D.Double(point.getX(), point.getY(), 100, 100)); } catch (Exception e) { LOGGER.error("Unable to highligh screenshot: " + e.getMessage()); } return this; }
@Override public List<WebElement> findElements(SearchContext context) { final String findByPoint = format("return document.elementFromPoint(%d,%d);", coordinates.getX(), coordinates.getY()); final WebElement element = execute(WebElement.class, findByPoint, webDriver); return element != null ? Collections.singletonList(element) : Collections.<WebElement>emptyList(); }
@Override public void addAnnotationData(Map<String, Object> data, WebElement webElement) { Point location = center(webElement); data.put("x", location.getX()); data.put("y", location.getY()); data.put("r", 20); } }
private void calculateCoordinates() { int relativeX = x - scrollTo.getX(); int relativeY = y - scrollTo.getY(); elementCoordinates = new Point(relativeX, relativeY); } }
@Override public int getLeft() { return getElement().getLocation().getX(); }
private void performShortClickAction(Point point) { try { new TouchActions(this).down(point.getX(), point.getY()).perform(); Thread.sleep(SHORT_TAP_TIME_MILLIS); new TouchActions(this).up(point.getX(), point.getY()).perform(); } catch (InterruptedException exe) { throw new WebDriverException("InterruptedException occurred during shortClick", exe); } }
@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()); } }
/** * Tap with TouchAction by the center of element * * @param element ExtendedWebElement */ default public void tap(ExtendedWebElement element) { Point point = element.getLocation(); Dimension size = helper.performIgnoreException(() -> element.getSize()); tap(point.getX() + size.getWidth() / 2, point.getY() + size.getHeight() / 2); }
/** * Tap with TouchAction by the center of element * * @param element ExtendedWebElement */ public static void tap(ExtendedWebElement element) { Point point = element.getLocation(); Dimension size = helper.performIgnoreException(() -> element.getSize()); tap(point.getX() + size.getWidth() / 2, point.getY() + size.getHeight() / 2); }