【问题标题】:Behat - Step definition for date? Today and -/+ days?Behat - 日期的步骤定义?今天和 -/+ 天?
【发布时间】:2019-08-14 03:13:03
【问题描述】:

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

我想输入格式为 - dd/mm/YYYY 的日期 - 我可以手动输入,但表格的变量适用于超过 30 天的日期。

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

【问题讨论】:

    标签: php drupal behat


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我想补充一下@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);
        }
      

      【讨论】:

        【解决方案3】:

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

        &lt;input id="from_date" ....../&gt;

        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

        【讨论】:

        • 嘿,是的,我知道我可以手动输入日期,但我希望脚本能够在脚本运行时输入“今天的日期”,并且在运行时输入“今天的日期 +/- 天”脚本运行,如果我现在设置一个日期,并在一个月内运行它,该日期将过时一个月,我的测试将无法正确执行..
        • 在 FeatureContext 文件中创建一个新的步骤定义来为你做这件事。你在这里有很多例子:www.inanzzz.com
        • 啊好的,我看过了,不知道怎么做,我想我想用 "$today = date("m.d.y"); " php命令,但我无法弄清楚如何在步骤定义中使用它,并且该站点上似乎没有足够接近的示例供我修改以解决它..
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        相关资源
        最近更新 更多