Element must be user-editable in order to clear it (Behat)

人走茶凉 提交于 2019-12-06 05:19:40
Ian Bytchek

I assume you are using PHP and Angular, which doesn't make it a very standard date field. Doing the following (Behat 3) doesn't throw any errors up until the last step, because it doesn't change the actual value either.

/**
 * @When /^I select a "(?P<id>.+)" date of "(?P<date>.+)"$/
 *
 * @param $id
 * @param $date
 */
public function startDate($id, $date)
{
    $this->getSession()->getPage()->find('css', '#' . $id)->setValue($date);
}

@javascript
Scenario: Test
    When I go to "https://docs.angularjs.org/examples/example-date-input-directive/index.html"
    And I select a "exampleInput" date of "2013-11-11"
    Then I should see "2013-11-11"

It looks like that the component must receive a value from the javascript, especially looking at this.

Edit:

To do this with JS you can use the following, though if a person familiar with Angular might suggest a simpler approach:

/**
 * @When /^I select date of "(?P<date>.+)"$/
 *
 * @param $date
 */
public function startDate($date)
{
    $element = $this->findElement('//*[@id="exampleInput"]');

    $script = '
        (function(){
            var element = document.evaluate(\'' . $element->getXpath() . '\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            var $scope = angular.element(element).scope();
            $scope.$apply(function() {
                $scope.value = new Date(\'' . $date . '\');
            });
        })();
        ';

    $this->getSession()->executeScript($script);
}

@javascript
Scenario: Test
    When I go to "https://docs.angularjs.org/examples/example-date-input-directive/index.html"
    And I select date of "2013-11-11"
    Then I should see "2013-11-11"

The test passes, the value gets updated.

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