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!

  • 2020-04-20 Diego Aguiar

    Ah, yes, he got this same problem but when working with Codeception, the work around it's the same, just install PHPUnit and ignore the warning :)

  • 2020-04-20 Cristóbal Rejdych

    Hey Diego ;)
    First of all thanks for quick reply. Yeah I saw this page before when I looked for solution, but that guy was talking about component called codeception which looks for me like another library to making tests. I was thinking about install PHPUnit and leave PHPUnitBridge too but it looks like duplication of code and I was affraid to break PHPUnitBridge. So that what I supposed to do? Just install PHPUnit without remove anything and ignore warnings?

  • 2020-04-20 Diego Aguiar

    Hey Cristóbal Rejdych

    It's not a stupid question at all, actually, you hit a pesky bug! Check at this thread

    TL-DR If you need to work with PHPUnit directly, you will have to install that depedency ignoring the warnings coming from Symfony PHPUnitBridge


  • 2020-04-19 Cristóbal Rejdych

    Hi Diego,
    probably I had another stupid question but I can't find the answer so maybe you can help me. What about when I'm using PHPUnit Bridge (v5.0) and I want use asserts in behat.I found that the path to asserts functions is in other directory (not in vendor dir but in bin), but when I try to require it with new path

    require_once __DIR__.'/../../bin/.phpunit/phpunit-7.5-0/src/Framework/Assert/Functions.php';

    or by using namespaces

    use PHPUnit\Framework\Assert;

    I still get an error

    Fatal error: Class 'PHPUnit\Framework\Assert' not found (Behat\Testwork\Call\Exception\FatalThrowableError)

    Thanks a lot for any help.

  • 2017-04-13 Jon Robinson

    Thanks Diego.

    I wasn't able to get it working today, but i'll keep trying

  • 2017-04-12 Diego Aguiar

    Hey Jon!

    That's totally possible with the find method, using CSS selector, brings you all you can do with normal CSS, so you could do something like this:

    $row = $this->getSession()->getPage()->find('css', sprintf('tr:contains("%s")', $rowText));

    $td = $row->find('css', sprintf('td:nth-child(%s)', $columnNumber));

    assertEquals($text, $td->getText());

    I didn't test it (it should work), but it should give you an idea of what can you do :)

    Have a nice day!

  • 2017-04-12 Diego Aguiar

    Hahaha, don't worry! It happens to all of us, I preffer to don't talk about the dummy things I've done :p

  • 2017-04-12 Jon Robinson

    On a separate note... can Behat / PHPUnit assert some text in a cell in a row containing some text, but also based of the what column the cell is in


    Adapting Then I should see (the text ):text in the :rowText row
    .... with Then I should see (the text ):text in the :rowText row in the Column called "column4"

  • 2017-04-12 Jon Robinson

    Thanks very much... it was a stupid one!

    Keep up the good work

  • 2017-04-11 Diego Aguiar

    Hey Jon!

    I think you forgot to require PHPUnit assert functions library, just add this line to the top of your Context class (Adjust the path to your needs)
    require_once __DIR__.'/../../../vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';

    Have a nice day!

  • 2017-04-11 Jon Robinson

    Hello and thank you for this screencast. It's really superb and helpful.

    So i'm having difficulties with this feature... finding the fa check in the row. I'm just getting Fatal error: Call to undefined function assertNotNull() in /Users/...

    Any idea why? I have PHPUnit installed, so i've obviously missed something stupid?

    Thanks in advance.