Behat open link on a table row with text
Well this is a simple blog post. I needed a behat test to click an edit button in a table row... and I was struggling to figure out just how to make it work. Then I found the attached link which had the following code examples. I have copied these solely to make sure they persist incase their website ever goes away or down or whatever.
The table in question:
<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>
The 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"
The Context Code:
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);
}
}