1

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-d\TH: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?

2 Answers 2

1
When I fill in the date field "Publish on" with "+1 second"

I get it to work with these updates.

  /**
   * @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);
    }
  }

The key to get it work is to set the right formats for date (Y-m-d) and time (H:i:s).

This step will accept the field label instead of the field id.

1
  • This doesn't seem to work for me. The browser reports "Please enter a valid value. The field is incomplete or has an invalid date" specifically over the time element. I've tried H:i:s or H:i:s A or 'h:i:s A` to no avail. The element shows everything but the AM/PM designation so I know something is off in chrome/chromium.
    – mradcliffe
    Jun 30, 2023 at 15:14
-1

I haven't been able to find a way to do this, so for now, I rewrote my CI configuration so that I can use Standalone Chrome with Pantheon/CircleCI instead of the Behat Chrome Extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.