From 3a7a015c0dbd41661c1a082d0ed57c33fa62b26a Mon Sep 17 00:00:00 2001 From: ryneeverett Date: Tue, 12 Jan 2016 00:42:37 -0500 Subject: [PATCH] Add debug test method. Hopefully this will help figure out what's going wrong with the selenium tests (#2). --- test/test_browser.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/test_browser.py b/test/test_browser.py index a68f772..7b1392a 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1,5 +1,8 @@ +import re import sys import time +import inspect +import tempfile import unittest from django.contrib.staticfiles.testing import StaticLiveServerTestCase @@ -86,6 +89,33 @@ def fillPerson(self, person): self.jQueryDialog( ".find('#id_entity').val('{person}')".format(person=person)) + def debug(self, _callable): + """ + Capture screen and html before executing the callable. + + WARNING: Taking a screenshot can cause phantomjs tests to pass. + """ + # Introspect. Adapted from http://stackoverflow.com/a/8856387/1938621. + caller_frame = inspect.currentframe().f_back + frame_info = inspect.getframeinfo(caller_frame) + + lineno = frame_info.lineno + + lines = ''.join([line.strip() for line in frame_info.code_context]) + line = re.search(r'debug\((.+?)\)$', lines).group(1) + + # Capture screen and html. + filename = '-'.join([str(lineno), line, WEBDRIVER.upper()]) + + self.browser.screenshot(name=filename) + + with tempfile.NamedTemporaryFile( + prefix=filename, suffix='.html', delete=False) as f: + f.write(self.browser.html.encode('utf-8')) + + # Call callable. + _callable() + class TestAdmin(SplinterTestCase):