ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider Java Examples

The following examples show how to use ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoordsProviderTypeTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
private static Stream<Arguments> coordsProviderSource()
{
    return Stream.of(
      Arguments.of(CoordsProviderType.CEILING, CeilingJsCoordsProvider.class),
      Arguments.of(CoordsProviderType.WEB_DRIVER, WebDriverCoordsProvider.class)
    );
}
 
Example #2
Source File: TakeScreenShotSeleniumLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenGoogleIsLoaded_thenCaptureLogo() throws IOException {
    WebElement logo = driver.findElement(By.id("hplogo"));

    Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
        .coordsProvider(new WebDriverCoordsProvider())
        .takeScreenshot(driver, logo);

    ImageIO.write(screenshot.getImage(), "jpg", new File(resolveTestResourcePath("google-logo.png")));
    assertTrue(new File(resolveTestResourcePath("google-logo.png")).exists());
}
 
Example #3
Source File: ScreenShoter.java    From QVisual with Apache License 2.0 4 votes vote down vote up
public BufferedImage takeScreen(boolean fullScreen, WebDriver driver) {
    try {
        moveTo(0, 0, driver);

        switch (BROWSER_NAME.toLowerCase()) {
            case "chrome": {
                if (fullScreen) {
                    return read(getScreenshotFull(driver, FILE));
                } else {
                    return read(((TakesScreenshot) driver).getScreenshotAs(FILE));
                }
            }
            case "firefox": {
                if (fullScreen) {
                    ((JavascriptExecutor) driver).executeScript("return window.scrollTo(0, 0);");

                    return new AShot().coordsProvider(new WebDriverCoordsProvider())
                            .shootingStrategy(getStrategy())
                            .takeScreenshot(driver)
                            .getImage();
                } else {
                    return read(((TakesScreenshot) driver).getScreenshotAs(FILE));
                }
            }
            case "safari": {
                return read(((TakesScreenshot) driver).getScreenshotAs(FILE));
            }
            case "ie":
            case "internet explorer":
            case "edge": {
                return read(((TakesScreenshot) driver).getScreenshotAs(FILE));
            }
            default: {
                return new AShot().coordsProvider(new WebDriverCoordsProvider())
                        .shootingStrategy(getStrategy())
                        .takeScreenshot(driver)
                        .getImage();
            }
        }
    } catch (Exception e) {
        logger.error("[take screenshot]", e);
    }

    return null;
}
 
Example #4
Source File: AssertElementImage.java    From opentest with MIT License 4 votes vote down vote up
/**
 * Capture the image of the target HTML element.
 * @param targetLocator The locator of the HTML element to capture.
 * @param ignoredElements The HTML elements ignored from the comparison.
 * @param scaleFactor For retina displays, this has to be set to 2.
 * @param ignoredPixelsColor The color that will be used to obscure the
 * HTML elements that are ignored from the comparison.
 * @return The image of the HTML element.
 */
private BufferedImage captureImage(By targetLocator, By[] ignoredElements, Double scaleFactor, Color ignoredPixelsColor) {
    WebElement targetElement = this.getElement(targetLocator);
    ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(scaleFactor.floatValue()), 100);
    AShot ashot = new AShot()
            .coordsProvider(new WebDriverCoordsProvider())
            .shootingStrategy(shootingStrategy);

    // Hide scroll bars, so they don't impact the captured image
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    Object overflowValue = jsExecutor.executeScript("return document.querySelector('html').style.overflow");
    if (!(overflowValue instanceof String)) {
        overflowValue = "initial";
    }
    jsExecutor.executeScript("document.querySelector('html').style.overflow = 'hidden'");

    Screenshot screenshot = ashot.takeScreenshot(driver, targetElement);
    BufferedImage capturedImage = screenshot.getImage();

    jsExecutor.executeScript(String.format("document.querySelector('html').style.overflow = '%s';", overflowValue));

    for (By by : ignoredElements) {
        Point targetElemLocation = targetElement.getLocation();

        WebElement ignoredElement = this.getElement(by);
        Point ignoredElementLocation = ignoredElement.getLocation();

        Dimension size = ignoredElement.getSize();
        int width = size.width;
        int height = size.height;

        int relativeX = ignoredElementLocation.getX() - targetElemLocation.getX();
        int relativeY = ignoredElementLocation.getY() - targetElemLocation.getY();

        for (int xCoord = relativeX; xCoord < relativeX + width; xCoord++) {
            for (int yCoord = relativeY; yCoord < relativeY + height; yCoord++) {
                capturedImage.setRGB(xCoord, yCoord, ignoredPixelsColor.getRGB());
            }
        }
    }

    return capturedImage;
}