【问题标题】:Testing text cursor position using Codeception and WebDriver使用 Codeception 和 WebDriver 测试文本光标位置
【发布时间】:2017-07-25 08:10:03
【问题描述】:

使用 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

【问题讨论】:

    标签: php selenium webdriver bdd codeception


    【解决方案1】:

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

    When I click "New post"
    When I type "abcd"
    Then the "Title" field has the value "abcd"
    

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

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

    $webdriver-&gt;switchTo()-&gt;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');
    

    【讨论】:

      【解决方案2】:

      这似乎与另一个问题非常相似:https://stackoverflow.com/a/44165078/1668200

      也许该问题的答案会对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-08-13
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多