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.