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?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

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

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.