Search code examples
phphtmlseleniumphantomjsbehat

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


I'm using Behat: 3.0.15 with Selenium 3.4 and PhantomJS as a browser.

I have custom step to populate value of input date field. Basically it's one line:

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

I've noticed in next scenario step that result value is 19990101 and my field isn't passed validation.


Solution

  • Because setValue doesn't just "set a value" in the field

    If you look at the implementation of the setValue function in Selenium2Driver, you'll find that the actual value being input is defined in the following code:

    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;
    }
    

    Therefore, if you have some JS handling of keyboard input in the field, setValue might produce strange results.