3 namespace Behat\Mink\Tests\Driver\Custom;
5 use Behat\Mink\Driver\BrowserKitDriver;
6 use PHPUnit\Framework\TestCase;
7 use Symfony\Component\BrowserKit\Client;
8 use Symfony\Component\BrowserKit\Response;
10 class ErrorHandlingTest extends TestCase
17 protected function setUp()
19 $this->client = new TestClient();
22 public function testGetClient()
24 $this->assertSame($this->client, $this->getDriver()->getClient());
28 * @expectedException \Behat\Mink\Exception\DriverException
29 * @expectedExceptionMessage Unable to access the response before visiting a page
31 public function testGetResponseHeaderWithoutVisit()
33 $this->getDriver()->getResponseHeaders();
37 * @expectedException \Behat\Mink\Exception\DriverException
38 * @expectedExceptionMessage Unable to access the response content before visiting a page
40 public function testFindWithoutVisit()
42 $this->getDriver()->find('//html');
46 * @expectedException \Behat\Mink\Exception\DriverException
47 * @expectedExceptionMessage Unable to access the request before visiting a page
49 public function testGetCurrentUrlWithoutVisit()
51 $this->getDriver()->getCurrentUrl();
55 * @expectedException \Behat\Mink\Exception\DriverException
56 * @expectedExceptionMessage The selected node has an invalid form attribute (foo)
58 public function testNotMatchingHtml5FormId()
64 <input name="test" value="foo" form="foo">
71 $this->client->setNextResponse(new Response($html));
73 $driver = $this->getDriver();
74 $driver->visit('/index.php');
75 $driver->setValue('//input[./@name="test"]', 'bar');
79 * @expectedException \Behat\Mink\Exception\DriverException
80 * @expectedExceptionMessage The selected node has an invalid form attribute (foo)
82 public function testInvalidHtml5FormId()
88 <input name="test" value="foo" form="foo">
96 $this->client->setNextResponse(new Response($html));
98 $driver = $this->getDriver();
99 $driver->visit('/index.php');
100 $driver->setValue('//input[./@name="test"]', 'bar');
104 * @expectedException \Behat\Mink\Exception\DriverException
105 * @expectedExceptionMessage The selected node does not have a form ancestor.
107 public function testManipulateInputWithoutForm()
113 <input type="submit">
116 <input name="test" value="foo">
122 $this->client->setNextResponse(new Response($html));
124 $driver = $this->getDriver();
125 $driver->visit('/index.php');
126 $driver->setValue('//input[./@name="test"]', 'bar');
130 * @expectedException \Behat\Mink\Exception\DriverException
131 * @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and submit or reset buttons only. But "div" provided
133 public function testClickOnUnsupportedElement()
143 $this->client->setNextResponse(new Response($html));
145 $driver = $this->getDriver();
146 $driver->visit('/index.php');
147 $driver->click('//div');
150 private function getDriver()
152 return new BrowserKitDriver($this->client);
156 class TestClient extends Client
158 protected $nextResponse = null;
159 protected $nextScript = null;
161 public function setNextResponse(Response $response)
163 $this->nextResponse = $response;
166 public function setNextScript($script)
168 $this->nextScript = $script;
171 protected function doRequest($request)
173 if (null === $this->nextResponse) {
174 return new Response();
177 $response = $this->nextResponse;
178 $this->nextResponse = null;