From 567c6e38ce0047a67c622d95d34ab1d81753746d Mon Sep 17 00:00:00 2001 From: Cameron Zemek Date: Thu, 8 Jan 2015 17:13:13 +1000 Subject: [PATCH] Added exist methods for button and select elements --- src/WebAssert.php | 76 ++++++++++++++++++++++ tests/WebAssertTest.php | 135 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) diff --git a/src/WebAssert.php b/src/WebAssert.php index 213b416a3..31dd24b95 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -697,6 +697,82 @@ public function fieldValueNotEquals($field, $value, TraversableElement $containe $this->assert(!preg_match($regex, $actual), $message); } + /** + * Checks that specific button exists on the current page. + * + * @param string $button button id|name|title|value + * @param TraversableElement $container document to check against + * + * @return NodeElement + * + * @throws ElementNotFoundException + */ + public function buttonExists($button, TraversableElement $container = null) + { + $container = $container ?: $this->session->getPage(); + $node = $container->findButton($button); + + if (null === $node) { + throw new ElementNotFoundException($this->session, 'button', 'id|name|title|value', $button); + } + + return $node; + } + + /** + * Checks that specific button does not exists on the current page. + * + * @param string $button button id|name|title|value + * @param TraversableElement $container document to check against + * + * @return NodeElement + */ + public function buttonNotExists($button, TraversableElement $container = null) + { + $container = $container ?: $this->session->getPage(); + $node = $container->findButton($button); + + $this->assert(null === $node, sprintf('A button "%s" appears on this page, but it should not.', $button)); + } + + /** + * Checks that specific select field exists on the current page. + * + * @param string $select select id|name|label + * @param TraversableElement $container document to check against + * + * @return NodeElement + * + * @throws ElementNotFoundException + */ + public function selectExists($select, TraversableElement $container = null) + { + $container = $container ?: $this->session->getPage(); + $node = $container->find('named', array('select', $select)); + + if (null === $node) { + throw new ElementNotFoundException($this->session, 'select', 'id|name|label', $select); + } + + return $node; + } + + /** + * Checks that specific select field does not exists on the current page. + * + * @param string $select select id|name|label + * @param TraversableElement $container document to check against + * + * @return NodeElement + */ + public function selectNotExists($select, TraversableElement $container = null) + { + $container = $container ?: $this->session->getPage(); + $node = $container->find('named', array('select', $select)); + + $this->assert(null === $node, sprintf('A select "%s" appears on this page, but it should not.', $select)); + } + /** * Checks that specific checkbox is checked. * diff --git a/tests/WebAssertTest.php b/tests/WebAssertTest.php index 1d79f9105..36ff92a22 100644 --- a/tests/WebAssertTest.php +++ b/tests/WebAssertTest.php @@ -3,6 +3,7 @@ namespace Behat\Mink\Tests; use Behat\Mink\Exception\ExpectationException; +use Behat\Mink\Selector\SelectorsHandler; use Behat\Mink\WebAssert; class WebAssertTest extends \PHPUnit_Framework_TestCase @@ -1059,6 +1060,140 @@ public function testFieldNotExists() ); } + public function testButtonExists() + { + $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('findButton') + ->with('test') + ->will($this->onConsecutiveCalls($element, null)) + ; + + $this->assertCorrectAssertion('buttonExists', array('test')); + $this->assertWrongAssertion( + 'buttonExists', + array('test'), + 'Behat\\Mink\\Exception\\ElementNotFoundException', + 'Button with id|name|title|value "test" not found.' + ); + } + + public function testButtonNotExists() + { + $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('findButton') + ->with('test') + ->will($this->onConsecutiveCalls(null, $element)) + ; + + $this->assertCorrectAssertion('buttonNotExists', array('test')); + $this->assertWrongAssertion( + 'buttonNotExists', + array('test'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'A button "test" appears on this page, but it should not.' + ); + } + + public function testSelectExists() + { + $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') + ->will($this->onConsecutiveCalls($element, null)) + ; + + $this->assertCorrectAssertion('selectExists', array('choice')); + $this->assertWrongAssertion( + 'selectExists', + array('test'), + 'Behat\\Mink\\Exception\\ElementNotFoundException', + 'Select with id|name|label "test" not found.' + ); + } + + public function testSelectNotExists() + { + $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') + ->will($this->onConsecutiveCalls(null, $element)) + ; + + $this->assertCorrectAssertion('selectNotExists', array('choice')); + $this->assertWrongAssertion( + 'selectNotExists', + array('test'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'A select "test" appears on this page, but it should not.' + ); + } + public function testFieldValueEquals() { $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')