简体   繁体   中英

Unable to take browser full page screenshot

Here my code code 1:

scenario.embed(sharedDriver.getScreenshotAs(OutputType.BYTES), "image/png");

code 2:

 BufferedImage image = null;
        try {
            image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            File file = new File("image.png");
            ImageIO.write(image, "png", file);
            scenario.embed(FileUtils.readFileToByteArray(file), "image/png");
        } catch (Exception e) {
            e.printStackTrace();
        }

code 3:

  try {

            Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(2000)).takeScreenshot(sharedDriver);
            File file = new File("screenshot.png");
            ImageIO.write(screenshot.getImage(), "png", file);
            scenario.embed(FileUtils.readFileToByteArray(file), "image/png");
        } catch (Exception e) {
            e.printStackTrace();
        }

But none of the above code take full page screenshot. It just take only visible browser screen. I'm running test it in linux machine. I didn't test in windows machine.

You haven't mentioned what browser you are using but the screen capturing behavior works differently for different browsers. Only Firefox is capable of taking a full screenshot without helper tools like AShot. So your "code1" block might only work for Firefox.

Your "code2" block will not work as Java Robot library works only with physical screen contents so if there is anything that does not appear on the screen, it won't be visible.

AShot however should work fine for any browser (at least for Chrome and Firefox). I think you just have an issue with your code. Try changing your code like this:

Screenshot screenCapture = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);

You can try below code . It works for me atleast.

// Get the entire page screenshot
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        BufferedImage fullImg = ImageIO.read(screenshot);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM