为什么 Behat\Mink\Element\NodeElement:setValue 从传递的值中删除破折号?

Why Behat\Mink\Element\NodeElement:setValue removes dashes from passed value?

我正在使用 Behat:3.0.15 与 Selenium 3.4 和 PhantomJS 作为浏览器。

我有自定义步骤来填充输入日期字段的值。基本上是一行:

$element->setValue('1999-01-01');

我注意到在下一个场景步骤中,结果值为 19990101 并且我的字段未通过验证。

因为 setValue 不只是 "set a value" 在现场

如果你查看 Selenium2DriversetValue 函数的实现,你会发现实际输入的值定义在以下代码中:

if (in_array($elementName, array('input', 'textarea'))) {
    $existingValueLength = strlen($element->attribute('value'));
        // Add the TAB key to ensure we unfocus the field as browsers are triggering the change event only
        // after leaving the field.
    $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value . Key::TAB;
}

因此,如果您在字段中对键盘输入进行一些 JS 处理,setValue 可能会产生奇怪的结果。