Skip to content

Instantly share code, notes, and snippets.

@edysmp
Created March 17, 2022 00:50
Show Gist options
  • Save edysmp/74bf7ea481fa4a24e5e4dbc72871ab55 to your computer and use it in GitHub Desktop.
Save edysmp/74bf7ea481fa4a24e5e4dbc72871ab55 to your computer and use it in GitHub Desktop.
I fill in the date field behat step definition
/**
* @Then I fill in the date field :field with :date
*
* Assign the specified date to the element.
*/
public function iFillDateField($field, $date): void {
$field_label = $this->getSession()->getPage()->find('xpath', "//*[text() = '$field']");
if ($field_label == NULL | !$field_label->hasClass('form-item__label')) {
throw new ExpectationException('Could not find date field with label: ' . $field, $this->getSession());
}
$datetime = new \DateTime($date, \timezone_open('UTC'));
$datetime->setTimezone(new \DateTimeZone(\date_default_timezone_get()));
$date_input = $field_label->getParent()->find('xpath', "//input[@type='date']");
$date_output_string = $datetime->format('Y-m-d');
$this->getSession()->getPage()->fillField($date_input->getAttribute('id'), $date_output_string);
$time_input = $field_label->getParent()->find('xpath', "//input[@type='time']");
if ($time_input) {
$time_output_string = $datetime->format('H:i:s');
$this->getSession()->getPage()->fillField($time_input->getAttribute('id'), $time_output_string);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment