drevops / behat-steps

🧪 A collection of Behat step definitions for Drupal
GNU General Public License v3.0
18 stars 13 forks source link

Add Metatag trait #249

Closed AlexSkrypnyk closed 2 months ago

AlexSkrypnyk commented 2 months ago
  /**
   * @Then the metatag :type :name is :value
   */
  public function assertMetatag(string $type, string $name, string $value): void {
    $element = $this->getSession()->getPage()->find('xpath', '/head/meta[@' . $type . '="' . $name . '"]/@content');
    if (!$element) {
      throw new \Exception(sprintf('The metatag %s "%s" does not exist', $type, $name));
    }

    $text = $element->getText();
    if ($value !== $text) {
      throw new \Exception(sprintf("The metatag %s '%s' text '%s' does not match the expected value '%s'", $type, $name, $text, $value));
    }
  }

  /**
   * @Then the metatag :type :name does not exist
   */
  public function assertMetatagNotExist(string $type, string $name): void {
    $element = $this->getSession()->getPage()->find('xpath', '/head/meta[@' . $type . '="' . $name . '"]/@content');
    if ($element) {
      throw new \Exception(sprintf('The metatag %s "%s" exists on the page', $type, $name));
    }
  }