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?

1

4 Answers 4

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

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

This code will definitely help

Sign up to request clarification or add additional context in comments.

Comments

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...

6 Comments

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.
I'm really interested in this. Why does that resizing matter to you, what does it break?
@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).
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
Yeah, IE has its screenshotting logic hidden here. 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.
|
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

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.