Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behat - Step definition for date? Today and -/+ days?

Tags:

php

drupal

behat

Wonder if someone can help me, I am using Behat to automate testing of a drupal site..

I am wanting to input a date in the format - dd/mm/YYYY - I can manually enter this, but a variable of a form is for a date over 30 days.

Is there a way in Behat (I can't find one) that I can call that will put todays date in a field, and also todays date + or - X number of days? It doesn't seem like this is built in, but I

like image 872
Karl Avatar asked Jan 05 '23 20:01

Karl


2 Answers

I managed to get this working...add this to your FormContext file -

/**
 * Fills in specified field with date
 * Example: When I fill in "field_ID" with date "now"
 * Example: When I fill in "field_ID" with date "-7 days"
 * Example: When I fill in "field_ID" with date "+7 days"
 * Example: When I fill in "field_ID" with date "-/+0 weeks"
 * Example: When I fill in "field_ID" with date "-/+0 years"
 *
 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with date "(?P<value>(?:[^"]|\\")*)"$/
 */
public function fillDateField($field, $value)
{
    $newDate = strtotime("$value");

    $dateToSet = date("d/m/Y", $newDate);
    $this->getSession()->getPage()->fillField($field, $dateToSet);
}
like image 107
Karl Avatar answered Jan 17 '23 20:01

Karl


I'd like to add to @Karl's response-- his uses a particular date format, I extended it to add a configurable date format. I kept both step definitions (one with a default format, one with configurable format)

 /**
   * Fills in specified field with date
   * Example: When I fill in "field_ID" with date "now" in the format "m/d/Y"
   * Example: When I fill in "field_ID" with date "-7 days" in the format "m/d/Y"
   * Example: When I fill in "field_ID" with date "+7 days" in the format "m/d/Y"
   * Example: When I fill in "field_ID" with date "-/+0 weeks" in the format "m/d/Y"
   * Example: When I fill in "field_ID" with date "-/+0 years" in the format "m/d/Y"
   *
   * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with date "(?P<value>(?:[^"]|\\")*)" in the format "(?P<format>(?:[^"]|\\")*)"$/
   */
  public function fillDateFieldFormat($field, $value, $format)
  {
    $newDate = strtotime("$value");

    $dateToSet = date($format, $newDate);
    $this->getSession()->getPage()->fillField($field, $dateToSet);
  }
like image 33
George Agathos Avatar answered Jan 17 '23 19:01

George Agathos