4

Taking screenshot is very fast if the code is executed on a server at localhost, less than a second (around 400-500ms):

private RemoteWebDriver driver;
private DesiredCapabilities dc = new DesiredCapabilities();

@Before
public void setUp() throws MalformedURLException {
    ....
    ....
    dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
}

@Test
public void test() throws InterruptedException, IOException {
    driver.get("https://google.com");
    driver.findElement(By.name("q")).sendKeys("automation test");
    
    long before = System.currentTimeMillis();
    //here is the problem
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    long after = System.currentTimeMillis();
    System.out.println(after-before);

    FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png"));
}

But if the target server is changed to public ip which has Selenium server installed, taking the screenshot will be slower (around 5 seconds). Maybe this could be due to the distance between the client and the server, so there must be a difference.

Is possible to reduce the screenshot taking time? I'm thinking about reducing the image resolution, but how do I adjust it?

frianH
  • 7,295
  • 6
  • 20
  • 45
  • Check your network usage during that time. Most likely, it's the time it takes to download the image. – chrylis -cautiouslyoptimistic- Nov 30 '20 at 06:06
  • @chrylis-cautiouslyoptimistic- Else than network issue, is there any other way to make this code work better? – frianH Nov 30 '20 at 08:35
  • have you tried getting the screen shot as base64 (https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/OutputType.html#BASE64) . You could also look at this answer: https://stackoverflow.com/a/42143900/1387701 – DMart Dec 02 '20 at 04:48
  • are the file stored in remote mechine and copied to local system in this? – PDHide Dec 08 '20 at 03:19
  • @PDHide Take screenshot from the client side, copy direct from the server. Not stored on the server first. – frianH Dec 10 '20 at 04:11

2 Answers2

1

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):

@Test
public 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);
}
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • Thanks for the answer, but this doesn't seem to solve the problem. In fact the problem is in the line `File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);`, not after. Sorry, code in the question updated. – frianH Dec 04 '20 at 08:12
  • Oh, I see. There is a Java library called Ashot that does screenshots of different parts of a web page. Maybe you can try it. See https://stackoverflow.com/questions/52015559/how-to-capture-screenshot-of-a-webelement-within-a-webpage-but-not-the-entire-sc. – Mate Mrše Dec 04 '20 at 08:33
1

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);
        
DMart
  • 2,401
  • 1
  • 14
  • 19
  • Thanks for the response, I've tried but that didn't reduce the time at all. – frianH Dec 10 '20 at 04:06
  • I think you're out of luck then as far as native selenium goes. Though the screen shot behavior does vary by browser. – DMart Dec 10 '20 at 16:25