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
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);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With