Chris@0: baseUrl = $base_url; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function cleanUrl($url) { Chris@0: if ($url instanceof Url) { Chris@0: $url = $url->setAbsolute()->toString(); Chris@0: } Chris@0: // Strip the base URL from the beginning for absolute URLs. Chris@0: if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) { Chris@0: $url = substr($url, strlen($this->baseUrl)); Chris@0: } Chris@0: // Make sure there is a forward slash at the beginning of relative URLs for Chris@0: // consistency. Chris@0: if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) { Chris@0: $url = "/$url"; Chris@0: } Chris@0: return parent::cleanUrl($url); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific button exists on the current page. Chris@0: * Chris@0: * @param string $button Chris@0: * One of id|name|label|value for the button. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement Chris@0: * The matching element. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * When the element doesn't exist. Chris@0: */ Chris@0: public function buttonExists($button, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->findButton($button); Chris@0: Chris@0: if ($node === NULL) { Chris@0: throw new ElementNotFoundException($this->session, 'button', 'id|name|label|value', $button); Chris@0: } Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that the specific button does NOT exist on the current page. Chris@0: * Chris@0: * @param string $button Chris@0: * One of id|name|label|value for the button. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * When the button exists. Chris@0: */ Chris@0: public function buttonNotExists($button, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->findButton($button); Chris@0: Chris@0: $this->assert(NULL === $node, sprintf('A button "%s" appears on this page, but it should not.', $button)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific select field exists on the current page. Chris@0: * Chris@0: * @param string $select Chris@0: * One of id|name|label|value for the select field. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement Chris@0: * The matching element Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * When the element doesn't exist. Chris@0: */ Chris@0: public function selectExists($select, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->find('named', [ Chris@0: 'select', Chris@17: $select, Chris@0: ]); Chris@0: Chris@0: if ($node === NULL) { Chris@0: throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); Chris@0: } Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific option in a select field exists on the current page. Chris@0: * Chris@0: * @param string $select Chris@0: * One of id|name|label|value for the select field. Chris@0: * @param string $option Chris@0: * The option value. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement Chris@0: * The matching option element Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * When the element doesn't exist. Chris@0: */ Chris@0: public function optionExists($select, $option, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $select_field = $container->find('named', [ Chris@0: 'select', Chris@17: $select, Chris@0: ]); Chris@0: Chris@0: if ($select_field === NULL) { Chris@0: throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); Chris@0: } Chris@0: Chris@0: $option_field = $select_field->find('named', ['option', $option]); Chris@0: Chris@0: if ($option_field === NULL) { Chris@17: throw new ElementNotFoundException($this->session->getDriver(), 'select', 'id|name|label|value', $option); Chris@0: } Chris@0: Chris@0: return $option_field; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that an option in a select field does NOT exist on the current page. Chris@0: * Chris@0: * @param string $select Chris@0: * One of id|name|label|value for the select field. Chris@0: * @param string $option Chris@17: * The option value that should not exist. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * When the select element doesn't exist. Chris@0: */ Chris@0: public function optionNotExists($select, $option, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $select_field = $container->find('named', [ Chris@0: 'select', Chris@17: $select, Chris@0: ]); Chris@0: Chris@0: if ($select_field === NULL) { Chris@0: throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); Chris@0: } Chris@0: Chris@0: $option_field = $select_field->find('named', ['option', $option]); Chris@0: Chris@0: $this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pass if the page title is the given string. Chris@0: * Chris@0: * @param string $expected_title Chris@0: * The string the page title should be. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the title is a different one. Chris@0: */ Chris@0: public function titleEquals($expected_title) { Chris@0: $title_element = $this->session->getPage()->find('css', 'title'); Chris@0: if (!$title_element) { Chris@16: throw new ExpectationException('No title element found on the page', $this->session->getDriver()); Chris@0: } Chris@0: $actual_title = $title_element->getText(); Chris@0: $this->assert($expected_title === $actual_title, 'Title found'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the specified label is found. Chris@0: * Chris@0: * An optional link index may be passed. Chris@0: * Chris@0: * @param string $label Chris@0: * Text between the anchor tags. Chris@0: * @param int $index Chris@0: * Link position counting from zero. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages: use strtr() to embed variables in the message text, not Chris@0: * t(). If left blank, a default message will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkExists($label, $index = 0, $message = '') { Chris@0: $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label])); Chris@0: $links = $this->session->getPage()->findAll('named', ['link', $label]); Chris@0: $this->assert(!empty($links[$index]), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the exactly specified label is found. Chris@0: * Chris@0: * An optional link index may be passed. Chris@0: * Chris@0: * @param string $label Chris@0: * Text between the anchor tags. Chris@0: * @param int $index Chris@0: * Link position counting from zero. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages: use strtr() to embed variables in the message text, not Chris@0: * t(). If left blank, a default message will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkExistsExact($label, $index = 0, $message = '') { Chris@0: $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label])); Chris@0: $links = $this->session->getPage()->findAll('named_exact', ['link', $label]); Chris@0: $this->assert(!empty($links[$index]), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the specified label is not found. Chris@0: * Chris@0: * An optional link index may be passed. Chris@0: * Chris@0: * @param string $label Chris@0: * Text between the anchor tags. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages: use strtr() to embed variables in the message text, not Chris@0: * t(). If left blank, a default message will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkNotExists($label, $message = '') { Chris@0: $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label])); Chris@0: $links = $this->session->getPage()->findAll('named', ['link', $label]); Chris@0: $this->assert(empty($links), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the exactly specified label is not found. Chris@0: * Chris@0: * An optional link index may be passed. Chris@0: * Chris@0: * @param string $label Chris@0: * Text between the anchor tags. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages: use strtr() to embed variables in the message text, not Chris@0: * t(). If left blank, a default message will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkNotExistsExact($label, $message = '') { Chris@0: $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label])); Chris@0: $links = $this->session->getPage()->findAll('named_exact', ['link', $label]); Chris@0: $this->assert(empty($links), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link containing a given href (part) is found. Chris@0: * Chris@0: * @param string $href Chris@0: * The full or partial value of the 'href' attribute of the anchor tag. Chris@0: * @param int $index Chris@0: * Link position counting from zero. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@0: * variables in the message text, not t(). If left blank, a default message Chris@0: * will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkByHrefExists($href, $index = 0, $message = '') { Chris@0: $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]); Chris@0: $message = ($message ? $message : strtr('Link containing href %href found.', ['%href' => $href])); Chris@0: $links = $this->session->getPage()->findAll('xpath', $xpath); Chris@0: $this->assert(!empty($links[$index]), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link containing a given href (part) is not found. Chris@0: * Chris@0: * @param string $href Chris@0: * The full or partial value of the 'href' attribute of the anchor tag. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@0: * variables in the message text, not t(). If left blank, a default message Chris@0: * will be displayed. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Thrown when element doesn't exist, or the link label is a different one. Chris@0: */ Chris@0: public function linkByHrefNotExists($href, $message = '') { Chris@0: $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]); Chris@0: $message = ($message ? $message : strtr('No link containing href %href found.', ['%href' => $href])); Chris@0: $links = $this->session->getPage()->findAll('xpath', $xpath); Chris@0: $this->assert(empty($links), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds an XPath query. Chris@0: * Chris@0: * Builds an XPath query by replacing placeholders in the query by the value Chris@0: * of the arguments. Chris@0: * Chris@0: * XPath 1.0 (the version supported by libxml2, the underlying XML library Chris@0: * used by PHP) doesn't support any form of quotation. This function Chris@0: * simplifies the building of XPath expression. Chris@0: * Chris@0: * @param string $xpath Chris@0: * An XPath query, possibly with placeholders in the form ':name'. Chris@0: * @param array $args Chris@0: * An array of arguments with keys in the form ':name' matching the Chris@0: * placeholders in the query. The values may be either strings or numeric Chris@0: * values. Chris@0: * Chris@0: * @return string Chris@0: * An XPath query with arguments replaced. Chris@0: */ Chris@0: public function buildXPathQuery($xpath, array $args = []) { Chris@0: // Replace placeholders. Chris@0: foreach ($args as $placeholder => $value) { Chris@0: if (is_object($value)) { Chris@0: throw new \InvalidArgumentException('Just pass in scalar values for $args and remove all t() calls from your test.'); Chris@0: } Chris@0: // XPath 1.0 doesn't support a way to escape single or double quotes in a Chris@0: // string literal. We split double quotes out of the string, and encode Chris@0: // them separately. Chris@0: if (is_string($value)) { Chris@0: // Explode the text at the quote characters. Chris@0: $parts = explode('"', $value); Chris@0: Chris@0: // Quote the parts. Chris@0: foreach ($parts as &$part) { Chris@0: $part = '"' . $part . '"'; Chris@0: } Chris@0: Chris@0: // Return the string. Chris@0: $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0]; Chris@0: } Chris@0: Chris@0: // Use preg_replace_callback() instead of preg_replace() to prevent the Chris@0: // regular expression engine from trying to substitute backreferences. Chris@0: $replacement = function ($matches) use ($value) { Chris@0: return $value; Chris@0: }; Chris@0: $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath); Chris@0: } Chris@0: return $xpath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text IS NOT found escaped on the loaded page. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: */ Chris@0: public function assertNoEscaped($raw) { Chris@0: $this->responseNotContains(Html::escape($raw)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text IS found escaped on the loaded page. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: */ Chris@0: public function assertEscaped($raw) { Chris@0: $this->responseContains(Html::escape($raw)); Chris@0: } Chris@0: Chris@0: /** Chris@18: * Checks that page HTML (response content) contains text. Chris@18: * Chris@18: * @param string|object $text Chris@18: * Text value. Any non-string value will be cast to string. Chris@18: * Chris@18: * @throws ExpectationException Chris@18: */ Chris@18: public function responseContains($text) { Chris@18: parent::responseContains((string) $text); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Checks that page HTML (response content) does not contains text. Chris@18: * Chris@18: * @param string|object $text Chris@18: * Text value. Any non-string value will be cast to string. Chris@18: * Chris@18: * @throws ExpectationException Chris@18: */ Chris@18: public function responseNotContains($text) { Chris@18: parent::responseNotContains((string) $text); Chris@18: } Chris@18: Chris@18: /** Chris@0: * Asserts a condition. Chris@0: * Chris@0: * The parent method is overridden because it is a private method. Chris@0: * Chris@0: * @param bool $condition Chris@0: * The condition. Chris@0: * @param string $message Chris@0: * The success message. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * When the condition is not fulfilled. Chris@0: */ Chris@0: public function assert($condition, $message) { Chris@0: if ($condition) { Chris@0: return; Chris@0: } Chris@0: Chris@0: throw new ExpectationException($message, $this->session->getDriver()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that a given form field element is disabled. Chris@0: * Chris@0: * @param string $field Chris@0: * One of id|name|label|value for the field. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement Chris@0: * The matching element. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: */ Chris@12: public function fieldDisabled($field, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->findField($field); Chris@0: Chris@0: if ($node === NULL) { Chris@0: throw new ElementNotFoundException($this->session->getDriver(), 'field', 'id|name|label|value', $field); Chris@0: } Chris@0: Chris@0: if (!$node->hasAttribute('disabled')) { Chris@0: throw new ExpectationException("Field $field is disabled", $this->session->getDriver()); Chris@0: } Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific hidden field exists. Chris@0: * Chris@0: * @param string $field Chris@0: * One of id|name|value for the hidden field. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement Chris@0: * The matching element. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: */ Chris@0: public function hiddenFieldExists($field, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: if ($node = $container->find('hidden_field_selector', ['hidden_field', $field])) { Chris@0: return $node; Chris@0: } Chris@0: throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field); Chris@0: } Chris@0: Chris@0: /** Chris@17: * Checks that specific hidden field does not exist. Chris@0: * Chris@0: * @param string $field Chris@0: * One of id|name|value for the hidden field. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: */ Chris@0: public function hiddenFieldNotExists($field, TraversableElement $container = NULL) { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->find('hidden_field_selector', ['hidden_field', $field]); Chris@0: $this->assert($node === NULL, "A hidden field '$field' exists on this page, but it should not."); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific hidden field have provided value. Chris@0: * Chris@0: * @param string $field Chris@0: * One of id|name|value for the hidden field. Chris@0: * @param string $value Chris@0: * The hidden field value that needs to be checked. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: */ Chris@0: public function hiddenFieldValueEquals($field, $value, TraversableElement $container = NULL) { Chris@0: $node = $this->hiddenFieldExists($field, $container); Chris@0: $actual = $node->getValue(); Chris@0: $regex = '/^' . preg_quote($value, '/') . '$/ui'; Chris@0: $message = "The hidden field '$field' value is '$actual', but '$value' expected."; Chris@0: $this->assert((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific hidden field doesn't have the provided value. Chris@0: * Chris@0: * @param string $field Chris@0: * One of id|name|value for the hidden field. Chris@0: * @param string $value Chris@0: * The hidden field value that needs to be checked. Chris@0: * @param \Behat\Mink\Element\TraversableElement $container Chris@0: * (optional) The document to check against. Defaults to the current page. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: */ Chris@0: public function hiddenFieldValueNotEquals($field, $value, TraversableElement $container = NULL) { Chris@0: $node = $this->hiddenFieldExists($field, $container); Chris@0: $actual = $node->getValue(); Chris@0: $regex = '/^' . preg_quote($value, '/') . '$/ui'; Chris@0: $message = "The hidden field '$field' value is '$actual', but it should not be."; Chris@0: $this->assert(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@17: /** Chris@17: * Checks that current page contains text only once. Chris@17: * Chris@17: * @param string $text Chris@17: * The string to look for. Chris@17: * Chris@17: * @see \Behat\Mink\WebAssert::pageTextContains() Chris@17: */ Chris@17: public function pageTextContainsOnce($text) { Chris@17: $actual = $this->session->getPage()->getText(); Chris@17: $actual = preg_replace('/\s+/u', ' ', $actual); Chris@17: $regex = '/' . preg_quote($text, '/') . '/ui'; Chris@17: $count = preg_match_all($regex, $actual); Chris@17: if ($count === 1) { Chris@17: return; Chris@17: } Chris@17: Chris@17: if ($count > 1) { Chris@17: $message = sprintf('The text "%s" appears in the text of this page more than once, but it should not.', $text); Chris@17: } Chris@17: else { Chris@17: $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text); Chris@17: } Chris@17: Chris@17: throw new ResponseTextException($message, $this->session->getDriver()); Chris@17: } Chris@17: Chris@0: }