Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Surlica's avatar

Dusk type decimal with parseFloat in JS gives NaN

Using Dusk to test, I have the following javascript function attached to onchange/onblur events on an input field, to better format it into decimal:

reformat__quantity: function(o)
{
    var v = parseFloat(o.val());
    if (isNaN(v))
    {
    // it's always NaN, why?
        v = 1.00;
    }
    else if (v <= 0)
    {
        v = 1.00;
    }
    v = v.toFixed(2);
    o.val(v);
}

Trying to type a decimal value into the field via Dusk

->type('quantity', '3.71')
->type('quantity', 3.71) // same thing happen

parseFloat in js always gives NaN (and reformats to 1.00 here), but why? Using regular browser manually works as expected, the value shows up as 3.71 Removing this function from event helps with the test, but it is needed in real usage.

Please help.

0 likes
4 replies
staudenmeir's avatar

Please put console.log(o.val()); in your JavaScript code and run the test.

The console should show the output (look for INFO:CONSOLE).

Surlica's avatar

Not sure how to log anything while running test if test does not result in error. How can I set this up? I managed to "print to page" using jQuery's append, and take screenshot of it, with the following:

$('#info').append(o.val()); // on first line of reformat__quantity function

While running test, o.val() seems to be empty string at the moment I "log" it, which is incorrect, and explains why it is NaN. (doing the same in real browser have the correct value of 3.71)

Then, the screenshot shows that the value of 3.71 was appended to the existing value of the input field which is 1.00. So the result looks like 1.003.71 at the time of screenshot. I notice that if I remove value attribute from the input field

<input type="text" name="quantity" spellcheck="false" value="1.00"> // change this
<input type="text" name="quantity" spellcheck="false"> // into this

then the field gets correctly populated with 3.71

I have tried to do any of the following before doing ->type('quantity', 3.71) but it did not help:

->clear('quantity')
->type('quantity', '')

There seems to be some kind of delay or non-synchronization in the way headless browser types in data and executes javascript versus actual browser.

Do you have any idea what could be the cause, and how to run test while retaining existing functionality of the javascript as it is set for real usage?

Surlica's avatar

Thank you. Using ->value before ->type seems to resolve this issue.

->value('input[name="quantity"]', '')
->type('quantity', '3.71')

Please or to participate in this conversation.