Skip to content

Commit

Permalink
Assert.css, closes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
dag committed Jan 2, 2011
1 parent c829faf commit 87449fb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
18 changes: 18 additions & 0 deletions attest/__init__.py
Expand Up @@ -868,6 +868,24 @@ def json(self):
import json
return Assert(json.loads(self.obj))

def css(self, selector):
"""Parse the wrapped object as
:abbr:`HTML (HyperText Markup Language)` and return a list (wrapped
in :class:`Assert`) of elements matching the
:abbr:`CSS (Cascading Style Sheets)` *selector*. Requires lxml 2.0
or newer.
.. note::
Not tested on Python 2.5 and PyPy due to difficulties
installing lxml on these implementations.
.. versionadded:: 0.4
"""
from lxml import html
return Assert(html.fromstring(self.obj).cssselect(selector))

def __repr__(self):
"""Not proxied to the wrapped object. To test that do something
like::
Expand Down
23 changes: 23 additions & 0 deletions attest/tests/asserts.py
Expand Up @@ -251,3 +251,26 @@ def json():

with Assert.raises(AssertionError):
Assert('{"works": true}').json == dict(works=False)


try:
import lxml
except ImportError:
lxml = None

@suite.test_if(lxml)
def css():
"""Assert.css"""

html = Assert("""
<div id="maincontent">
<div class="container">
<p>Hello World</p>
</div>
</div>
""")

html.css('#maincontent .container p')[0].text == 'Hello World'

with Assert.raises(AssertionError):
html.css('#maincontent .container p')[0].text != 'Hello World'
3 changes: 2 additions & 1 deletion docs/api.rst
Expand Up @@ -39,7 +39,8 @@ Asserting conditions
--------------------

.. autoclass:: Assert
:members: isinstance, not_isinstance, issubclass, not_issubclass, json,
:members:
isinstance, not_isinstance, issubclass, not_issubclass, json, css,
__str__, __getattr__, __call__, __getitem__, __eq__, __ne__, is_,
is_not, __contains__, in_, not_in, __lt__, __le__, __gt__, __ge__,
__nonzero__, __repr__
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -2,6 +2,7 @@
envlist = py25, py26, py27, py31, pypy, docs

[testenv]
deps = lxml>=2.0
commands = python runtests.py

[testenv:py25]
Expand Down

0 comments on commit 87449fb

Please sign in to comment.