Finding inside HTML Tables

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

To finish the scenario, we need a Then that's able to look for a check mark on a specific row. The check mark icon itself is an element with a fa fa-check class.

There is no built in definition to find elements inside of a specific row. So let's describe this using natural language. How about:

... lines 1 - 27
Then the "Foo1" row should have a check mark
... lines 29 - 43

Execute Behat to get that definition printed out for us:

./vendor/bin/behat features/product_admin.feature:21

When you're feeling really lazy, you can add a --append-snippets flag and Behat will put the definitions inside of the FeatureContext class for you:

./vendor/bin/behat features/product_admin.feature:21 --append-snippets

Change arg1 to be rowText:

... lines 1 - 118
* @Then the :rowText row should have a check mark
public function theProductRowShouldShowAsPublished($rowText)
... lines 124 - 127
... lines 129 - 231

Ok, this is a bit harder. First, we need to find a row that contains that $rowText and then look inside of just that element to see if it has a fa-check class in it.

Start by finding via CSS, $row = $this->getPage()->find('css'). For the selector, use table tr: and then the contains pseudo selector that looks for some text inside. Pass %s and set the value using sprintf():

... lines 1 - 123
$row = $this->getPage()->find('css', sprintf('table tr:contains("%s")', $rowText));
... lines 125 - 231

$row will now be the first tr containing this text, or null. It's not perfect: if $rowText had some bad characters in it, the selector would fail. But this is my test so I'll be lazy until I can't. Add assertNotNull() function:

... lines 1 - 124
assertNotNull($row, 'Cannot find a table row with this text!');
... lines 126 - 231

Finally, assert that the row's HTML has a fa-check class inside of it with assertContains():

... lines 1 - 126
assertContains('fa-check', $row->getHtml(), 'Could not find the fa-check element in the row!');
... lines 128 - 231

Moment of truth:

./vendor/bin/behat features/product_admin.feature:21

We're green! Now, let's get even harder.

Leave a comment!

This tutorial uses a very old version of Symfony. The fundamentals of Behat are still valid, but integration with Symfony will be different.

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": ">=5.4.0, <7.3.0",
        "symfony/symfony": "^2.7", // v2.7.4
        "twig/twig": "^1.22", // v1.22.1
        "sensio/framework-extra-bundle": "^3.0", // v3.0.16
        "doctrine/doctrine-bundle": "^1.5", // v1.5.1
        "doctrine/orm": "^2.5", // v2.5.1
        "doctrine/doctrine-fixtures-bundle": "^2.2", // v2.2.1
        "behat/symfony2-extension": "^2.0" // v2.0.0
    "require-dev": {
        "behat/mink-extension": "^2.0", // v2.0.1
        "behat/mink-goutte-driver": "^1.1", // v1.1.0
        "behat/mink-selenium2-driver": "^1.2", // v1.2.0
        "phpunit/phpunit": "^4.8" // 4.8.18