Debugging Functional Browser tests
I've just had one of those slap-yourself-on-the-forehead-why-haven't-I-thought-of-this-before moments that I'd like to share with my esteemed readership.
In Martin Aspeli's new (and highly recommended) book Professional Plone Development he shares a handy little tidbit regarding the debugging of the raw console output of a browser test's page contents:
"Because reading raw HTML in the console can be a little cumbersome, the author sometimes uses this trick during debugging:
>>> open('/tmp/test-output.html', 'w').write(browser.contents)
[...] When executed, this statement will write the current contents of the test-browser to
/tmp/test-output.html. Images and stylesheets will not work, but the text in the page is normally enough to understand why some output is not as expected."
That's a real life-saver right there. But it can still be a bit cumbersome if you're debugging a form interactively. So I've done the following:
-
Subclass Five's
Browserclass - add a convenience method for dumping the contents
- in tests import the extended browser class
In a nutshell this is what I added to my current project's base.py test class:
from Products.Five.testbrowser import Browser as BaseBrowser
[...]
class Browser(BaseBrowser):
def dc(self, filename="/tmp/test-output.html"):
open(filename, 'w').write(self.contents)
In my tests I use the abovementioned browser instead of Five's and whenever I'm in a debug session, I just enter browser.dc() to get a 'snapshot' of the current browser page. The tmp-file is left open in my editor (TextMate, which does an excellent job of picking up refreshed contents)...
Debugging a form with a large number of fields, spread out over multiple pages has just become a whole lot easier ;-)
