Behat - 日期的步骤定义?今天和 -/+ 天?

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

想知道是否有人可以帮助我,我正在使用 Behat 来自动测试 drupal 站点..

我想输入一个日期格式 - dd/mm/YYYY - 我可以手动输入这个,但是表格的变量是一个超过 30 天的日期。

在 Behat 中有没有一种方法(我找不到)我可以调用它将今天的日期放在一个字段中,以及今天的日期 + 或 - X 天数?这似乎不是内置的,但我

如果您的字段具有 id 属性,您可以使用下面的步骤定义。

<input id="from_date" ....../>

And I fill in "from_date" with "02/03/2016"

如果您 运行 $ bin/behat -dl,您将看到所有内置步骤。

备忘单:http://blog.lepine.pro/images/2012-03-behat-cheat-sheet-en.pdf

我设法让它工作...将它添加到您的 FormContext 文件中 -

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

我想补充一下@Karl 的回复——他使用了特定的日期格式,我对其进行了扩展以添加可配置的日期格式。我保留了两个步骤定义(一个具有默认格式,一个具有可配置格式)

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