2

I am trying to take a screenshot of a webpage while selenium is running. I am using the following code for this purpose

WebDriver augmentedDriver = new Augmenter().augment(seleniumDriver);
            File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);

Now it serves my purpose perfectly well except that whenever this method gets called the browser automatically gets into the default size and maximizes again.

And this continues every time the screenshot function gets called. I am able to solve the problem If I am NOT using the selenium webdriver for taking the screenshots and using other java functions.

I wanted to know if anyone had similar problems/why I am having this problem. And is there any workaround?

Rabimba Karanjai
  • 186
  • 2
  • 5
  • 14

4 Answers4

2
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("D:\\screenshot.jpg"));

This code will definitely help

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Manasa
  • 21
  • 2
1

It tries to adapt to page size and take the screenshot as small as possible or as large as needed for the whole page to fit. Apart from being annoying, it should not be a cause of any other problems and is therefore considered a better solution than taking a screenshot of just the actual viewport which could be missing some important piece of the page you're trying to examine.

If you're not happy about it, use Robot and its createScreenCapture() method.


Or, but it will only work for Firefox, you may try overriding the FirefoxDriver's method for screenshots. Not tested, no idea whether you'll be allowed to do it or not.

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("FirefoxDriver.prototype.screenshot = function(a){};");

and (if that's not enough) maybe even

js.executeScript("FirefoxDriver.prototype.saveScreenshot = function(a,b){};");

Inferred from here. The actual screenshooting code is here. You can replace the FirefoxDriver.prototype.screenshot function with your own that wouldn't take the maximum scrollable values for height and width...

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • I did try the Robot CreateScreenCapture() method and now it seems like my only option. because the adaptive re-size is causing problems with my other methods. – Rabimba Karanjai Jun 09 '12 at 09:26
  • I'm really interested in this. Why does that resizing matter to you, what does it break? – Petr Janeček Jun 09 '12 at 09:28
  • @RabimbaKaranjai Also, I edited the answer with a possible new solution that prevents FF to take the screenshot. You can look into the screenshooter.js to make a new screenshooting script that woudn't resize the window (refactor the `width` and `height` usage there). – Petr Janeček Jun 09 '12 at 09:49
  • I'm using IE primarily to run the tests. So I should be changing the Firefox driver to selenium webdriver. Will have a go with this – Rabimba Karanjai Jun 09 '12 at 10:48
  • Yeah, IE has its screenshotting logic hidden [here](http://code.google.com/p/selenium/source/browse/trunk/cpp/IEDriver/CommandHandlers/ScreenshotCommandHandler.h). It's a C++ file and you could provide your version of IEDriver.dll to Selenium ... I just don't think it's worth the effort. – Petr Janeček Jun 09 '12 at 10:49
  • Yeah....At present i'm using the robot screenshot functionality. Was searching for a better solution. The by default webdriver screenshot was not a problem with me till I started calling it after every step (even the passed one). Only then this problem became apparent and also some of mu methods started failing becuase of the frequent resizing. @slanec – Rabimba Karanjai Jun 09 '12 at 10:57
0

The code to take the screenshot make use of getScreenshotAs method of TakesScreenshot interface. Following code will take screenshot of the webpage opened by webDriver instance.

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));

Now in order to take screenshot in case of test failure we will use AfterMethod annotation of TestNG. In the AfterMethod annotation we will use ITestResult interface's getStatus() method that returns the test result and in case of failure we can use the above commands to take screenshot. Code snippet to take screenshot on test failure-

@AfterMethod
public void takeScreenShotOnFailure(ITestResult testResult) throws IOException {
    if (testResult.getStatus() == ITestResult.FAILURE) {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
   }        
}

For complete sample script refer to http://artoftesting.com/automationTesting/screenShotInSelenium.html

0

I have created a method for this, which is very easy to use. It takes the screenshot of the whole web page. It counts and name the screen shot depending on the number of screenshots, the method also stores the screenshot as .png file into src.

public static void screenshot(WebDriver driver) 
{

    System.out.println("Taking the screenshot.");   
    console_logs("Taking the screenshot.");
    scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
        FileUtils.copyFile(scrFile, new File("src//tempOutput//webPage_screenshot_"+count+".png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    count++;
    //return scrFile;
}

Using the method:

screenshot(driver);
Salman Arshad
  • 59
  • 1
  • 14