Created
January 22, 2015 10:22
Python script that demos how to use EventFiringWebDriver to capture screenshots after test failures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# * Note: phantomjs must be in your PATH | |
# | |
# This script: | |
# - Navigates to www.google.com | |
# - Intentionally raises an exception by searching for a nonexistent element | |
# - Leaves behind a screenshot in exception.png | |
import unittest | |
from selenium import webdriver | |
from selenium.webdriver.support.events import EventFiringWebDriver | |
from selenium.webdriver.support.events import AbstractEventListener | |
class ScreenshotListener(AbstractEventListener): | |
def on_exception(self, exception, driver): | |
screenshot_name = "exception.png" | |
driver.get_screenshot_as_file(screenshot_name) | |
print("Screenshot saved as '%s'" % screenshot_name) | |
class TestDemo(unittest.TestCase): | |
def test_demo(self): | |
pjsdriver = webdriver.PhantomJS("phantomjs") | |
d = EventFiringWebDriver(pjsdriver, ScreenshotListener()) | |
d.get("http://www.google.com") | |
d.find_element_by_css_selector("div.that-does-not-exist") | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment