是否可以在重复的div中断言文本?

时间:2015-04-16 20:17:17

标签: behat mink

我正在使用behat / mink创建一些BDD测试。我想知道是否可以在页面中重复的div中获取文本。例如:

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

课程重复,但文字不同。我想断言第二个div中显示的文本。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

http://casperjs.readthedocs.org/en/latest/modules/tester.html

这是一个javascript测试API,允许您在dom中声明任何内容