Browser tools (reahl-browsertools)¶
Reahl browser tools provides an interface to Selenium WebDriver that simplifies tests that deal with ajax. It also includes programmatically composable XPaths that are easy to read in code.
Composing XPaths programmatically¶
XPath can be used to compose complicated XPath expressions in a readable, consistent way.
Creating an XPath¶
To create an
XPath, call a class method for a simple element:
>>> from reahl.browsertools.browsertools import XPath >>> XPath.div() XPath('//div')
To obtain the raw xpath expression, cast an
XPath to a string:
>>> str(XPath.div()) '//div'
Call methods on an
XPath instance to compose a larger expression:
>>> XPath.div().including_class('myclass') XPath('//div[contains(concat(" ", @class, " "), " myclass ")]')
These methods can be chained, since each one returns an
>>> XPath.div().including_class('myclass').including_text('hello') XPath('//div[contains(concat(" ", @class, " "), " myclass ")][contains(normalize-space(), normalize-space("hello"))]')
Since methods like
including_text() can be called on any
XPath, you get
standardised behaviour such as that spaces are always normalised: if you pass text with a single space in it, but
the HTML is laid out with more than one space, the resulting expression will still match.
Less simple starting elements¶
Smart browser interfaces¶
These methods always automatically wait for the operated-on element to appear, so you don’t have to write that in
your tests. Where sensible, they also wait for possible ajax action to complete before returning, as in the case
Why wait? Because typing a value and tabbing out to the next field might trigger changes to the page. You want to wait for the page to change before performing another action.
For consistency when not using webdriver,
Browser provides a similar interface to WebTest. This makes for faster tests