3 namespace Behat\Mink\Tests\Driver\Custom;
5 use Behat\Mink\Driver\BrowserKitDriver;
6 use Symfony\Component\BrowserKit\Client;
7 use Symfony\Component\BrowserKit\Response;
9 class ErrorHandlingTest extends \PHPUnit_Framework_TestCase
16 protected function setUp()
18 $this->client = new TestClient();
21 public function testGetClient()
23 $this->assertSame($this->client, $this->getDriver()->getClient());
27 * @expectedException \Behat\Mink\Exception\DriverException
28 * @expectedExceptionMessage Unable to access the response before visiting a page
30 public function testGetResponseHeaderWithoutVisit()
32 $this->getDriver()->getResponseHeaders();
36 * @expectedException \Behat\Mink\Exception\DriverException
37 * @expectedExceptionMessage Unable to access the response content before visiting a page
39 public function testFindWithoutVisit()
41 $this->getDriver()->find('//html');
45 * @expectedException \Behat\Mink\Exception\DriverException
46 * @expectedExceptionMessage Unable to access the request before visiting a page
48 public function testGetCurrentUrlWithoutVisit()
50 $this->getDriver()->getCurrentUrl();
54 * @expectedException \Behat\Mink\Exception\DriverException
55 * @expectedExceptionMessage The selected node has an invalid form attribute (foo)
57 public function testNotMatchingHtml5FormId()
63 <input name="test" value="foo" form="foo">
70 $this->client->setNextResponse(new Response($html));
72 $driver = $this->getDriver();
73 $driver->visit('/index.php');
74 $driver->setValue('//input[./@name="test"]', 'bar');
78 * @expectedException \Behat\Mink\Exception\DriverException
79 * @expectedExceptionMessage The selected node has an invalid form attribute (foo)
81 public function testInvalidHtml5FormId()
87 <input name="test" value="foo" form="foo">
95 $this->client->setNextResponse(new Response($html));
97 $driver = $this->getDriver();
98 $driver->visit('/index.php');
99 $driver->setValue('//input[./@name="test"]', 'bar');
103 * @expectedException \Behat\Mink\Exception\DriverException
104 * @expectedExceptionMessage The selected node does not have a form ancestor.
106 public function testManipulateInputWithoutForm()
112 <input type="submit">
115 <input name="test" value="foo">
121 $this->client->setNextResponse(new Response($html));
123 $driver = $this->getDriver();
124 $driver->visit('/index.php');
125 $driver->setValue('//input[./@name="test"]', 'bar');
129 * @expectedException \Behat\Mink\Exception\DriverException
130 * @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and submit or reset buttons only. But "div" provided
132 public function testClickOnUnsupportedElement()
142 $this->client->setNextResponse(new Response($html));
144 $driver = $this->getDriver();
145 $driver->visit('/index.php');
146 $driver->click('//div');
149 private function getDriver()
151 return new BrowserKitDriver($this->client);
155 class TestClient extends Client
157 protected $nextResponse = null;
158 protected $nextScript = null;
160 public function setNextResponse(Response $response)
162 $this->nextResponse = $response;
165 public function setNextScript($script)
167 $this->nextScript = $script;
170 protected function doRequest($request)
172 if (null === $this->nextResponse) {
173 return new Response();
176 $response = $this->nextResponse;
177 $this->nextResponse = null;