Skip to content

Commit acc744c

Browse files
Merge pull request #310 from creative-commoners/pulls/6/new-method
NEW Add theFieldShouldHaveFocus method
2 parents 4c6f88b + 87a3009 commit acc744c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/Context/BasicContext.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,31 @@ public function iFocusOnTheElement(string $selector)
16631663
$element->focus();
16641664
}
16651665

1666+
/**
1667+
* Checks if an element has focus.
1668+
*
1669+
* Example: Then the ".some-css-selector" element should have focus
1670+
* @Then /^the "([^"]*)" element should have focus$/
1671+
*/
1672+
public function theFieldShouldHaveFocus(string $selector)
1673+
{
1674+
$page = $this->getSession()->getPage();
1675+
$element = $page->find('css', $selector);
1676+
Assert::assertNotNull($element, sprintf('Element %s not found', $selector));
1677+
$sel = str_replace('"', '\\"', $selector);
1678+
$script = <<<JS
1679+
return (function() {
1680+
var el = document.querySelector("$sel");
1681+
if (!el) {
1682+
return false;
1683+
}
1684+
return el == document.activeElement;
1685+
})();
1686+
JS;
1687+
$res = $this->getSession()->evaluateScript($script);
1688+
Assert::assertTrue($res);
1689+
}
1690+
16661691
/**
16671692
* @Given /^I type "([^"]+)" in the field$/
16681693
*

0 commit comments

Comments
 (0)