[TIP] Testing HTML using CSS selectors

Kumar McMillan kumar.mcmillan at gmail.com
Thu Mar 1 13:21:13 PST 2007


wow!  great idea for assertions.  I haven't worked too directly with
web response assertions but I do a lot with straight xml, for testing
services.  I believe BeautifulSoup and the like will tidy up html
enough to make it loadable by lxml.xpath() [1] ?  Xpath is amazing and
very easy to learn [2].  But, having CSS selectors are great for
people who don't know xpath (and getting more people to write
functional tests is always good).  Just for comparison, here is an
example of how to do what you're talking about in xpath:

tree = lxml.xpath(tidied_response)
assert tree.find("div[@id=messages]") is not None
msgs = tree.find("div[@id=messages]/ul/li")
eq_(msgs[0].text, 'Test message one')
eq_(msgs[1].text, 'Test message two')
eq_(msgs[2].text, 'Test message three')

[1] http://codespeak.net/lxml/api.html#xpath
[2] http://www.w3schools.com/xpath/

On 3/1/07, Simon Willison <simon at simonwillison.net> wrote:
> Hi all,
>
> I've been testing my current Web project using BeautifulSoup.
> BeautifulSoup is a wonderful tool, but then I heard about
> assert_select in Rails:
>
> http://push.cx/2007/rails-121-impression
>
> Running assertions against an HTML document using CSS selectors was
> just too good an idea to miss, so I've put together a simple extension
> to BeautifulSoup that allows you to do exactly that:
>
> http://code.google.com/p/soupselect/
>
> Here's some example lines from one of my tests:
>
> response = client.get('/test/show_messages/')
> soup = Soup(response.content)
> self.assert_(soup.findSelect('div#messages'), 'No messages div')
> lis = soup.findSelect('div#messages ul li')
> self.assertEqual(len(lis), 3)
> self.assertEqual(lis[0].string, 'Test message one')
> self.assertEqual(lis[1].string, 'Test message two')
> self.assertEqual(lis[2].string, 'Test message three')
>
> My implementation is pretty limited at the moment - it does class, ID
> and attribute selectors but doesn't do child selectors or
> pseudo-selectors yet. I eventually plan to have it cover everything
> from CSS 2.1, as described here:
>
> http://www.456bereastreet.com/archive/200509/css_21_selectors_part_1/
>
> Cheers,
>
> Simon
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>



More information about the testing-in-python mailing list