Selenium Tips: Capturing Screenshots vs. Scrollbars!

Posted Dec 12th, 2009

Capturing screenshots of your tests is one of the most important features you can give to an automation engineer. It's the easiest way to actually understand from a report, why a test failed and how to reproduce it. While our OnDemand will help you record videos of your entire test, sometimes you just want a screenshot. Selenium supports capturing screenshots on remote machines, as Sean discussed in a previous Tip of the Week (Taking ScreenShots on the Server). Selenium has two methods dedicated to screenshot taking:

  • CaptureScreenshot
  • CaptureEntirePageScreenshot

An example of using the later function shows that it's pretty amazing, giving you a full rendering of the page: Entire Page Screenshot This just isn't going to work if we want to test something below the fold. There is a partial workaround, focus(). Focus will force all browsers to scroll the page so the selected element is visible. Here's an example of a test we run to view the bottom of our homepage:

def test_case(self):
        browser = self.browser
        browser.open("/")
        browser.focus("css=a.indexTwitter")
        png = browser.capture_screenshot_to_string()
        f = open('screenshot.png', 'wb')
        f.write(png.decode('base64'))
        f.close()

Regular Plus Focus Screenshottwitter Santiago


Written by

Santiago Suarez Ordoñez