The Mink extension to behat makes it really easy to test the contents of a page. I can use the
assertElementContainsText feature to assert that some text exists within a certain element:
If there is more than one
h1 element on the page, I can use a css selector to increase specificity:
This is really powerful and css selectors make it pretty easy to identify most elements on a page. However, given the block of html below, how do I test the text in a repeated element?
My initial attempt was to use a more advanced css selector:
Unfortuntely, the Symfony 2 web driver does not support this syntax. After talking with a few colleagues, I decided to create a feature that allowed me to do this. Here is an example of my feature syntax:
Here is what the code looks like:
I take advantage of the fact that the repeated elements have a common parent
div.items. I find all children of the
div.items element using the Mink
find API. I can loop over the children and take advantage of the fact that the children are of type
NodeElement class shares the same base class as
DocumentElement object returned from
$this->getSession()->getPage() call. When I use the
find method on the
$child object, I will only search for elements that are within the context of the current child. Here is what that context looks like on the first iteration:
Now when I search for the element matching
div.item-row-name, I only get back the one element within this context. I can then assert that this text within this element matches the corresponding item in my table.
Notice that I use a PHPUnit assertion within this feature. I would have preferred to re-use an existing Mink web assertion, but all of the assertions assume a global context. Look at the elementTextContains code to see what I mean.