Aşağıdaki örnekte, <li> elementi ile sarılmış olan ve içinde "inanzzz" kelimesini barındıran linke tıklayacağız.


HTML li menü


<ul class="nav">
<li><a href="http://www.google.com">google</a></li>
<li>
<a>Users</a>
<ul>
<li><a href="http://www.inanzzz.com">inanzzz</a></li>
<li><a href="http://www.hello.com">hello</a></li>
</ul>
</li>
<li><a href="http://www.world.com">world</a></li>
</ul>

Feature dosyası


Feature: Test feature

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

FeatureContext.php


use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext
{
/**
* @When /^I click li option "([^"]*)"$/
*
* @param $text
* @throws \InvalidArgumentException
*/
public function iClickLiOption($text)
{
$session = $this->getSession();
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '*//*[text()="'. $text .'"]')
);

if (null === $element) {
throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
}

$element->click();
}
}