Skip to content

Commit

Permalink
Merge pull request #817 from murilohpucci/feature/element-attribute-n…
Browse files Browse the repository at this point in the history
…ot-exists

Add element attribute not exists
  • Loading branch information
stof authored Dec 9, 2021
2 parents 8f86e44 + a27946b commit 34c0ae0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 34c0ae0

Please sign in to comment.