2015-04-16 34 views
0

我正在使用behat/mink創建一些BDD測試。我想知道是否有可能在頁面中重複使用div的文本。例如:是否可以在重複的div中聲明文本?

<div class="message">Text 1</div> 
<div class="message">Text 2</div> 
<div class="message">Text 3</div> 

該類重複但文本不同。我想斷言顯示在第二個div中的文本。

+0

這是可能的!完整答案如下。 – BentCoder

回答

1

斷言什麼,你可以清潔/修改iReadContentOfDiv()方法,如你所願。

小黃瓜

Scenario: Iterate classes 
    Given I am on "about" 
    Then I should see "Welcome to About page" 
    And The content of repeated ".message" div should be: 
     | content | 
     | Text 1 | 
     | Text 2 | 
     | Text 3 | 

FeatureContext.php

namespace MyProject\ApiBundle\Features\Context; 

use Behat\Gherkin\Node\TableNode; 
use Behat\MinkExtension\Context\MinkContext; 

class FeatureContext extends MinkContext 
{ 

    /** 
    * @When /^The content of repeated "([^"]*)" div should be:$/ 
    */ 
    public function iReadContentOfDiv($class, TableNode $table) 
    { 
     $session = $this->getSession(); 
     $page = $session->getPage(); 
     $element = $page->findAll('css', $class); 

     if (null === $element) { 
      throw new \InvalidArgumentException(sprintf('Could not evaluate CSS: "%s"', $class)); 
     } 

     $found = []; 
     foreach ($element as $e) { 
      $found[] = $e->getText(); 
     } 

     foreach ($table->getHash() as $element) { 
      if (!in_array($element['content'], $found)) { 
       throw new Exception(sprintf('Data "%s" not found in DOM element "%s".', $element['content'], $class)); 
      } 
     } 
    } 
} 

有關網頁內容:

<div class="message">Text 1</div> 
<div class="message">Text 2</div> 
<div class="message">Text 3</div> 
+0

Hello Bent!非常感謝你的幫助!我根據你的建議來解決這個問題。 – Thomas

+0

@Thomas - 很高興你把它整理出來。如果解決了,請接受答案,以便其他人可以從中受益。謝謝 – BentCoder

0

基地在@BentCoder答案,我做了一個小的變化d解決問題:

/** 
    * @Then /^The content of repeated "([^"]*)" div should contain "([^"]*)"$/ 
    */ 
    public function iReadContentOfDiv($class, $text) 
    { 
    $session = $this->getSession(); 
    $page = $session->getPage(); 
    $element = $page->findAll('css', $class); 

    if (null === $element) { 
     throw new \InvalidArgumentException(sprintf('Could not evaluate CSS: "%s"', $class)); 
    } 

    foreach ($element as $e) { 
     if (strpos($e->getText(), $text)){ 
     print 'opa'; 
     return; 
     } 
    } 

    throw new Exception(sprintf('Data "%s" not found in DOM element "%s".', $text, $class)); 

    } 
相關問題