Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Behat\Mink; Chris@0: Chris@0: use Behat\Mink\Element\Element; Chris@0: use Behat\Mink\Element\ElementInterface; Chris@0: use Behat\Mink\Element\NodeElement; Chris@0: use Behat\Mink\Element\TraversableElement; Chris@0: use Behat\Mink\Exception\ElementNotFoundException; Chris@0: use Behat\Mink\Exception\ExpectationException; Chris@0: use Behat\Mink\Exception\ResponseTextException; Chris@0: use Behat\Mink\Exception\ElementHtmlException; Chris@0: use Behat\Mink\Exception\ElementTextException; Chris@0: Chris@0: /** Chris@0: * Mink web assertions tool. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class WebAssert Chris@0: { Chris@0: protected $session; Chris@0: Chris@0: /** Chris@0: * Initializes assertion engine. Chris@0: * Chris@0: * @param Session $session Chris@0: */ Chris@0: public function __construct(Session $session) Chris@0: { Chris@0: $this->session = $session; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current session address is equals to provided one. Chris@0: * Chris@0: * @param string $page Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function addressEquals($page) Chris@0: { Chris@0: $expected = $this->cleanUrl($page); Chris@0: $actual = $this->getCurrentUrlPath(); Chris@0: Chris@0: $this->assert($actual === $expected, sprintf('Current page is "%s", but "%s" expected.', $actual, $expected)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current session address is not equals to provided one. Chris@0: * Chris@0: * @param string $page Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function addressNotEquals($page) Chris@0: { Chris@0: $expected = $this->cleanUrl($page); Chris@0: $actual = $this->getCurrentUrlPath(); Chris@0: Chris@0: $this->assert($actual !== $expected, sprintf('Current page is "%s", but should not be.', $actual)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current session address matches regex. Chris@0: * Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function addressMatches($regex) Chris@0: { Chris@0: $actual = $this->getCurrentUrlPath(); Chris@0: $message = sprintf('Current page "%s" does not match the regex "%s".', $actual, $regex); Chris@0: Chris@0: $this->assert((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specified cookie exists and its value equals to a given one. Chris@0: * Chris@0: * @param string $name cookie name Chris@0: * @param string $value cookie value Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function cookieEquals($name, $value) Chris@0: { Chris@0: $this->cookieExists($name); Chris@0: Chris@0: $actualValue = $this->session->getCookie($name); Chris@0: $message = sprintf('Cookie "%s" value is "%s", but should be "%s".', $name, $actualValue, $value); Chris@0: Chris@0: $this->assert($actualValue == $value, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specified cookie exists. Chris@0: * Chris@0: * @param string $name cookie name Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function cookieExists($name) Chris@0: { Chris@0: $message = sprintf('Cookie "%s" is not set, but should be.', $name); Chris@0: $this->assert($this->session->getCookie($name) !== null, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response code equals to provided one. Chris@0: * Chris@0: * @param int $code Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function statusCodeEquals($code) Chris@0: { Chris@0: $actual = $this->session->getStatusCode(); Chris@0: $message = sprintf('Current response status code is %d, but %d expected.', $actual, $code); Chris@0: Chris@0: $this->assert(intval($code) === intval($actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response code not equals to provided one. Chris@0: * Chris@0: * @param int $code Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function statusCodeNotEquals($code) Chris@0: { Chris@0: $actual = $this->session->getStatusCode(); Chris@0: $message = sprintf('Current response status code is %d, but should not be.', $actual); Chris@0: Chris@0: $this->assert(intval($code) !== intval($actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header equals value. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $value Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderEquals($name, $value) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf('Current response header "%s" is "%s", but "%s" expected.', $name, $actual, $value); Chris@0: Chris@0: $this->assert($value === $actual, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header does not equal value. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $value Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderNotEquals($name, $value) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf('Current response header "%s" is "%s", but should not be.', $name, $actual, $value); Chris@0: Chris@0: $this->assert($value !== $actual, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header contains value. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $value Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderContains($name, $value) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name); Chris@0: Chris@18: $this->assert(false !== stripos($actual, (string) $value), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header does not contain value. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $value Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderNotContains($name, $value) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name); Chris@0: Chris@18: $this->assert(false === stripos($actual, (string) $value), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header matches regex. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderMatches($name, $regex) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf('The pattern "%s" was not found anywhere in the "%s" response header.', $regex, $name); Chris@0: Chris@0: $this->assert((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header does not match regex. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseHeaderNotMatches($name, $regex) Chris@0: { Chris@0: $actual = $this->session->getResponseHeader($name); Chris@0: $message = sprintf( Chris@0: 'The pattern "%s" was found in the text of the "%s" response header, but it should not.', Chris@0: $regex, Chris@0: $name Chris@0: ); Chris@0: Chris@0: $this->assert(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current page contains text. Chris@0: * Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ResponseTextException Chris@0: */ Chris@0: public function pageTextContains($text) Chris@0: { Chris@0: $actual = $this->session->getPage()->getText(); Chris@0: $actual = preg_replace('/\s+/u', ' ', $actual); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text); Chris@0: Chris@0: $this->assertResponseText((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current page does not contains text. Chris@0: * Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ResponseTextException Chris@0: */ Chris@0: public function pageTextNotContains($text) Chris@0: { Chris@0: $actual = $this->session->getPage()->getText(); Chris@0: $actual = preg_replace('/\s+/u', ' ', $actual); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: $message = sprintf('The text "%s" appears in the text of this page, but it should not.', $text); Chris@0: Chris@0: $this->assertResponseText(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current page text matches regex. Chris@0: * Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ResponseTextException Chris@0: */ Chris@0: public function pageTextMatches($regex) Chris@0: { Chris@0: $actual = $this->session->getPage()->getText(); Chris@0: $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $regex); Chris@0: Chris@0: $this->assertResponseText((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current page text does not matches regex. Chris@0: * Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ResponseTextException Chris@0: */ Chris@0: public function pageTextNotMatches($regex) Chris@0: { Chris@0: $actual = $this->session->getPage()->getText(); Chris@0: $message = sprintf('The pattern %s was found in the text of the current page, but it should not.', $regex); Chris@0: Chris@0: $this->assertResponseText(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that page HTML (response content) contains text. Chris@0: * Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseContains($text) Chris@0: { Chris@0: $actual = $this->session->getPage()->getContent(); Chris@0: $message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text); Chris@0: Chris@18: $this->assert(stripos($actual, (string) $text) !== false, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that page HTML (response content) does not contains text. Chris@0: * Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseNotContains($text) Chris@0: { Chris@0: $actual = $this->session->getPage()->getContent(); Chris@0: $message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text); Chris@0: Chris@18: $this->assert(stripos($actual, (string) $text) === false, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that page HTML (response content) matches regex. Chris@0: * Chris@0: * @param string $regex Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseMatches($regex) Chris@0: { Chris@0: $actual = $this->session->getPage()->getContent(); Chris@0: $message = sprintf('The pattern %s was not found anywhere in the HTML response of the page.', $regex); Chris@0: Chris@0: $this->assert((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that page HTML (response content) does not matches regex. Chris@0: * Chris@0: * @param $regex Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function responseNotMatches($regex) Chris@0: { Chris@0: $actual = $this->session->getPage()->getContent(); Chris@0: $message = sprintf('The pattern %s was found in the HTML response of the page, but it should not.', $regex); Chris@0: Chris@0: $this->assert(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that there is specified number of specific elements on the page. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param int $count expected count Chris@0: * @param ElementInterface $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function elementsCount($selectorType, $selector, $count, ElementInterface $container = null) Chris@0: { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $nodes = $container->findAll($selectorType, $selector); Chris@0: Chris@0: $message = sprintf( Chris@0: '%d %s found on the page, but should be %d.', Chris@0: count($nodes), Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector, count($nodes) !== 1), Chris@0: $count Chris@0: ); Chris@0: Chris@0: $this->assert(intval($count) === count($nodes), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element exists on the current page. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param ElementInterface $container document to check against Chris@0: * Chris@0: * @return NodeElement Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function elementExists($selectorType, $selector, ElementInterface $container = null) Chris@0: { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->find($selectorType, $selector); Chris@0: Chris@0: if (null === $node) { Chris@0: if (is_array($selector)) { Chris@0: $selector = implode(' ', $selector); Chris@0: } Chris@0: Chris@0: throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector); Chris@0: } Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element does not exists on the current page. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param ElementInterface $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function elementNotExists($selectorType, $selector, ElementInterface $container = null) Chris@0: { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->find($selectorType, $selector); Chris@0: Chris@0: $message = sprintf( Chris@0: 'An %s appears on this page, but it should not.', Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assert(null === $node, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element contains text. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param string $text expected text Chris@0: * Chris@0: * @throws ElementTextException Chris@0: */ Chris@0: public function elementTextContains($selectorType, $selector, $text) Chris@0: { Chris@0: $element = $this->elementExists($selectorType, $selector); Chris@0: $actual = $element->getText(); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The text "%s" was not found in the text of the %s.', Chris@0: $text, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElementText((bool) preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element does not contains text. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param string $text expected text Chris@0: * Chris@0: * @throws ElementTextException Chris@0: */ Chris@0: public function elementTextNotContains($selectorType, $selector, $text) Chris@0: { Chris@0: $element = $this->elementExists($selectorType, $selector); Chris@0: $actual = $element->getText(); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The text "%s" appears in the text of the %s, but it should not.', Chris@0: $text, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElementText(!preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element contains HTML. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param string $html expected text Chris@0: * Chris@0: * @throws ElementHtmlException Chris@0: */ Chris@0: public function elementContains($selectorType, $selector, $html) Chris@0: { Chris@0: $element = $this->elementExists($selectorType, $selector); Chris@0: $actual = $element->getHtml(); Chris@0: $regex = '/'.preg_quote($html, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The string "%s" was not found in the HTML of the %s.', Chris@0: $html, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElement((bool) preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific element does not contains HTML. Chris@0: * Chris@0: * @param string $selectorType element selector type (css, xpath) Chris@0: * @param string|array $selector element selector Chris@0: * @param string $html expected text Chris@0: * Chris@0: * @throws ElementHtmlException Chris@0: */ Chris@0: public function elementNotContains($selectorType, $selector, $html) Chris@0: { Chris@0: $element = $this->elementExists($selectorType, $selector); Chris@0: $actual = $element->getHtml(); Chris@0: $regex = '/'.preg_quote($html, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The string "%s" appears in the HTML of the %s, but it should not.', Chris@0: $html, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElement(!preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that an attribute exists in an element. Chris@0: * Chris@0: * @param string $selectorType Chris@0: * @param string|array $selector Chris@0: * @param string $attribute Chris@0: * Chris@0: * @return NodeElement Chris@0: * Chris@0: * @throws ElementHtmlException Chris@0: */ Chris@0: public function elementAttributeExists($selectorType, $selector, $attribute) Chris@0: { Chris@0: $element = $this->elementExists($selectorType, $selector); Chris@0: Chris@0: $message = sprintf( Chris@0: 'The attribute "%s" was not found in the %s.', Chris@0: $attribute, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElement($element->hasAttribute($attribute), $message, $element); Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that an attribute of a specific elements contains text. Chris@0: * Chris@0: * @param string $selectorType Chris@0: * @param string|array $selector Chris@0: * @param string $attribute Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ElementHtmlException Chris@0: */ Chris@0: public function elementAttributeContains($selectorType, $selector, $attribute, $text) Chris@0: { Chris@0: $element = $this->elementAttributeExists($selectorType, $selector, $attribute); Chris@0: $actual = $element->getAttribute($attribute); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The text "%s" was not found in the attribute "%s" of the %s.', Chris@0: $text, Chris@0: $attribute, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElement((bool) preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that an attribute of a specific elements does not contain text. Chris@0: * Chris@0: * @param string $selectorType Chris@0: * @param string|array $selector Chris@0: * @param string $attribute Chris@0: * @param string $text Chris@0: * Chris@0: * @throws ElementHtmlException Chris@0: */ Chris@0: public function elementAttributeNotContains($selectorType, $selector, $attribute, $text) Chris@0: { Chris@0: $element = $this->elementAttributeExists($selectorType, $selector, $attribute); Chris@0: $actual = $element->getAttribute($attribute); Chris@0: $regex = '/'.preg_quote($text, '/').'/ui'; Chris@0: Chris@0: $message = sprintf( Chris@0: 'The text "%s" was found in the attribute "%s" of the %s.', Chris@0: $text, Chris@0: $attribute, Chris@0: $this->getMatchingElementRepresentation($selectorType, $selector) Chris@0: ); Chris@0: Chris@0: $this->assertElement(!preg_match($regex, $actual), $message, $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific field exists on the current page. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @return NodeElement Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function fieldExists($field, TraversableElement $container = null) Chris@0: { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->findField($field); Chris@0: Chris@0: if (null === $node) { Chris@0: throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field); Chris@0: } Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific field does not exists on the current page. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function fieldNotExists($field, TraversableElement $container = null) Chris@0: { Chris@0: $container = $container ?: $this->session->getPage(); Chris@0: $node = $container->findField($field); Chris@0: Chris@0: $this->assert(null === $node, sprintf('A field "%s" appears on this page, but it should not.', $field)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific field have provided value. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param string $value field value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function fieldValueEquals($field, $value, TraversableElement $container = null) Chris@0: { Chris@0: $node = $this->fieldExists($field, $container); Chris@0: $actual = $node->getValue(); Chris@0: $regex = '/^'.preg_quote($value, '/').'$/ui'; Chris@0: Chris@0: $message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value); Chris@0: Chris@0: $this->assert((bool) preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific field have provided value. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param string $value field value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function fieldValueNotEquals($field, $value, TraversableElement $container = null) Chris@0: { Chris@0: $node = $this->fieldExists($field, $container); Chris@0: $actual = $node->getValue(); Chris@0: $regex = '/^'.preg_quote($value, '/').'$/ui'; Chris@0: Chris@0: $message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual); Chris@0: Chris@0: $this->assert(!preg_match($regex, $actual), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific checkbox is checked. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function checkboxChecked($field, TraversableElement $container = null) Chris@0: { Chris@0: $node = $this->fieldExists($field, $container); Chris@0: Chris@0: $this->assert($node->isChecked(), sprintf('Checkbox "%s" is not checked, but it should be.', $field)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that specific checkbox is unchecked. Chris@0: * Chris@0: * @param string $field field id|name|label|value Chris@0: * @param TraversableElement $container document to check against Chris@0: * Chris@0: * @throws ExpectationException Chris@0: */ Chris@0: public function checkboxNotChecked($field, TraversableElement $container = null) Chris@0: { Chris@0: $node = $this->fieldExists($field, $container); Chris@0: Chris@0: $this->assert(!$node->isChecked(), sprintf('Checkbox "%s" is checked, but it should not be.', $field)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets current url of the page. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function getCurrentUrlPath() Chris@0: { Chris@0: return $this->cleanUrl($this->session->getCurrentUrl()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Trims scriptname from the URL. Chris@0: * Chris@0: * @param string $url Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function cleanUrl($url) Chris@0: { Chris@0: $parts = parse_url($url); Chris@0: $fragment = empty($parts['fragment']) ? '' : '#'.$parts['fragment']; Chris@0: $path = empty($parts['path']) ? '/' : $parts['path']; Chris@0: Chris@0: return preg_replace('/^\/[^\.\/]+\.php\//', '/', $path).$fragment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts a condition. Chris@0: * Chris@0: * @param bool $condition Chris@0: * @param string $message Failure message Chris@0: * Chris@0: * @throws ExpectationException when the condition is not fulfilled Chris@0: */ Chris@0: private function assert($condition, $message) Chris@0: { 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: * Asserts a condition involving the response text. Chris@0: * Chris@0: * @param bool $condition Chris@0: * @param string $message Failure message Chris@0: * Chris@0: * @throws ResponseTextException when the condition is not fulfilled Chris@0: */ Chris@0: private function assertResponseText($condition, $message) Chris@0: { Chris@0: if ($condition) { Chris@0: return; Chris@0: } Chris@0: Chris@0: throw new ResponseTextException($message, $this->session->getDriver()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts a condition on an element. Chris@0: * Chris@0: * @param bool $condition Chris@0: * @param string $message Failure message Chris@0: * @param Element $element Chris@0: * Chris@0: * @throws ElementHtmlException when the condition is not fulfilled Chris@0: */ Chris@0: private function assertElement($condition, $message, Element $element) Chris@0: { Chris@0: if ($condition) { Chris@0: return; Chris@0: } Chris@0: Chris@0: throw new ElementHtmlException($message, $this->session->getDriver(), $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts a condition involving the text of an element. Chris@0: * Chris@0: * @param bool $condition Chris@0: * @param string $message Failure message Chris@0: * @param Element $element Chris@0: * Chris@0: * @throws ElementTextException when the condition is not fulfilled Chris@0: */ Chris@0: private function assertElementText($condition, $message, Element $element) Chris@0: { Chris@0: if ($condition) { Chris@0: return; Chris@0: } Chris@0: Chris@0: throw new ElementTextException($message, $this->session->getDriver(), $element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $selectorType Chris@0: * @param string|array $selector Chris@0: * @param bool $plural Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: private function getMatchingElementRepresentation($selectorType, $selector, $plural = false) Chris@0: { Chris@0: $pluralization = $plural ? 's' : ''; Chris@0: Chris@0: if (in_array($selectorType, array('named', 'named_exact', 'named_partial')) Chris@0: && is_array($selector) && 2 === count($selector) Chris@0: ) { Chris@0: return sprintf('%s%s matching locator "%s"', $selector[0], $pluralization, $selector[1]); Chris@0: } Chris@0: Chris@0: if (is_array($selector)) { Chris@0: $selector = implode(' ', $selector); Chris@0: } Chris@0: Chris@0: return sprintf('element%s matching %s "%s"', $pluralization, $selectorType, $selector); Chris@0: } Chris@0: }