Search code examples
phpdrupalbehat

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


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


Solution

  • 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);
    }