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.

With example below, we will click on the link in a <li> element that contains "inanzzz" as text.


HTML li menu


<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 file


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();
}
}