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?

asked Aug 3, 2018 at 17:51
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.

answered Aug 3, 2018 at 20:36
11
  • 1
    Could you please specify why should OP go for java.awt.Robot instead of a webdriver that has an implemented screenshot feature? Commented 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. Commented 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? Commented 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. Commented Aug 8, 2018 at 6:33
  • @KDM, would this solution give better resolution than the solution that you present? Commented 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"));
answered Aug 8, 2018 at 6:29
4
  • Can you just feed the file into the Write Method? Commented Aug 8, 2018 at 18:12
  • I don’t think so. You can, of coarse, extract this into a method. Commented Aug 8, 2018 at 18:21
  • I meant without first creating a new BufferedImage? Commented Aug 8, 2018 at 18:23
  • 1
    Imageio works with image objects. So you need to create a new buffered image of RGB type. Commented 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.

answered Aug 8, 2018 at 1:20
5
  • 1
    Renaming a file to JPG doesn't convert the file format! Commented Aug 8, 2018 at 6:25
  • @KDM There are no restrictions mentioned here seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/… besides I ran this code before I gave it as an answer. It created a jpg image for me. The down vote was uncalled for :) Commented Aug 8, 2018 at 15:40
  • 1
    Rename to changes the file name. The content will still be PNG. open the file with a image editor and check the type. Commented Aug 8, 2018 at 15:45
  • unsee.cc/a1caf721 the file is created as a jpg image. Commented Aug 8, 2018 at 16:17
  • Not this will not give you an JPG. You can open it, because the file contains information about its format. The extension it self is irrelevant for this process. Commented Aug 8, 2018 at 17:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.