Example usage for org.openqa.selenium OutputType BYTES

List of usage examples for org.openqa.selenium OutputType BYTES

Introduction

In this page you can find the example usage for org.openqa.selenium OutputType BYTES.

Prototype

OutputType BYTES

To view the source code for org.openqa.selenium OutputType BYTES.

Click Source Link

Document

Obtain the screenshot as raw bytes.

Usage

From source file:acceptance.SharedDriver.java

License:Open Source License

@After
public void embedScreenshot(Scenario scenario) {
    if (scenario.isFailed()) {
        try {/* ww  w.ja v  a 2s. com*/
            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (UnsupportedOperationException somePlatformsDontSupportScreenshots) {
            System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
    }
}

From source file:at.tugraz.ist.cucumber.SeleniumStepdefs.java

License:Open Source License

private void captureScreen() {
    if (this.driver != null) {
        WebDriver augmenter = new Augmenter().augment(driver);
        byte[] imageBytes = ((TakesScreenshot) augmenter).getScreenshotAs(OutputType.BYTES);
        this.scenario.write("");
        this.scenario.embed(imageBytes, "image/png");
    }//from   w w w  .ja va2  s.  co m
}

From source file:com.btmatthews.selenium.junit4.rule.ScreenShotOnFailure.java

License:Apache License

/**
 * Handle a test case failure by taking a screen shot from the browser and writing it to a file.
 *
 * @param exception   The exception that describes the test case failure.
 * @param description Describes the unit test that failed.
 *//* ww w  .ja  va 2 s.  co m*/
@Override
protected void failed(final Throwable exception, final Description description) {
    try {
        if (webDriver != null) {
            if (webDriver instanceof TakesScreenshot) {
                final File target = generator.getTargetFilename(description);
                final byte[] source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
                FileUtils.forceMkdir(target.getParentFile());
                FileUtils.writeByteArrayToFile(target, source);
            }
        } else {
            final File target = generator.getTargetFilename(description);
            FileUtils.forceMkdir(target.getParentFile());
            server.captureEntirePageScreenshot(target.getAbsolutePath(), "");
        }
    } catch (final IOException e) {
        LOGGER.error("I/O error capturing screen shot after failure", e);
    }
}

From source file:com.cisco.dbds.utils.selenium.SeleniumUtilities.java

License:Open Source License

/**
 * Returns the screenshot as a byte array to embed in cucumber reports.
 * //from w w  w  .j av  a2s . co  m
 * @return the byte[]
 */
public static byte[] takeScreenshot() {
    final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    return screenshot;
}

From source file:com.cognifide.aet.job.common.collectors.screen.ScreenCollector.java

License:Apache License

private byte[] getFullPageScreenshot() {
    return ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
}

From source file:com.cognifide.qa.bobcumber.GlobalHooks.java

License:Apache License

private void addScreenshot(Scenario scenario) {
    byte[] screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
    scenario.embed(screenshot, "image/png");
}

From source file:com.coinbot.captcha.CaptchaDetector.java

License:Open Source License

private BufferedImage captureCaptcha(WebDriver driver, WebElement captcha) {
    WebElement iframe = null;//from  www.  ja va2s  .  co  m
    try {
        iframe = captcha.findElement(By.tagName("iframe"));
    } catch (NoSuchElementException e) {
    }

    // Si tiene iframe es la version 2
    if (iframe != null) {
        driver.switchTo().frame(iframe);
        JavascriptExecutor js2 = (JavascriptExecutor) driver;
        js2.executeScript("document.getElementById('loading').style.display = 'none'");
        js2.executeScript("document.getElementById('overlay').style.display = 'inline'");
        driver.switchTo().defaultContent();
    }

    byte[] screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    BufferedImage imageScreen = null;
    try {
        imageScreen = ImageIO.read(new ByteArrayInputStream(screen));
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    Point capLocation = captcha.getLocation();
    Dimension capDimension = captcha.getSize();

    return imageScreen.getSubimage(capLocation.x, capLocation.y, capDimension.width, capDimension.height);
}

From source file:com.galenframework.components.mocks.driver.MockedDriver.java

License:Apache License

@Override
public <X> X getScreenshotAs(OutputType<X> xOutputType) throws WebDriverException {
    if (xOutputType.equals(OutputType.FILE)) {
        return (X) new File(getClass().getResource("/mocks/pages/screenshot.png").getFile());
    } else if (xOutputType.equals(OutputType.BYTES)) {
        File file = new File(getClass().getResource("/mocks/pages/screenshot.png").getFile());

        BufferedImage image = null;
        try {// w  ww  . j  a v a2s .  com
            image = Rainbow4J.loadImage(file.getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return (X) ((DataBufferByte) image.getData().getDataBuffer()).getData();

    } else
        throw new RuntimeException("Cannot make screenshot");
}

From source file:com.galenframework.utils.GalenUtils.java

License:Apache License

public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException {
    // scroll up first
    scrollVerticallyTo(driver, 0);/*from   w w w .j a v a 2s.  c  om*/
    byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
    int capturedWidth = image.getWidth();
    int capturedHeight = image.getHeight();

    long longScrollHeight = (Long) ((JavascriptExecutor) driver).executeScript(
            "return Math.max(" + "document.body.scrollHeight, document.documentElement.scrollHeight,"
                    + "document.body.offsetHeight, document.documentElement.offsetHeight,"
                    + "document.body.clientHeight, document.documentElement.clientHeight);");

    Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver)
            .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();

    int scrollHeight = (int) longScrollHeight;

    File file = File.createTempFile("screenshot", ".png");

    int adaptedCapturedHeight = (int) (((double) capturedHeight) / devicePixelRatio);

    BufferedImage resultingImage;

    if (Math.abs(adaptedCapturedHeight - scrollHeight) > 40) {
        int scrollOffset = adaptedCapturedHeight;

        int times = scrollHeight / adaptedCapturedHeight;
        int leftover = scrollHeight % adaptedCapturedHeight;

        final BufferedImage tiledImage = new BufferedImage(capturedWidth,
                (int) (((double) scrollHeight) * devicePixelRatio), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2dTile = tiledImage.createGraphics();
        g2dTile.drawImage(image, 0, 0, null);

        int scroll = 0;
        for (int i = 0; i < times - 1; i++) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            g2dTile.drawImage(nextImage, 0, (i + 1) * capturedHeight, null);
        }
        if (leftover > 0) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            BufferedImage lastPart = nextImage.getSubimage(0,
                    nextImage.getHeight() - (int) (((double) leftover) * devicePixelRatio),
                    nextImage.getWidth(), leftover);
            g2dTile.drawImage(lastPart, 0, times * capturedHeight, null);
        }

        scrollVerticallyTo(driver, 0);

        resultingImage = tiledImage;
    } else {
        resultingImage = image;
    }

    if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {
        try {
            resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage);
        } catch (Exception ex) {
            LOG.trace("Couldn't resize screenshot", ex);
        }
    }

    ImageIO.write(resultingImage, "png", file);
    return file;
}

From source file:com.github.mfriedenhagen.phantomjstest.AbstractWebDriverRule.java

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override/*from  ww w. jav  a  2  s.  c o  m*/
        public void evaluate() throws Throwable {
            before();
            try {
                base.evaluate();
            } catch (Throwable e) {
                final String name = description.getClassName() + "." + description.getMethodName();
                writeScreenshot(name);
                writeSource(name);
                throw e;
            } finally {
                System.err.println("Last URL=" + driver.getCurrentUrl());
                System.err.println("Last TITLE=" + driver.getTitle());
                driver.quit();
                after();
            }
        }

        private void writeScreenshot(String name) throws IOException {
            final File file = new File("target/" + name + ".png");
            Files.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES), file);
            System.err.println("Wrote screenshot: " + name);
        }

        private void writeSource(String name) throws IOException {
            final File file = new File("target/" + name + ".html");
            Files.write(driver.getPageSource().getBytes(), file);
            System.err.println("Wrote source: " + name);
        }
    };
}