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



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

我想以格式输入日期-DD/mm/yyyy-我可以手动输入此内容,但是表格的变量是一个超过30天的日期。

在Behat中有办法(我找不到一个)可以将今天的日期放在一个字段中,还有今天的日期 或-x天数?似乎不是内置的,但是i

我设法使此工作...将其添加到您的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);
  }

如果您的字段具有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-cheat-sheet-en.pdf

最新更新