2016-07-22 25 views
0

我想在我的功能上下文中使用動作中的可選部分編寫函數。Behat featurecontext可重複使用的動作正則表達式問題

/** 
* 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"

所以我想要的類型部分是可選的,而正則表達式似乎在網上正則表達式測試儀做工精細,它似乎並不適用於我的腳本。

當我把And I scroll "foo" into view放在我的behat腳本中時,它不能識別這個函數。我的正則表達式有什麼問題嗎?或者這是一個問題。

回答

0

經過一番艱難的思考後,解決方案非常邏輯。導致behat失敗的主要問題是正則表達式中的global => g,因此在刪除它之後,它正在識別該操作。

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

第二個問題是分組,在我的功能我用的是第二組爲我的參數,但是這是該組的可選(類型爲「富」的),並且本來就不是該組的變量。所以在給函數添加第三個參數之後,我能夠檢索它。

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

我希望這對未來的其他人有用。