首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用协同欺骗和WebDriver测试文本光标位置

使用协同欺骗和WebDriver测试文本光标位置
EN

Stack Overflow用户
提问于 2017-07-25 16:10:03
回答 2查看 666关注 0票数 1

使用Codeception和Gherkin,我试图弄清楚如何测试文本光标位置的自动更新:

代码语言:javascript
复制
When I click "New post"
Then the blinking text cursor should be in the "Title" field

代码如下所示:

代码语言:javascript
复制
<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 -->

因此,我的问题是,我可以在下面的步骤定义中编写什么来测试此功能?

代码语言:javascript
复制
/**
 * @Then the blinking text cursor should be in the :label field
 * @param string $label
 */
public function theBlinkingTextCursorShouldBeInTheField(string $label)
{
    // @TODO
}

tests/acceptance.suite.yml

代码语言:javascript
复制
actor: AcceptanceTester
modules:
    enabled:
        - Symfony:
            part: SERVICES
        - Doctrine2:
            depends: Symfony
        - WebDriver:
            url: http://localhost:8000
            browser: chrome
        - \Helper\Acceptance
EN

Stack Overflow用户

发布于 2017-07-30 02:01:24

您应该测试按下的键是否出现在预期的字段中,而不是检查光标:

代码语言:javascript
复制
When I click "New post"
When I type "abcd"
Then the "Title" field has the value "abcd"

当前来自codeception的API似乎没有提供获取活动元素或在活动字段中键入的方法。

因此,您可能必须使用底层API。

使用$webdriver->switchTo()->activeElement()

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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');

,或使用底层键盘接口:

代码语言:javascript
复制
// 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');
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45297478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档