1

I am creating automated test cases by using selenium 3 & testng. Everything looks good, except the screenshots that are generated. Here is my piece of code to create screenshots PNG files:

    file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(file, new File(pngfile));

which is pretty standard way to do it, but the quality of the created PNG file is not so good.

As you can see from following PNG file. In the picture, the email value ( "....@yahoo.com"), which should be at the upper-right corner of the web-page and should be as high as the other navigation bar elements on the left side. But in the created PNG file, this item has been squeezed to the lower level, which is not what I am looking for. Any ideas ? Thanks for the help !

enter image description here

user3595231
  • 711
  • 12
  • 29
  • Did you get a chance to look into [ashot](https://stackoverflow.com/tags/ashot/info) Check the discussion [Selenium: Not able to take complete page screenshot using aShot library](https://stackoverflow.com/questions/50871748/selenium-not-able-to-take-complete-page-screenshot-using-ashot-library) – undetected Selenium Jul 03 '18 at 18:32
  • Thanks DebanjanB. Actually I did, but my feeling is it can handle the case if the web page is Vertically over-sized. In my case, if the page is of full-size, full-screen, everything should be visible, no need to scroll left or right. – user3595231 Jul 03 '18 at 18:43

3 Answers3

0

Make sure your window is the right size when you're opening the browser. You can do this via visual inspection or using Selenium's getSize method. I assume you're using Java, but here it is in Python as well.

Then, if the window is not of the correct size in order to guarantee that your webpage's CSS doesn't break, use setSize. Here is that method in Python as well.

Afterwards, your screenshot should look like the window does.

Community
  • 1
  • 1
natn2323
  • 1,983
  • 1
  • 13
  • 30
  • Thank for the help. What I have just realized is this has something to do with on what host and what type of Driver I am using. A) If it was with a window ChromeDriver, the ScreenShot PNG was perfect as I needed. B) But if I was using the RemoteWebDriver, such as sauceLab, then either on window or Ubuntu box, the PNG file is "squeezed" as I originally reported. C) To use a local ChromeDriver on Unbntu, I have see some other issues, as the test execution breaks quite frequently. – user3595231 Jul 04 '18 at 01:29
0

Please try this,

public void calltakeScreenShot(String SSName) throws Exception
{
    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);      
    BufferedImage img = ImageIO.read(screen);
    File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
    ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + SSName + ".png"));
}

Additionally Here you just need to pre-created Screenshots folder in your Project directory. It will store it by getting absolute path of your project. Also you can manage screenshot name by passing argument.

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
  • Thanks Ishita. I don't have problem to create the screenshot in PNG files, but the quality of the saved screenshot is not that good. This is the issue I am facing now. As I mentioned in another thread, if I was using the chrome driver from sauceLab, which is our default driver ( I am going through the RemoteWebDriver way, and also I have set "fullscreen", "maxsize" during the driver setting), some elements is kind of "squeezed" in the final saved PNG files. – user3595231 Jul 04 '18 at 16:58
0

After several day's research, here is my latest summary.

A). If I was executing the scripts, that is not in the "headless" mode. When the selenium test case is being executed, I will see a new browser session is being popped up and get to that URL, click some buttons, etc, ... till the execution is finished. In this execution, the screenshot page will be saved in good quality.

B). For the same selenium test script, if I am including one extra ChromeOptions setting, which is "--headless", I will not see any browser being brought up during the execution. And once execution is finished, I will get the screenshot with such squeezed web elements.

Comments ?

user3595231
  • 711
  • 12
  • 29