You are here

public function VarbaseContext::ishouldSeeTextInTheHtmlTagElement in Varbase: The Ultimate Drupal CMS Starter Kit (Bootstrap Ready) 8.8

Same name and namespace in other branches
  1. 8.4 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::ishouldSeeTextInTheHtmlTagElement()
  2. 8.5 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::ishouldSeeTextInTheHtmlTagElement()
  3. 8.6 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::ishouldSeeTextInTheHtmlTagElement()
  4. 8.7 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::ishouldSeeTextInTheHtmlTagElement()
  5. 9.0.x tests/features/bootstrap/VarbaseContext.php \VarbaseContext::ishouldSeeTextInTheHtmlTagElement()

Check if we do have the text in the selected element.

Varbase Context #varbase.

Example #1: Then I should see "your text" in the "ol" element with the "class" attribute set to "breadcrumb" Example #2: And I should see "your text" in the "div" element with the "id" attribute set to "right-panel"

@Then /^I should see "(?P<text>[^"]*)" in the "(?P<htmlTagName>[^"]*)" element with the "(?P<attribute>[^"]*)" attribute set to "(?P<value>[^"]*)"$/

File

tests/features/bootstrap/VarbaseContext.php, line 919

Class

VarbaseContext
Defines application features from the specific context.

Code

public function ishouldSeeTextInTheHtmlTagElement($text, $htmlTagName, $attribute, $value) {
  $elements = $this
    ->getSession()
    ->getPage()
    ->findAll('css', $htmlTagName);
  if (empty($elements)) {
    throw new \Exception(sprintf('The element "%s" was not found in the page', $htmlTagName));
  }
  $found = FALSE;
  foreach ($elements as $element) {
    $actual = $element
      ->getText();
    $actual = preg_replace('/\\s+/u', ' ', $actual);
    $regex = '/' . preg_quote($text, '/') . '/ui';
    if (preg_match($regex, $actual)) {
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    throw new \Exception(sprintf('"%s" was not found in the "%s" element', $text, $htmlTagName));
  }
  if (!empty($attribute)) {
    $attr = $element
      ->getAttribute($attribute);
    if (empty($attr)) {
      throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s"', $attribute, $htmlTagName));
    }
    if (strpos($attr, "{$value}") === FALSE) {
      throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s"', $attribute, $value, $htmlTagName));
    }
  }
}