testing – How do I set the value of a date and time field with Behat?


I am trying to test my site with Behat.

On my entity edit form, I have a Datetime field that uses the Date and time widget. I want to use the Date and time widget because I need to set the time in increments of seconds, which I cannot do with the Select list widget.

I want to set the value of this field with a step like this:

And I assign the date "yesterday" with timezone "Asia/Tokyo" for "edit-field-datetime-MYFIELD-0-value"

To do so, in FeatureContext.php, I wrote the following code.

  /**
   * @Then I assign the date :date with timezone :timezone for :field
   *
   * Assign the specified date to the element with the given CSS.
   */
  public function assertEnterDateForField($field, $timezone, $date_string) {
    $date_field = $field . '-date';
    $time_field = $field . '-time';
    // Drupal stores timezones as UTC so we need the base time in UTC.
    $datetime = new DateTime($date_string, timezone_open('UTC'));
    // We need to convert the UTC time to the user's timezone.
    // This is because when saving the entity,
    // Drupal will convert the value to UTC.
    $datetime->setTimezone(timezone_open($timezone));
    $date_output_string = $datetime->format('mdY');
    $time_output_string = $datetime->format('hisA');
    $datetime_debug_string = $datetime->format('Y-m-dTH:i:sO');
    $this->fillfield($time_field, $time_output_string);
    $this->fillField($date_field, $date_output_string);
    echo "Datetime $datetime_debug_string
    Field set to
    date: $date_output_string
    time: $time_output_string";
  }

On my local environment (lando, running the Standalone Chrome container), this code works as expected; the date/time is set correctly for the field.

However, on my test server (Pantheon/CircleCI, running the Behat Chrome Extension, it doesn’t work. The debug information is correct; for example:

  │ Datetime 2020-08-13T09:00:00+0900
  │     Field set to
  │     date: 08132020
  │     time: 090000AM

However, the date/time is not actually filled in, so when the entity is saved, the date/time has not been updated.

How can I set the value of a field that uses the Date and time widget with Behat/Mink/Drupal Extension?