0
votes

I've been looking at how to use selenium web driver to take screenshot of a web page. The result is looking quite promising. The only problem I have is from all the examples I could find (e.g. Take a screenshot with Selenium WebDriver), it only lets you navigate to a web page, and then take a snapshot which only gives you the initial state of the page. However, the web pages I need to take screenshot of has many JS content and user interactions. Is it possible to set the current state of a web page in selenium?

For example, a user logs in my app, and clicks a few buttons, tabs and opens a modal dialog. How do I take a screenshot of the page with all the user interactions the user has performed?

What I can think of is sending the entire HTML document to server and generate a static html page, and then let webdirver take a screenshot of the static html page.

Thx in advance.

3

3 Answers

0
votes

You can take a screenshot of the page whenever you want... just call the code when you want to capture the current state of the page. You already have the code in that link.

0
votes

You can, but as far as I know you have to still call the code to take the screenshot yourself. So what we have done is create utility methods which we call to interact with the webpage and take a screenshot after every interaction. So for example we have lots of methods similar to this one:

protected void waitAndSelectLabeled(String label, String text) throws InterruptedException {
    jGrowl("Select " + text + " labeled with " + label);
    waitAndSelectBy(By.xpath("//th/label[contains(text(), '" + label + "')]/../following-sibling::*//select"), text);
    screenshot();
}
0
votes

you may call some similar function when ever you want

private static void screenshot(int i)
{
  driver.manage().window().maximize(); \\Maximize to capture complete screen  
  Thread.sleep(1000);
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("C:\\screenshot"+i+".png"), true); \\To avoid overwritting of screenshots. We used 'i'.
}