Search code examples
javaselenium-firefoxdriverfirefox-driver

FirefoxDriver takes screenshots of different size


I made a java application that compares screenshots taken from our staging environment against the production ones. The app fails due to different screenshot sizes.

How can I define the screenshot size? I am using the following code to generate the screenshot.

    final WebDriver driver = new FirefoxDriver();

    try {
        driver.manage().window().setSize(new Dimension(1024, 768));
        driver.get(link);
        File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    ....

Solution

  • As @Würgspaß mentioned in the comments, you can do this with OutputType.BYTE. Here is an example:

    byte[] bytes = driver.getScreenshotAs(OutputType.BYTES);
    BufferedImage full = ImageIO.read(new ByteArrayInputStream(bytes));
    full.getSubimage(0, 0, 1200, 800);