2013-09-26 22 views
0

我一直在通過Behat | Mink在BDD工作。Behat/Mink是否可以通過不完整的字符串進行查找?

但是我沒有找到一個ID或名稱的輸入(字段),如果他們不完整。

我打算做這樣的事情:

$field = $this->getSession()->getPage()->findField("*first_name*"); 

其中「*」意味着可以是任何一條繩子那裏。

的問題是,我有,我不能確切地知道完整的ID原因的情況下:

id="<random-string>_<actual-field-name>_<random-number>" 

任何想法,我該怎麼辦呢?

有什麼我試圖

- get it through label (for some reasons there are some labels that dont have 'for' attribute and sometimes the name is a sentence and not an simple name) 
- get it through css/classes (to many variables) 

編輯:

我創建一個PHP函數讀取屬性,並返回預期的屬性值。

/** 
* 
* @param string $haystack The complete html 
* @param string $needle The string with the part of the attribute value 
* @param string $tag  The tag of where the attribute should belong 
* @param string $attr  The attribute to ifnd 
* @return string 
*/ 
protected function findCompleteAttribute($haystack, $needle, $tag = null, $attr = 'id') 

然而,在案件還有另外一個領域(例如:隱藏的登錄表單)和針也是這些輸入,它會得到他們,如果他們是第一次。

所以我需要指定的形式,但是,一旦更多的,有我沒有辦法找到通過名稱的形式:

$field = $this->getSession()->getPage()->find('named', array('form', $formName)); 

回答

-1

後學習更多的Xpath和根據我對@gontrollez的評論答案,它可以通過單個Xpath解決:

//form[ @name = 'Register' ]//input[ contains(@id, 'username')] 

這會ret甕有「用戶名」在他們的id屬性所有輸入和輸入是任何形式,它的name屬性等於「註冊」

<form> 
    <input id="login_username" type="text"/> 
</form> 
<form name="Register> 
    <div> 
     <input id="other_username" type="text"/> 
    </div> 
</form> 
</body> 

這將返回只有第二輸入

+0

特別感謝@ gontrollez – mloureiro

2

嘗試使用XPath:

$el = $this->getSession()->getPage()->find('xpath', "//input[contains(@id,'first_name')]"); 
+0

內這確實所有的我通過手工完成的功能(我希望我之前知道)。 但是,我仍然遇到有兩個或更多包含'first_name'的字段的問題,因此我決定向查找中添加更多詳細信息,現在我正在使用這種方式 – mloureiro

+0

您可以優化XPath表達式,其中包括超過1屬性條件:http://stackoverflow.com/questions/6029232/xpath-select-multiple-attr – gontrollez

+0

謝謝@gontrollez,但我不認爲多屬性搜索會做,導致問題是,我需要一種方式找到一個'表格',以便我可以搜索內部的輸入。 類似: '$形式= $這個 - >的getSession() - > GETPAGE() - >查找( '的XPath', 「//形式[conains(@名, '註冊']」); $ input = $ form-> find('xpath',// input [contains(@ id,'first_name')]「);' 但直到現在還沒有成功搜索表單 – mloureiro

相關問題