Codeception: Check if element has focus?

做~自己de王妃 提交于 2019-12-10 21:58:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!