Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Allow clicking on text directly in an element #290

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,13 @@ public function iClickOnTheElementConfirmingTheDialog($selector)
}

/**
* @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/
* @Given /^I (click|double click) "([^"]*)"(| directly) in the "([^"]*)" element$/
* @param string $clickType
* @param string $text
* @param string $directly If used, the text must be directly in the element. Otherwise it can be in a child element.
* @param string $selector
*/
public function iClickInTheElement($clickType, $text, $selector)
public function iClickInTheElement($clickType, $text, $directly, $selector)
{
$clickTypeMap = array(
"double click" => "doubleclick",
Expand All @@ -510,7 +511,13 @@ public function iClickInTheElement($clickType, $text, $selector)
$page = $this->getSession()->getPage();
$parentElement = $page->find('css', $selector);
Assert::assertNotNull($parentElement, sprintf('"%s" element not found', $selector));
$element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text));
if ($directly) {
// Finds the specific text within the selector element (to validate it's there), and then grabs the element that holds the text
$element = $parentElement->find('xpath', sprintf('/text()[contains(.,"%s")]/..', $text));
} else {
// Finds a child of the selector element which contains the text
$element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text));
}
Assert::assertNotNull($element, sprintf('"%s" not found', $text));
$clickTypeFn = $clickTypeMap[$clickType];
$element->$clickTypeFn();
Expand Down