3

For taking a screenshot in Selenium, does it support only the PNG format or will it also support the JPEG format?

Which is the default one image format for taking screenshot in Selenium and differences?

2

3 Answers 3

1

Based on the Stack Overflow post that was given in a comment, you can do this with fairly easily it appears.

In that post it looks like you can use Java.awt.Robot to create an image with pixels that are taken from the screen...a screenshot. The code that they give is:

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "jpg", new File(args[0]));

Keep in mind that this is nearly 10 year old code and will only capture the primary monitor.

The ImageIO.Write() method takes a parameter that allows for different image formats:

ImageIO.write(capture, "jpg", new File(args[0]));

This ImageIO takes the buffered image from the robot and writes it to a file of any format that you like.

Just change the "jpg" to the format you want and give it a test to make sure that it works.

11
  • 1
    Could you please specify why should OP go for java.awt.Robot instead of a webdriver that has an implemented screenshot feature? Aug 4, 2018 at 20:00
  • As far as I know, the screenshot implementation provided by the webdriver only creates a .png file. I could be wrong, but that was the point of the post, which was why I linked to the alternate method which does seem to show a method to create other formats. Aug 6, 2018 at 13:54
  • 1
    Ah, good point. I believe that in C# you can select what image format the file will be saved in, but if in Java only .png is supported, this answer makes more sense. @Malachi, I think you should edit your answer so that it's clear that Robot class is used because of a better image format support. In what image formats can the Robot save the file? Aug 7, 2018 at 5:37
  • 1
    The advantage of using WebDriver for taking the screenshot is that what we get is the browser window. This example gives the whole of the screen which IMO, not desirable. You can convert the PNG to JPG using ImageIO rather than using the java.awt.Robot. Aug 8, 2018 at 6:33
  • @KDM, would this solution give better resolution than the solution that you present?
    – Malachi
    Aug 8, 2018 at 18:10
1

Selenium's TakesScreenshot interface supports only the PNG file format. Once you get the screenshot, it is trivial to convert it into JPEG using the ImageIO library.

File screenShot = (File)((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
bufferedImage = ImageIO.read(screenShot);

// Create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
                                                   bufferedImage.getHeight(),
                                                   BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

// Write to a JPEG file
ImageIO.write(newBufferedImage, "jpg", new File("JPGFilePath.jpg"));
4
  • Can you just feed the file into the Write Method?
    – Malachi
    Aug 8, 2018 at 18:12
  • I don’t think so. You can, of coarse, extract this into a method. Aug 8, 2018 at 18:21
  • I meant without first creating a new BufferedImage?
    – Malachi
    Aug 8, 2018 at 18:23
  • 1
    Imageio works with image objects. So you need to create a new buffered image of RGB type. Aug 8, 2018 at 18:25
-2

Using TakesScreenshot interface as suggested by Aulis you can easily capture the screen.

Usage:

screenShot = (File)((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
screenShot.renameTo(new File(outputPath, String.format("%s.jpg", "screenshot")));

I believe you should be able to easily change the extension here to get a jpg as well. An advantage of choosing this over Robot is that when you're executing your code on a remote VM Robot will not work whereas this one will. Hope this helps.

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.