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

时间:2016-06-22 13:31:13

标签: php drupal behat

不知道有人可以帮助我,我正在使用Behat自动测试一个drupal网站..

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

Behat(我找不到一个)我可以打电话给今天的日期在一个字段中,还有今天的日期+或 - X天?它似乎不是内置的,但我

3 个答案:

答案 0 :(得分:5)

我设法让这个工作......将它添加到你的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);
}

答案 1 :(得分:0)

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

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

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

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

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

答案 2 :(得分:0)

我想添加到@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);
  }