Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Consider table below. We want to click on "view" link in a row that contains "inanzzz" as text.


HTML table


<table>
<tr>
<td>1</td>
<td>hello</td>
<td><a href="http://www.hello.com">view</a></td>
</tr>
<tr>
<td>2</td>
<td>world</td>
<td><a href="http://www.world.com">view</a></td>
</tr>
<tr>
<td>3</td>
<td>inanzzz</td>
<td><a href="http://www.inanzzz.com">view</a></td>
</tr>
<tr>
<td>4</td>
<td>welcome</td>
<td><a href="http://www.welcome.com">view</a></td>
</tr>
</table>

Feature file


Feature: Test feature

Scenario: Scenario description
Given I am on "/"
Then the response status code should be 200
And I click "view" on the row containing "inanzzz"
Then I should see "Behat"

FeatureContext.php


use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext
{
/**
* @When /^I click "([^"]*)" on the row containing "([^"]*)"$/
*/
public function iClickOnOnTheRowContaining($linkName, $rowText)
{
/** @var $row \Behat\Mink\Element\NodeElement */
$row = $this->getSession()->getPage()->find('css', sprintf('table tr:contains("%s")', $rowText));
if (!$row) {
throw new \Exception(sprintf('Cannot find any row on the page containing the text "%s"', $rowText));
}

$row->clickLink($linkName);
}
}