Testing Repeated Elements With Behat+Mink
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:
{% highlight gherkin %} Then I should see "My Page Title" in the "h1" element. {% endhighlight %}
If there is more than one h1
element on the page, I can use a css selector to increase specificity:
{% highlight gherkin %} Then I should see "My Page Title" in the "h1.page-title" element. {% endhighlight %}
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?
{% highlight html %}
Item 1
Item 2
Item 3
My initial attempt was to use a more advanced css selector:
{% highlight gherkin %} Then I should see "Item 1" in the "div.item-row:nth(0)" element. {% endhighlight %}
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:
{% highlight gherkin %} Then I should see the following in the repeated "div.item-row-name" element within the context of the "div.items" element: | text | | Item 1 | | Item 2 | | Item 3 | {% endhighlight %}
Here is what the code looks like:
{% highlight php %}
[^"]*)" element within the context of the "(?PItem 1