diff --git a/src/WebAssert.php b/src/WebAssert.php index 3feb532eb..b03c9829e 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -565,6 +565,32 @@ public function elementAttributeExists($selectorType, $selector, $attribute) return $element; } + /** + * Checks that an attribute does not exist in an element. + * + * @param string $selectorType + * @param string|array $selector + * @param string $attribute + * + * @return NodeElement + * + * @throws ElementHtmlException + */ + public function elementAttributeNotExists($selectorType, $selector, $attribute) + { + $element = $this->elementExists($selectorType, $selector); + + $message = sprintf( + 'The attribute "%s" was found in the %s.', + $attribute, + $this->getMatchingElementRepresentation($selectorType, $selector) + ); + + $this->assertElement(!$element->hasAttribute($attribute), $message, $element); + + return $element; + } + /** * Checks that an attribute of a specific elements contains text. * diff --git a/tests/WebAssertTest.php b/tests/WebAssertTest.php index 57035c67f..529dc3317 100644 --- a/tests/WebAssertTest.php +++ b/tests/WebAssertTest.php @@ -1087,6 +1087,54 @@ public function testElementAttributeExists() ); } + public function testElementAttributeNotExists() + { + $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $this->session + ->expects($this->exactly(2)) + ->method('getPage') + ->will($this->returnValue($page)) + ; + + $page + ->expects($this->exactly(2)) + ->method('find') + ->with('css', 'h2 > span') + ->will($this->returnValue($element)) + ; + + $element + ->expects($this->at(0)) + ->method('hasAttribute') + ->with('name') + ->will($this->returnValue(false)) + ; + + $element + ->expects($this->at(1)) + ->method('hasAttribute') + ->with('name') + ->will($this->returnValue(true)) + ; + + $this->assertCorrectAssertion('elementAttributeNotExists', array('css', 'h2 > span', 'name')); + $this->assertWrongAssertion( + 'elementAttributeNotExists', + array('css', 'h2 > span', 'name'), + 'Behat\\Mink\\Exception\\ElementHtmlException', + 'The attribute "name" was found in the element matching css "h2 > span".' + ); + } + public function testElementAttributeNotContains() { $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')