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.

You can use example below to get the content of given element with ID.


FeatureContext.php


class FeatureContext extends MinkContext
{
/**
* Get content of element with the provided css id
*
* @Then /^The content of the element with id "([^"]*)" equals to "([^"]*)"$/
*
* @param $elementId ID attribute of an element
* @param $content Content
* @throws \InvalidArgumentException
*/
public function contentsAreEqual($elementId, $content)
{
$session = $this->getSession();
$page = $session->getPage();

$element = $page->findById($elementId);

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

$element_content = $element->getText();

if ($element_content == $content) {
$element->click();
}

throw new \InvalidArgumentException(sprintf('Could not evaluate CSS content: "%s"', $content));
}
}