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?
-
1See stackoverflow.com/questions/58305/…Bill Hileman– Bill Hileman2018年08月03日 17:59:04 +00:00Commented Aug 3, 2018 at 17:59
-
Are you able to use the TakesScreenshot interface?Aulis Ronkainen– Aulis Ronkainen2018年08月04日 19:51:40 +00:00Commented Aug 4, 2018 at 19:51
3 Answers 3
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.
-
1Could you please specify why should OP go for java.awt.Robot instead of a webdriver that has an implemented screenshot feature?Aulis Ronkainen– Aulis Ronkainen2018年08月04日 20:00:41 +00:00Commented 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.Bill Hileman– Bill Hileman2018年08月06日 13:54:18 +00:00Commented Aug 6, 2018 at 13:54
-
1Ah, 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?Aulis Ronkainen– Aulis Ronkainen2018年08月07日 05:37:53 +00:00Commented Aug 7, 2018 at 5:37
-
1The 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
.Dakshinamurthy Karra– Dakshinamurthy Karra2018年08月08日 06:33:42 +00:00Commented Aug 8, 2018 at 6:33 -
@KDM, would this solution give better resolution than the solution that you present?Malachi– Malachi2018年08月08日 18:10:17 +00:00Commented Aug 8, 2018 at 18:10
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"));
-
Can you just feed the file into the Write Method?Malachi– Malachi2018年08月08日 18:12:07 +00:00Commented Aug 8, 2018 at 18:12
-
I don’t think so. You can, of coarse, extract this into a method.Dakshinamurthy Karra– Dakshinamurthy Karra2018年08月08日 18:21:12 +00:00Commented Aug 8, 2018 at 18:21
-
I meant without first creating a new
BufferedImage
?Malachi– Malachi2018年08月08日 18:23:44 +00:00Commented Aug 8, 2018 at 18:23 -
1Imageio works with image objects. So you need to create a new buffered image of RGB type.Dakshinamurthy Karra– Dakshinamurthy Karra2018年08月08日 18:25:43 +00:00Commented Aug 8, 2018 at 18:25
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.
-
1Renaming a file to JPG doesn't convert the file format!Dakshinamurthy Karra– Dakshinamurthy Karra2018年08月08日 06:25:28 +00:00Commented 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 :)Sairaj M– Sairaj M2018年08月08日 15:40:20 +00:00Commented Aug 8, 2018 at 15:40
-
1Rename to changes the file name. The content will still be PNG. open the file with a image editor and check the type.Dakshinamurthy Karra– Dakshinamurthy Karra2018年08月08日 15:45:57 +00:00Commented Aug 8, 2018 at 15:45
-
unsee.cc/a1caf721 the file is created as a jpg image.Sairaj M– Sairaj M2018年08月08日 16:17:02 +00:00Commented 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.Niels van Reijmersdal– Niels van Reijmersdal2018年08月08日 17:08:40 +00:00Commented Aug 8, 2018 at 17:08
Explore related questions
See similar questions with these tags.