From 02eec04ba704c956a4f454b0f84cca65d84b0561 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Fri, 2 May 2014 03:41:16 +0200 Subject: [PATCH] Added extra unit tests in the driver --- src/Behat/Mink/Driver/BrowserKitDriver.php | 9 +- .../Mink/Driver/BrowserKitDriverTest.php | 2 +- tests/Behat/Mink/Driver/ExtraDriverTest.php | 181 ++++++++++++++++++ 3 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 tests/Behat/Mink/Driver/ExtraDriverTest.php diff --git a/src/Behat/Mink/Driver/BrowserKitDriver.php b/src/Behat/Mink/Driver/BrowserKitDriver.php index 346c012..a9a9acd 100644 --- a/src/Behat/Mink/Driver/BrowserKitDriver.php +++ b/src/Behat/Mink/Driver/BrowserKitDriver.php @@ -12,6 +12,7 @@ use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\DriverException; +use Behat\Mink\Exception\UnsupportedDriverActionException; use Behat\Mink\Session; use Symfony\Component\BrowserKit\Client; use Symfony\Component\BrowserKit\Cookie; @@ -489,8 +490,9 @@ public function click($xpath) } elseif ($this->canResetForm($crawlerNode)) { $this->resetForm($crawlerNode); } else { - $message = 'BrowserKit driver supports clicking on links and buttons only. But "%s" provided'; - throw new DriverException(sprintf($message, $tagName)); + $message = sprintf('%%s supports clicking on links and buttons only. But "%s" provided', $tagName); + + throw new UnsupportedDriverActionException($message, $this); } } @@ -690,7 +692,7 @@ private function getFormNode(\DOMElement $element) $formId = $element->getAttribute('form'); $formNode = $element->ownerDocument->getElementById($formId); - if (null === $formNode) { + if (null === $formNode || 'form' !== $formNode->nodeName) { throw new DriverException(sprintf('The selected node has an invalid form attribute (%s).', $formId)); } @@ -728,6 +730,7 @@ private function getFieldPosition(\DOMElement $fieldNode) // more than one element contains this name ! // so we need to find the position of $fieldNode foreach ($elements as $key => $element) { + /** @var \DOMElement $element */ if ($element->getNodePath() === $fieldNode->getNodePath()) { return $key; } diff --git a/tests/Behat/Mink/Driver/BrowserKitDriverTest.php b/tests/Behat/Mink/Driver/BrowserKitDriverTest.php index 710694e..1431a30 100644 --- a/tests/Behat/Mink/Driver/BrowserKitDriverTest.php +++ b/tests/Behat/Mink/Driver/BrowserKitDriverTest.php @@ -7,7 +7,7 @@ use Symfony\Component\HttpKernel\Client; /** - * @group browserkitdriver + * @group functional */ class BrowserKitDriverTest extends GeneralDriverTest { diff --git a/tests/Behat/Mink/Driver/ExtraDriverTest.php b/tests/Behat/Mink/Driver/ExtraDriverTest.php new file mode 100644 index 0000000..13b7ea8 --- /dev/null +++ b/tests/Behat/Mink/Driver/ExtraDriverTest.php @@ -0,0 +1,181 @@ +client = new TestClient(); + } + + public function testGetClient() + { + $this->assertSame($this->client, $this->getDriver()->getClient()); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage Unable to access the response before visiting a page + */ + public function testGetResponseHeaderWithoutVisit() + { + $this->getDriver()->getResponseHeaders(); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage Unable to access the response content before visiting a page + */ + public function testFindWithoutVisit() + { + $this->getDriver()->find('//html'); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage Unable to access the request before visiting a page + */ + public function testGetCurrentUrlWithoutVisit() + { + $this->getDriver()->getCurrentUrl(); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage The selected node has an invalid form attribute (foo) + */ + public function testNotMatchingHtml5FormId() + { + $html = <<<'HTML' + + +
+ + +
+ + +HTML; + + $this->client->setNextResponse(new Response($html)); + + $driver = $this->getDriver(); + $driver->visit('/index.php'); + $driver->setValue('//input[./@name="test"]', 'bar'); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage The selected node has an invalid form attribute (foo) + */ + public function testInvalidHtml5FormId() + { + $html = <<<'HTML' + + +
+ + +
+
+ + +HTML; + + $this->client->setNextResponse(new Response($html)); + + $driver = $this->getDriver(); + $driver->visit('/index.php'); + $driver->setValue('//input[./@name="test"]', 'bar'); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage The selected node does not have a form ancestor. + */ + public function testManipulateInputWithoutForm() + { + $html = <<<'HTML' + + +
+ +
+
+ +
+ + +HTML; + + $this->client->setNextResponse(new Response($html)); + + $driver = $this->getDriver(); + $driver->visit('/index.php'); + $driver->setValue('//input[./@name="test"]', 'bar'); + } + + /** + * @expectedException \Behat\Mink\Exception\DriverException + * @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and buttons only. But "div" provided + */ + public function testClickOnUnsupportedElement() + { + $html = <<<'HTML' + + +
+ + +HTML; + + $this->client->setNextResponse(new Response($html)); + + $driver = $this->getDriver(); + $driver->visit('/index.php'); + $driver->click('//div'); + } + + private function getDriver() + { + return new BrowserKitDriver($this->client); + } +} + +class TestClient extends Client +{ + protected $nextResponse = null; + protected $nextScript = null; + + public function setNextResponse(Response $response) + { + $this->nextResponse = $response; + } + + public function setNextScript($script) + { + $this->nextScript = $script; + } + + protected function doRequest($request) + { + if (null === $this->nextResponse) { + return new Response(); + } + + $response = $this->nextResponse; + $this->nextResponse = null; + + return $response; + } +}