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

SteveRaines's avatar

Dusk typing in same input twice instead of changing to the next input

I have some Dusk tests that I'm updating for a new feature I'm adding. The tests were working, but then some extra validation was added that the tests now need to account for. This should've been simple, just add two extra fields that are filled out on the form being tested. A pikaday datepicker input, and a regular text input after that, with IDs publication_date and publication_location. The problem is when I run the test in headless mode I see that the #publication_date field has both values as one string, "2022-10-30 00:00:00Wilmington, USA," as if the second input couldn't be focused on. Here is the test in question:

$this->browse(function (Browser $browser) use ($userId) {
            $browser->loginAs($userId)
                ->assertAuthenticated()
                ->visit(new PublicationsPage($this->user, $this->project, $this->secondaryProject, $this->rp))
                ->openPublicationForm('publications_type----presentation')
                ->addExistingAuthor('Bob Smith', '[email protected]')
                ->fillOutFormAndSave([
                    'title' => 'Test Conference Presentation',
                    'conference_name' => 'Test Conference',
                    'publication_date' => Carbon::yesterday()->format('Y-m-d H:i:s'),
                    'publication_location' => 'Wilmington, USA',
                    'distribution' => 'publications_distribution----local',
                    'status' => 'publications_status----awaiting_publication',
                    'support_acknowledgement' => 1,
                    'support_level' => 'publications_support_level----primarily_supported',
                    'primary_initiative' => 'primary_initiative----socio_ecological_observatory',
                ])
                ->waitForText(self::SUCCESS_MESSAGE);
        });

The change I made was to add the publication_date and publication_location fields to the fillOutFormAndSave method, which is defined on the Publications Dusk page here:

public function fillOutFormAndSave(Browser $browser, array $inputs)
    {
        $browser->assertVisible('@subformContainer')
            ->with('@subformContainer', function(Browser $subform) use($inputs) {
                foreach ($inputs as $name => $value) {
                    if (in_array($name, self::SELECT2_INPUTS)) {
                        $subform->script("$('#subform-container #$name').select2('val', '$value');");
                        continue;
                    }

                    $subform->type("#$name", $value);
                }

                $subform->check('@defaultProjectCheckbox')
                    ->check('@secondaryProjectCheckbox')
                    ->click('@saveButton');
            });
    }

When you focus on a pikaday input it makes a little calendar pop up so you can simply click a date if you don't want to manually type one in, so I thought maybe I just needed to hide that calendar. I added the following code to fillOutFormAndSave:

$subform->type("#$name", $value);

if (in_array($name, self::PIKADAY_INPUTS)) {
        $subform->script("$('#subform-container #$name').pikaday('hide');");
}

But this didn't work. The hide method works, I've tested it in browser myself. But apparently there's something else that needs to be done. The strange thing is that when I put a breakpoint on the bit that calls the pikaday hide method, when I click the play icon in the debugger, suddenly Dusk is able to focus on the next input and the test complete successfully.

0 likes
1 reply
SteveRaines's avatar
SteveRaines
OP
Best Answer
Level 1

Instead of trying to type in the pikaday input, I just did what I probably should've done in the first place (and WOULD have done in the first place if pikaday's documentation was better), which was manually set the date via script.

if (in_array($name, self::PIKADAY_INPUTS)) {
        $subform->script("$('#subform-container #$name').pikaday('setDate', '$value');");
        continue;
}

$subform->type("#$name", $value);

Also worth noting in case anyone else has this same issue, I had the date format wrong and it seems like that can cause pikaday to set the wrong date sometimes. Here's the fixed date:

'publication_date' => Carbon::yesterday()->format('m/d/Y'),

Please or to participate in this conversation.