0

I am using behat/mink to create some BDD tests. I would like to know if it's possible to get a text inside a div that is repeated in the page. For example:

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

The class is repeated but the text is different. I would like to assert the text that is displayed in the second div.

Thomas
  • 2,256
  • 6
  • 32
  • 47

3 Answers3

1

You can clean/modify iReadContentOfDiv() method as you wish.

Gherkin

  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));
            }
        }
    }
}

ABOUT page content:

<div class="message">Text 1</div>
<div class="message">Text 2</div>
<div class="message">Text 3</div>
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • Hello Bent! Thanks a lot for your help! I based in your suggestion to solve this issue. – Thomas Apr 22 '15 at 20:29
  • @Thomas - Glad you sorted it out. If solved then please accept the answer so that others can benefit from it. Thanks – BentCoder Apr 22 '15 at 21:15
1

Based on @BentCoder answer, I did a small changes to solve the problem:

  /**
   * @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));

  }
Thomas
  • 2,256
  • 6
  • 32
  • 47
  • This was a useful answer for me, but there's a small problem in your code. It will not detect text which is right at the beginning of the element's content. Changing `if (strpos($e->getText(), $text)) {` to `if (strpos($e->getText(), $text) !== false) {` will fix that! – JMac Feb 10 '22 at 12:17
0

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

This is a javascript testing API that allows you to assert anything in the dom

nyuen
  • 8,829
  • 2
  • 21
  • 28
Alaa Abuzaghleh
  • 1,023
  • 6
  • 11