[twill] Capturing Error Output/Nicely

Stephen Thorne stephen at netboxblue.com
Thu Apr 27 15:12:45 PDT 2006

Terry Peppers wrote:
> I think Titus knows that I really enjoy using Twill. And things are 
> heating up here @ work so that we'll end up using it more. But I think 
> one of my personal hangups with it is trying to track down errors that 
> occur in our application using Twill's 'show' command. I don't know 
> about anyone else, but my eyes just glaze over when I look @ HTML in a 
> terminal. So I got to thinking, isn't there a better way to do this?
> Couldn't I write something in my Python scripts that could take a failed 
> assert, output Twill's 'show', Tidy it up and then pass it to Element 
> Tree or some other parser and output things that might be relevant to 
> me? Maybe the page title or some blocks of text or maybe error messages 
> are passed in a <div> block that has a name of 'error.'
> Just thinking out loud, and something I'd like to see if others in the 
> Twill community are already doing.

Interesting. Hows this?


extend_with showstuff
go http://shiny.thorne.id.au/shiny/
tag title
tag_by_id div footer
tags_by_class div post


from twill.commands import get_browser
from BeautifulSoup import BeautifulSoup

def textify(node, pre=0):
     if isinstance(node, (str, unicode)):
         if pre:
             return node.strip() + '\n'
             return node.strip()

     strs = []

     for n in node:
         strs.append(textify(n, pre=(pre or node.name == 'pre')))

     if node.name in ('br', 'p', 'div'):
         return ' '.join(strs).strip() + '\n'
         return ' '.join(strs).strip()

def get_soup():
     return BeautifulSoup(get_browser().get_html())

def tag_by_id(tag, xmlid):
     soup = get_soup()
     print textify(soup.first('div',{'id':xmlid}))

def tags_by_class(tag, name):
     soup = get_soup()
     for tag in soup(tag, {'class':name}):
         print textify(tag)

def tag(tag):
     soup = get_soup()
     print textify(soup.first(tag))

__all__ = ['tags_by_class', 'tag_by_id', 'tag']

Stephen Thorne
Development Engineer

Scanned by the NetBox from NetBox Blue

More information about the twill mailing list