0

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.

Community
  • 1
  • 1
Lee Le
  • 337
  • 1
  • 4
  • 10

3 Answers3

0

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();
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
EGHM
  • 2,144
  • 23
  • 37
0

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.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Guess I need to explain it again. Say a user logs into my app, and clicks a few buttons, tabs and opens a modal dialog. How do I take a screenshot of the page the user is looking at? – Lee Le Sep 25 '15 at 03:07
  • @LeeLe You mean a real user or a user simulated with Selenium? If you mean a real user, you can't with Selenium. You'd have to have the user on a desktop you controlled and remotely trigger a screenshot. Selenium is for writing a script that simulates a user performing actions on a site. – JeffC Sep 25 '15 at 04:48
0

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'.
}
Ravichandra
  • 2,162
  • 4
  • 24
  • 36