ru.yandex.qatools.ashot.AShot Java Examples

The following examples show how to use ru.yandex.qatools.ashot.AShot. 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: Screen.java    From carina with Apache License 2.0 6 votes vote down vote up
@Override
public ICapturable capture(ScreenArea area) {
    try {
        switch (area) {
        case VISIBLE_SCREEN:
            screenshot = new AShot().takeScreenshot(driver).getImage();
            break;

        case FULL_SCREEN:
            screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver).getImage();
            break;
        }
    } catch (Exception e) {
        LOGGER.error("Unable to capture screenshot: " + e.getMessage());
    }
    return this;
}
 
Example #2
Source File: ScreenshotTaker.java    From vividus with Apache License 2.0 6 votes vote down vote up
private byte[] takeScreenshotAsByteArrayImpl(List<WebElement> webElements, boolean viewportScreenshot)
{
    WebDriver webDriver = webDriverProvider.get();
    try
    {
        AShot aShot = createAShot(viewportScreenshot);
        IndentCropper indentCropper = new IndentCropper(fullPageScreenshots ? Integer.MAX_VALUE : indent);
        highlighterType.addIndentFilter(indentCropper);
        aShot.imageCropper(indentCropper);
        return ImageTool.toByteArray(
                webElements.isEmpty() || HighlighterType.DEFAULT == highlighterType && fullPageScreenshots
                        ? aShot.takeScreenshot(webDriver) : aShot.takeScreenshot(webDriver, webElements));
    }
    catch (IOException e)
    {
        throw new IllegalStateException(e);
    }
}
 
Example #3
Source File: AshotFactoryTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void shouldCreateAshotUsingScrollableElement()
{
    WebElement webElement = mock(WebElement.class);
    ScreenshotConfiguration screenshotConfiguration = new ScreenshotConfiguration();
    screenshotConfiguration.setScrollableElement(() -> Optional.of(webElement));
    screenshotConfiguration.setCoordsProvider("CEILING");
    screenshotConfiguration.setScreenshotShootingStrategy(Optional.empty());
    AShot aShot = ashotFactory.create(false, Optional.of(screenshotConfiguration));

    assertThat(Whitebox.getInternalState(aShot, COORDS_PROVIDER), is(instanceOf(CeilingJsCoordsProvider.class)));
    ShootingStrategy scrollableElementAwareDecorator = getShootingStrategy(aShot);
    assertThat(scrollableElementAwareDecorator,
            is(instanceOf(AdjustingScrollableElementAwareViewportPastingDecorator.class)));
    assertEquals(webElement,
            Whitebox.getInternalState(scrollableElementAwareDecorator, "scrollableElement"));

    ShootingStrategy scalingDecorator = getShootingStrategy(scrollableElementAwareDecorator);
    assertThat(scalingDecorator, is(instanceOf(ScalingDecorator.class)));
    verifyDPR(scalingDecorator);
}
 
Example #4
Source File: AshotFactory.java    From vividus with Apache License 2.0 6 votes vote down vote up
private AShot createAshot(ScreenshotConfiguration screenshotConfiguration)
{
    ShootingStrategy decorated = baseShootingStrategy.get();

    int nativeFooterToCut = screenshotConfiguration.getNativeFooterToCut();
    int nativeHeaderToCut = screenshotConfiguration.getNativeHeaderToCut();
    if (anyNotZero(nativeFooterToCut, nativeHeaderToCut))
    {
        decorated = cutting(baseShootingStrategy.get(), new FixedCutStrategy(nativeHeaderToCut, nativeFooterToCut));
    }

    int footerToCut = screenshotConfiguration.getWebFooterToCut();
    int headerToCut = screenshotConfiguration.getWebHeaderToCut();
    if (anyNotZero(footerToCut, headerToCut))
    {
        decorated = cutting(decorated, new StickyHeaderCutStrategy(headerToCut, footerToCut));
    }

    decorated = ((DebuggingViewportPastingDecorator) decorateWithViewportPasting(decorated,
            screenshotConfiguration))
            .withDebugger(screenshotDebugger);

    return new AShot().shootingStrategy(decorated)
            .coordsProvider(screenshotConfiguration.getCoordsProvider().create(javascriptActions));
}
 
Example #5
Source File: AshotFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCreateAshotViaScreenshotShootingStrategyIfThereIfConfigurationNotFound()
{
    mockDeviceAndOrientation();
    ashotFactory.setScreenshotShootingStrategy(ScreenshotShootingStrategy.VIEWPORT_PASTING);
    AShot aShot = ashotFactory.create(false, Optional.empty());
    assertThat(Whitebox.getInternalState(aShot, COORDS_PROVIDER), is(instanceOf(CeilingJsCoordsProvider.class)));
    assertThat(Whitebox.getInternalState(aShot, SHOOTING_STRATEGY),
            instanceOf(AdjustingViewportPastingDecorator.class));
}
 
Example #6
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 #7
Source File: SeleniumDriver.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
private File getScreenShotFromAShot(WebDriver driver, ShootingStrategy strategy) throws IOException {
    File file = File.createTempFile("screenshot", ".png");
    Screenshot screenshot = new AShot().shootingStrategy(strategy)
            .takeScreenshot(driver);
    ImageIO.write(screenshot.getImage(), "png", file);
    return file;
}
 
Example #8
Source File: AshotFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCreateAshotWithCuttingStrategiesForNativeWebHeadersFooters()
{
    ScreenshotConfiguration screenshotConfiguration = new ScreenshotConfiguration();
    screenshotConfiguration.setNativeFooterToCut(TEN);
    screenshotConfiguration.setWebHeaderToCut(TEN);
    screenshotConfiguration.setScreenshotShootingStrategy(Optional.empty());

    AShot aShot = ashotFactory.create(false, Optional.of(screenshotConfiguration));

    assertThat(Whitebox.getInternalState(aShot, COORDS_PROVIDER), is(instanceOf(CeilingJsCoordsProvider.class)));
    ShootingStrategy viewportPastingDecorator = getShootingStrategy(aShot);
    assertThat(viewportPastingDecorator, is(instanceOf(AdjustingViewportPastingDecorator.class)));
    assertEquals(500, (int) Whitebox.getInternalState(viewportPastingDecorator, "scrollTimeout"));
    assertEquals(screenshotDebugger, Whitebox.getInternalState(viewportPastingDecorator, "screenshotDebugger"));

    ShootingStrategy webCuttingDecorator = getShootingStrategy(viewportPastingDecorator);
    assertThat(webCuttingDecorator, is(instanceOf(CuttingDecorator.class)));
    CutStrategy webCutStrategy = getCutStrategy(webCuttingDecorator);
    assertThat(webCutStrategy, is(instanceOf(StickyHeaderCutStrategy.class)));
    webCutStrategy.getHeaderHeight(null);
    validateCutStrategy(0, 10, webCutStrategy);

    ShootingStrategy nativeCuttingDecorator = getShootingStrategy(webCuttingDecorator);
    CutStrategy nativeCutStrategy = getCutStrategy(nativeCuttingDecorator);
    assertThat(nativeCutStrategy, is(instanceOf(FixedCutStrategy.class)));
    validateCutStrategy(10, 0, nativeCutStrategy);

    ShootingStrategy scalingDecorator = getShootingStrategy(nativeCuttingDecorator);
    assertThat(scalingDecorator, is(instanceOf(ScalingDecorator.class)));
    verifyDPR(scalingDecorator);
}
 
Example #9
Source File: AshotFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCreateAshotViaWithSimpleCoords()
{
    ashotFactory.setScreenshotShootingStrategy(ScreenshotShootingStrategy.SIMPLE);
    ShootingStrategy strategy = ScreenshotShootingStrategy.SIMPLE
            .getDecoratedShootingStrategy(baseShootingStrategy, false, false, null);
    AShot aShot = ashotFactory.create(false, Optional.empty());
    assertThat(Whitebox.getInternalState(aShot, COORDS_PROVIDER), is(instanceOf(CeilingJsCoordsProvider.class)));
    assertEquals(strategy, baseShootingStrategy);
}
 
Example #10
Source File: AshotFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCreateAshotViaScreenshotShootingStrategyUsingStrategyFromConfiguration()
{
    mockDeviceAndOrientation();
    ScreenshotConfiguration screenshotConfiguration = new ScreenshotConfiguration();
    screenshotConfiguration.setScreenshotShootingStrategy(Optional.of(ScreenshotShootingStrategy.SIMPLE));
    AShot aShot = ashotFactory.create(false, Optional.of(screenshotConfiguration));
    assertThat(Whitebox.getInternalState(aShot, COORDS_PROVIDER), is(instanceOf(CeilingJsCoordsProvider.class)));
    assertThat(Whitebox.getInternalState(aShot, SHOOTING_STRATEGY), instanceOf(ScalingDecorator.class));
}
 
Example #11
Source File: AshotFactory.java    From vividus with Apache License 2.0 5 votes vote down vote up
private AShot createAShot(ShootingStrategy baseShootingStrategy,
        ScreenshotShootingStrategy screenshotShootingStrategy, boolean viewportScreenshot)
{
    String deviceName = webDriverFactory.getCapability(SauceLabsCapabilityType.DEVICE_NAME, false);
    boolean landscapeOrientation = webDriverManager.isOrientation(ScreenOrientation.LANDSCAPE);
    ShootingStrategy shootingStrategy = screenshotShootingStrategy.getDecoratedShootingStrategy(
            baseShootingStrategy, viewportScreenshot, landscapeOrientation, deviceName);
    return new AShot().shootingStrategy(shootingStrategy)
            .coordsProvider(screenshotShootingStrategy == ScreenshotShootingStrategy.SIMPLE
                    ? CeilingJsCoordsProvider.getSimple(javascriptActions)
                    : CeilingJsCoordsProvider.getScrollAdjusted(javascriptActions));
}
 
Example #12
Source File: AshotFactory.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public AShot create(boolean viewportScreenshot, Optional<ScreenshotConfiguration> screenshotConfiguration)
{
    return screenshotConfiguration.map(
        ashotConfiguration -> ashotConfiguration.getScreenshotShootingStrategy()
                .map(sss -> createAShot(baseShootingStrategy.get(), sss, viewportScreenshot))
                .orElseGet(() -> createAshot(ashotConfiguration)))
            .orElseGet(() -> createAShot(baseShootingStrategy.get(), viewportScreenshot));
}
 
Example #13
Source File: ScreenshotTaker.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public ru.yandex.qatools.ashot.Screenshot takeAshotScreenshot(SearchContext searchContext,
        Optional<ScreenshotConfiguration> screenshotConfiguration)
{
    Optional<ScreenshotConfiguration> configuration = screenshotConfiguration.or(this::getConfiguration);
    AShot aShot = createAShot(false, configuration);
    return takeScreenshot(searchContext, aShot,
            configuration.map(ScreenshotConfiguration::getScrollableElement).flatMap(Supplier::get));
}
 
Example #14
Source File: ScreenshotTaker.java    From vividus with Apache License 2.0 5 votes vote down vote up
private ru.yandex.qatools.ashot.Screenshot takeScreenshot(SearchContext searchContext, AShot aShot,
        Optional<WebElement> scrollableElement)
{
    Supplier<ru.yandex.qatools.ashot.Screenshot> screenshotTaker = () -> searchContext instanceof WebDriver
            ? aShot.takeScreenshot((WebDriver) searchContext)
            : aShot.takeScreenshot(webDriverProvider.get(), (WebElement) searchContext);

    ru.yandex.qatools.ashot.Screenshot screenshot =
            scrollableElement.map(e -> scrollbarHandler.performActionWithHiddenScrollbars(screenshotTaker, e))
        .orElseGet(() -> scrollbarHandler.performActionWithHiddenScrollbars(screenshotTaker));

    screenshotDebugger.debug(this.getClass(), "After_AShot", screenshot.getImage());
    return screenshot;
}
 
Example #15
Source File: AshotFactory.java    From vividus with Apache License 2.0 4 votes vote down vote up
private AShot createAShot(ShootingStrategy baseShootingStrategy, boolean viewportScreenshot)
{
    return createAShot(baseShootingStrategy, screenshotShootingStrategy, viewportScreenshot);
}
 
Example #16
Source File: ScreenshotTaker.java    From vividus with Apache License 2.0 4 votes vote down vote up
private AShot createAShot(boolean viewportScreenshot, Optional<ScreenshotConfiguration> screenshotConfiguration)
{
    return ashotFactory.create(viewportScreenshot, screenshotConfiguration);
}
 
Example #17
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 #18
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;
}
 
Example #19
Source File: ScreenshotTaker.java    From vividus with Apache License 2.0 4 votes vote down vote up
private AShot createAShot(boolean viewportScreenshot)
{
    return ashotFactory.create(viewportScreenshot, getConfiguration());
}
 
Example #20
Source File: IAshotFactory.java    From vividus with Apache License 2.0 votes vote down vote up
AShot create(boolean viewportScreenshot, Optional<ScreenshotConfiguration> screenshotConfiguration);