Reducing screenshot taking time on selenium Reducing screenshot taking time on selenium selenium selenium

Reducing screenshot taking time on selenium


Crop the screenshot

One way of getting smaller sized screenshots (other than using various file formats) is changing the size of the screenshot: you can take a screenshot of a web element (or a region of a page) that is of special interest to you.

Try the following (you will need to use the BufferedImage class):

@Testpublic void test() throws InterruptedException, IOException {    driver.get("https://google.com");    driver.findElement(By.name("q")).sendKeys("automation test");        long before = System.currentTimeMillis();    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);    Point p = element.getLocation();    int width = element.getSize().getWidth();      int height = element.getSize().getHeight();    BufferedImage img = ImageIO.read(scrFile);    BufferedImage elementScreenshot = img.getSubimage(p.getX(), p.getY(), width, height);    //NOTE: the line above will crop the full page screenshot to element dimensions, change width and height if you wish to crop to region    Image.write(elementScreenshot, "png", scrFile);    FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png"));    long after = System.currentTimeMillis();    System.out.println(after-before);}


The problem is transferring the file over the wire most likely. IT might also be in the creation of the file.

I would suggest trying with the Base64 output to see if that reduces the transfer time:

String screenshotAsBase64String = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);