使用Codeception和Gherkin,我试图弄清楚如何测试文本光标位置的自动更新:
When I click "New post"
Then the blinking text cursor should be in the "Title" field代码如下所示:
<a href="#" id="js-move-text-cursor-to-post-title-input">
New post
</a>
…
<label>
Title
<input type="text" name="title">
</label>
…
<!-- Some JavaScript to set the text cursor to the "Title" input field -->因此,我的问题是,我可以在下面的步骤定义中编写什么来测试此功能?
/**
* @Then the blinking text cursor should be in the :label field
* @param string $label
*/
public function theBlinkingTextCursorShouldBeInTheField(string $label)
{
// @TODO
}tests/acceptance.suite.yml
actor: AcceptanceTester
modules:
enabled:
- Symfony:
part: SERVICES
- Doctrine2:
depends: Symfony
- WebDriver:
url: http://localhost:8000
browser: chrome
- \Helper\Acceptance发布于 2017-07-30 02:01:24
您应该测试按下的键是否出现在预期的字段中,而不是检查光标:
When I click "New post"
When I type "abcd"
Then the "Title" field has the value "abcd"当前来自codeception的API似乎没有提供获取活动元素或在活动字段中键入的方法。
因此,您可能必须使用底层API。
使用$webdriver->switchTo()->activeElement():
// click "New post"
$I->click('#js-move-text-cursor-to-post-title-input');
// type "abcd" in the focused field
$I->executeInSelenium(function($webdriver) {
$webdriver->switchTo()->activeElement()->sendKeys('abcd');
});
// assert that the value "abcd" is in the expected field
$I->seeInField('input[name="title"]', 'abcd');,或使用executeJS:
// click "New post"
$I->click('#js-move-text-cursor-to-post-title-input');
// type "abcd" in the focused field
$I->executeJS('return document.activeElement')->sendKeys('abcd');
// assert that the value "abcd" is in the expected field
$I->seeInField('input[name="title"]', 'abcd');,或使用底层键盘接口:
// click "New post"
$I->click('#js-move-text-cursor-to-post-title-input');
// type "abcd" in the focused field
$I->executeInSelenium(function($webdriver) {
$webdriver->getKeyboard()->sendKeys('abcd');
});
// assert that the value "abcd" is in the expected field
$I->seeInField('input[name="title"]', 'abcd');https://stackoverflow.com/questions/45297478
复制相似问题