【发布时间】:2019-08-14 03:13:03
【问题描述】:
想知道是否有人可以帮助我,我正在使用 Behat 来自动测试一个 drupal 站点..
我想输入格式为 - dd/mm/YYYY 的日期 - 我可以手动输入,但表格的变量适用于超过 30 天的日期。
Behat 中是否有一种方法(我找不到)我可以调用它来将今天的日期放在一个字段中,以及今天的日期 + 或 - X 天数?这个好像不是内置的,但是我
【问题讨论】:
想知道是否有人可以帮助我,我正在使用 Behat 来自动测试一个 drupal 站点..
我想输入格式为 - dd/mm/YYYY 的日期 - 我可以手动输入,但表格的变量适用于超过 30 天的日期。
Behat 中是否有一种方法(我找不到)我可以调用它来将今天的日期放在一个字段中,以及今天的日期 + 或 - X 天数?这个好像不是内置的,但是我
【问题讨论】:
我设法让这个工作......将它添加到您的 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-sheet-en.pdf
【讨论】: