Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Assuming that you have a set of div elements that contain different text inside and want to confirm the content.


FeatureContext.php


As you can see this is just a raw example so you better tidy it up a bit.


class FeatureContext extends MinkContext implements KernelAwareContext
{
/**
* @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));
}
}
}
}

Feature


Feature: Test
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 |

HTML


Text 1

Text 2

Text 3