问题
Is there a way to assert that an element (e.g. input or link) has the keyboard focus? I'm using Codeception with Selenium. Couldn't find anything on http://codeception.com/docs/modules/WebDriver
回答1:
A reliable source told me this works:
$I->executeJS('return $("#element").is(":focus")');
Happy testing!
回答2:
Some more details to the answer by @sunomad
Activate Codeception's Asserts Module in your acceptance.suite.yml
:
modules:
enabled:
# ...
- Asserts
Then use this in an Acceptance test:
$focus = $I->executeJS('return $("#element").is(":focus")');
$I->assertEquals(true, $focus);
Works (through Selenium) with Firefox and Chrome. However, it does not work with PhantomJS - see this issue: https://github.com/ariya/phantomjs/issues/10427
回答3:
To complete the @sunomad's answer. If you use the code line :
$I->executeJS('return $("#element").is(":focus")');
You got this error :
[Facebook\WebDriver\Exception\UnknownServerException] unknown error: $ is not a function
Switch $
by jQuery
$I->executeJS('return jQuery("#element").is(":focus")');
And it works !
来源:https://stackoverflow.com/questions/44096567/codeception-check-if-element-has-focus