2014-10-09 67 views
2

我正在創建一個Behat/Selenium自動化測試腳本。我想創建一個語句:使用Behat/Selenium在特定的DIV中查找內容

Given I am on "/" 
Then I should see <CONTENT> in <SPECIFIC DIV> 

我要創造我FeatureContext.php文件的功能,讓我來驗證內容(文本)只存在內的指定DIV標籤。有人可以請指點我正確的方向嗎?

+0

聽起來像你需要使用正則表達式? – sjagr 2014-10-09 21:00:00

回答

2

如果你想指出正確的方向,那麼你應該避免這樣做 - 他們與Behat意識形態相矛盾。您的方案描述了用戶執行的操作以及他作爲查看頁面的用戶來驗證結果的聲明,而不是作爲查看代碼的開發人員。最後的邏輯必須由你在幕後完成。換句話說,你的腳步應該多讀這樣的:

Given I am on the homepage 
Then the page title should read "hello world" 
And the user list should contain "john doe" 
And the dialogue title should be "foo bar" 

如果你想要的答案最初的問題,那麼你可以做它是這樣:

/** 
* @Then /^(?:|I)should see "(?P<text>.+)" in the "(?P<selector>\w+)" element$/ 
*/ 
public function assertElementText($text, $selector) { 
    $page = $this->getSession()->getPage(); 
    $element = $page->findAll('css', $selector); 

    foreach($this->getSession()->getPage()->findAll() as $element) { 
     if (strpos(strtolower($text), strtolower($element->getText()) !== false) { 
      return; 
     } 
    } 

    throw Exception("Text '{$text}' is not found in the '{$selector}' element."); 
} 

而且使用它是這樣的:

Then I should see "hello world" in the "div#title" element 
Then I should see "john doe" in the "ul#users > li" element 
+0

謝謝伊恩。我知道我遲到了答覆。但是,您的解決方案再一次發揮作用,讓我到需要在工作中進行測試的地方。再次感謝。有些事告訴我,這不會是我們最後一次通信的時候,哈哈。 – user3385054 2014-10-21 16:20:58

+0

歡迎您:) – 2014-10-21 19:54:29

相關問題