【问题标题】:Behat featurecontext reusable action regex issueBehat featurecontext 可重用动作正则表达式问题
【发布时间】:2016-07-22 12:52:42
【问题描述】:

我正在尝试在我的功能上下文中编写一个函数,并在操作中添加一个可选部分。

/**
* Function to scroll into view.
*
* @When /^I scroll "([^"]*)" into view( of type "([^"]*)")?$/g
*/
public function iScrollIntoView($locator, $type = "id") {
    // Some logic here.
}

所以我的想法是我可以在我的行为脚本中使用:I scroll "foo" into viewI scroll "foo" into view of type "bar"

所以我希望类型部分是可选的,虽然正则表达式在在线正则表达式测试器中似乎可以正常工作,但在我的 behat 脚本中似乎不起作用。

当我将 And I scroll "foo" into view 放入我的 behat 脚本时,它无法识别该函数。我的正则表达式有问题吗?或者这是一个行为问题。

【问题讨论】:

    标签: regex behat


    【解决方案1】:

    经过一番深思熟虑,解决方案非常合乎逻辑。导致 behat 失败的主要问题是正则表达式中的 global => g,因此在删除它之后 behat 正在识别操作。

    /**
    * Function to scroll into view.
    *
    * @When /^I scroll "([^"]*)" into view( of type "([^"]*)")?/
    */
    

    第二个问题是分组,在我的函数中,我使用第二组作为参数,但是这是可选组(类型为“foo”),实际上并不是该组的变量。因此,在向函数添加第三个参数后,我能够检索它。

    public function iScrollIntoView($locator, $of_type_provided = FALSE, $type = "id") {
        // Some logic here.
    }
    

    我希望这对将来的其他人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多