BrowserKitDriver.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. <?php
  2. /*
  3. * This file is part of the Behat\Mink.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Mink\Driver;
  10. use Behat\Mink\Exception\DriverException;
  11. use Behat\Mink\Exception\UnsupportedDriverActionException;
  12. use Symfony\Component\BrowserKit\Client;
  13. use Symfony\Component\BrowserKit\Cookie;
  14. use Symfony\Component\BrowserKit\Response;
  15. use Symfony\Component\DomCrawler\Crawler;
  16. use Symfony\Component\DomCrawler\Field\ChoiceFormField;
  17. use Symfony\Component\DomCrawler\Field\FileFormField;
  18. use Symfony\Component\DomCrawler\Field\FormField;
  19. use Symfony\Component\DomCrawler\Field\InputFormField;
  20. use Symfony\Component\DomCrawler\Field\TextareaFormField;
  21. use Symfony\Component\DomCrawler\Form;
  22. use Symfony\Component\HttpKernel\Client as HttpKernelClient;
  23. /**
  24. * Symfony2 BrowserKit driver.
  25. *
  26. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  27. */
  28. class BrowserKitDriver extends CoreDriver
  29. {
  30. private $client;
  31. /**
  32. * @var Form[]
  33. */
  34. private $forms = array();
  35. private $serverParameters = array();
  36. private $started = false;
  37. private $removeScriptFromUrl = false;
  38. private $removeHostFromUrl = false;
  39. /**
  40. * Initializes BrowserKit driver.
  41. *
  42. * @param Client $client BrowserKit client instance
  43. * @param string|null $baseUrl Base URL for HttpKernel clients
  44. */
  45. public function __construct(Client $client, $baseUrl = null)
  46. {
  47. $this->client = $client;
  48. $this->client->followRedirects(true);
  49. if ($baseUrl !== null && $client instanceof HttpKernelClient) {
  50. $client->setServerParameter('SCRIPT_FILENAME', parse_url($baseUrl, PHP_URL_PATH));
  51. }
  52. }
  53. /**
  54. * Returns BrowserKit HTTP client instance.
  55. *
  56. * @return Client
  57. */
  58. public function getClient()
  59. {
  60. return $this->client;
  61. }
  62. /**
  63. * Tells driver to remove hostname from URL.
  64. *
  65. * @param Boolean $remove
  66. *
  67. * @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead.
  68. */
  69. public function setRemoveHostFromUrl($remove = true)
  70. {
  71. trigger_error(
  72. 'setRemoveHostFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.',
  73. E_USER_DEPRECATED
  74. );
  75. $this->removeHostFromUrl = (bool) $remove;
  76. }
  77. /**
  78. * Tells driver to remove script name from URL.
  79. *
  80. * @param Boolean $remove
  81. *
  82. * @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead.
  83. */
  84. public function setRemoveScriptFromUrl($remove = true)
  85. {
  86. trigger_error(
  87. 'setRemoveScriptFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.',
  88. E_USER_DEPRECATED
  89. );
  90. $this->removeScriptFromUrl = (bool) $remove;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function start()
  96. {
  97. $this->started = true;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function isStarted()
  103. {
  104. return $this->started;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function stop()
  110. {
  111. $this->reset();
  112. $this->started = false;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function reset()
  118. {
  119. // Restarting the client resets the cookies and the history
  120. $this->client->restart();
  121. $this->forms = array();
  122. $this->serverParameters = array();
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function visit($url)
  128. {
  129. $this->client->request('GET', $this->prepareUrl($url), array(), array(), $this->serverParameters);
  130. $this->forms = array();
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getCurrentUrl()
  136. {
  137. $request = $this->client->getInternalRequest();
  138. if ($request === null) {
  139. throw new DriverException('Unable to access the request before visiting a page');
  140. }
  141. return $request->getUri();
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function reload()
  147. {
  148. $this->client->reload();
  149. $this->forms = array();
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function forward()
  155. {
  156. $this->client->forward();
  157. $this->forms = array();
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function back()
  163. {
  164. $this->client->back();
  165. $this->forms = array();
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function setBasicAuth($user, $password)
  171. {
  172. if (false === $user) {
  173. unset($this->serverParameters['PHP_AUTH_USER'], $this->serverParameters['PHP_AUTH_PW']);
  174. return;
  175. }
  176. $this->serverParameters['PHP_AUTH_USER'] = $user;
  177. $this->serverParameters['PHP_AUTH_PW'] = $password;
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function setRequestHeader($name, $value)
  183. {
  184. $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
  185. $name = str_replace('-', '_', strtoupper($name));
  186. // CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER
  187. if (!isset($contentHeaders[$name])) {
  188. $name = 'HTTP_' . $name;
  189. }
  190. $this->serverParameters[$name] = $value;
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function getResponseHeaders()
  196. {
  197. return $this->getResponse()->getHeaders();
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function setCookie($name, $value = null)
  203. {
  204. if (null === $value) {
  205. $this->deleteCookie($name);
  206. return;
  207. }
  208. $jar = $this->client->getCookieJar();
  209. $jar->set(new Cookie($name, $value));
  210. }
  211. /**
  212. * Deletes a cookie by name.
  213. *
  214. * @param string $name Cookie name.
  215. */
  216. private function deleteCookie($name)
  217. {
  218. $path = $this->getCookiePath();
  219. $jar = $this->client->getCookieJar();
  220. do {
  221. if (null !== $jar->get($name, $path)) {
  222. $jar->expire($name, $path);
  223. }
  224. $path = preg_replace('/.$/', '', $path);
  225. } while ($path);
  226. }
  227. /**
  228. * Returns current cookie path.
  229. *
  230. * @return string
  231. */
  232. private function getCookiePath()
  233. {
  234. $path = dirname(parse_url($this->getCurrentUrl(), PHP_URL_PATH));
  235. if ('\\' === DIRECTORY_SEPARATOR) {
  236. $path = str_replace('\\', '/', $path);
  237. }
  238. return $path;
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function getCookie($name)
  244. {
  245. // Note that the following doesn't work well because
  246. // Symfony\Component\BrowserKit\CookieJar stores cookies by name,
  247. // path, AND domain and if you don't fill them all in correctly then
  248. // you won't get the value that you're expecting.
  249. //
  250. // $jar = $this->client->getCookieJar();
  251. //
  252. // if (null !== $cookie = $jar->get($name)) {
  253. // return $cookie->getValue();
  254. // }
  255. $allValues = $this->client->getCookieJar()->allValues($this->getCurrentUrl());
  256. if (isset($allValues[$name])) {
  257. return $allValues[$name];
  258. }
  259. return null;
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function getStatusCode()
  265. {
  266. return $this->getResponse()->getStatus();
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function getContent()
  272. {
  273. return $this->getResponse()->getContent();
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function findElementXpaths($xpath)
  279. {
  280. $nodes = $this->getCrawler()->filterXPath($xpath);
  281. $elements = array();
  282. foreach ($nodes as $i => $node) {
  283. $elements[] = sprintf('(%s)[%d]', $xpath, $i + 1);
  284. }
  285. return $elements;
  286. }
  287. /**
  288. * {@inheritdoc}
  289. */
  290. public function getTagName($xpath)
  291. {
  292. return $this->getCrawlerNode($this->getFilteredCrawler($xpath))->nodeName;
  293. }
  294. /**
  295. * {@inheritdoc}
  296. */
  297. public function getText($xpath)
  298. {
  299. $text = $this->getFilteredCrawler($xpath)->text();
  300. $text = str_replace("\n", ' ', $text);
  301. $text = preg_replace('/ {2,}/', ' ', $text);
  302. return trim($text);
  303. }
  304. /**
  305. * {@inheritdoc}
  306. */
  307. public function getHtml($xpath)
  308. {
  309. // cut the tag itself (making innerHTML out of outerHTML)
  310. return preg_replace('/^\<[^\>]+\>|\<[^\>]+\>$/', '', $this->getOuterHtml($xpath));
  311. }
  312. /**
  313. * {@inheritdoc}
  314. */
  315. public function getOuterHtml($xpath)
  316. {
  317. $node = $this->getCrawlerNode($this->getFilteredCrawler($xpath));
  318. return $node->ownerDocument->saveHTML($node);
  319. }
  320. /**
  321. * {@inheritdoc}
  322. */
  323. public function getAttribute($xpath, $name)
  324. {
  325. $node = $this->getFilteredCrawler($xpath);
  326. if ($this->getCrawlerNode($node)->hasAttribute($name)) {
  327. return $node->attr($name);
  328. }
  329. return null;
  330. }
  331. /**
  332. * {@inheritdoc}
  333. */
  334. public function getValue($xpath)
  335. {
  336. if (in_array($this->getAttribute($xpath, 'type'), array('submit', 'image', 'button'), true)) {
  337. return $this->getAttribute($xpath, 'value');
  338. }
  339. $node = $this->getCrawlerNode($this->getFilteredCrawler($xpath));
  340. if ('option' === $node->tagName) {
  341. return $this->getOptionValue($node);
  342. }
  343. try {
  344. $field = $this->getFormField($xpath);
  345. } catch (\InvalidArgumentException $e) {
  346. return $this->getAttribute($xpath, 'value');
  347. }
  348. return $field->getValue();
  349. }
  350. /**
  351. * {@inheritdoc}
  352. */
  353. public function setValue($xpath, $value)
  354. {
  355. $this->getFormField($xpath)->setValue($value);
  356. }
  357. /**
  358. * {@inheritdoc}
  359. */
  360. public function check($xpath)
  361. {
  362. $this->getCheckboxField($xpath)->tick();
  363. }
  364. /**
  365. * {@inheritdoc}
  366. */
  367. public function uncheck($xpath)
  368. {
  369. $this->getCheckboxField($xpath)->untick();
  370. }
  371. /**
  372. * {@inheritdoc}
  373. */
  374. public function selectOption($xpath, $value, $multiple = false)
  375. {
  376. $field = $this->getFormField($xpath);
  377. if (!$field instanceof ChoiceFormField) {
  378. throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath));
  379. }
  380. if ($multiple) {
  381. $oldValue = (array) $field->getValue();
  382. $oldValue[] = $value;
  383. $value = $oldValue;
  384. }
  385. $field->select($value);
  386. }
  387. /**
  388. * {@inheritdoc}
  389. */
  390. public function isSelected($xpath)
  391. {
  392. $optionValue = $this->getOptionValue($this->getCrawlerNode($this->getFilteredCrawler($xpath)));
  393. $selectField = $this->getFormField('(' . $xpath . ')/ancestor-or-self::*[local-name()="select"]');
  394. $selectValue = $selectField->getValue();
  395. return is_array($selectValue) ? in_array($optionValue, $selectValue, true) : $optionValue === $selectValue;
  396. }
  397. /**
  398. * {@inheritdoc}
  399. */
  400. public function click($xpath)
  401. {
  402. $crawler = $this->getFilteredCrawler($xpath);
  403. $node = $this->getCrawlerNode($crawler);
  404. $tagName = $node->nodeName;
  405. if ('a' === $tagName) {
  406. $this->client->click($crawler->link());
  407. $this->forms = array();
  408. } elseif ($this->canSubmitForm($node)) {
  409. $this->submit($crawler->form());
  410. } elseif ($this->canResetForm($node)) {
  411. $this->resetForm($node);
  412. } else {
  413. $message = sprintf('%%s supports clicking on links and submit or reset buttons only. But "%s" provided', $tagName);
  414. throw new UnsupportedDriverActionException($message, $this);
  415. }
  416. }
  417. /**
  418. * {@inheritdoc}
  419. */
  420. public function isChecked($xpath)
  421. {
  422. $field = $this->getFormField($xpath);
  423. if (!$field instanceof ChoiceFormField || 'select' === $field->getType()) {
  424. throw new DriverException(sprintf('Impossible to get the checked state of the element with XPath "%s" as it is not a checkbox or radio input', $xpath));
  425. }
  426. if ('checkbox' === $field->getType()) {
  427. return $field->hasValue();
  428. }
  429. $radio = $this->getCrawlerNode($this->getFilteredCrawler($xpath));
  430. return $radio->getAttribute('value') === $field->getValue();
  431. }
  432. /**
  433. * {@inheritdoc}
  434. */
  435. public function attachFile($xpath, $path)
  436. {
  437. $field = $this->getFormField($xpath);
  438. if (!$field instanceof FileFormField) {
  439. throw new DriverException(sprintf('Impossible to attach a file on the element with XPath "%s" as it is not a file input', $xpath));
  440. }
  441. $field->upload($path);
  442. }
  443. /**
  444. * {@inheritdoc}
  445. */
  446. public function submitForm($xpath)
  447. {
  448. $crawler = $this->getFilteredCrawler($xpath);
  449. $this->submit($crawler->form());
  450. }
  451. /**
  452. * @return Response
  453. *
  454. * @throws DriverException If there is not response yet
  455. */
  456. protected function getResponse()
  457. {
  458. $response = $this->client->getInternalResponse();
  459. if (null === $response) {
  460. throw new DriverException('Unable to access the response before visiting a page');
  461. }
  462. return $response;
  463. }
  464. /**
  465. * Prepares URL for visiting.
  466. * Removes "*.php/" from urls and then passes it to BrowserKitDriver::visit().
  467. *
  468. * @param string $url
  469. *
  470. * @return string
  471. */
  472. protected function prepareUrl($url)
  473. {
  474. $replacement = ($this->removeHostFromUrl ? '' : '$1') . ($this->removeScriptFromUrl ? '' : '$2');
  475. return preg_replace('#(https?\://[^/]+)(/[^/\.]+\.php)?#', $replacement, $url);
  476. }
  477. /**
  478. * Returns form field from XPath query.
  479. *
  480. * @param string $xpath
  481. *
  482. * @return FormField
  483. *
  484. * @throws DriverException
  485. */
  486. protected function getFormField($xpath)
  487. {
  488. $fieldNode = $this->getCrawlerNode($this->getFilteredCrawler($xpath));
  489. $fieldName = str_replace('[]', '', $fieldNode->getAttribute('name'));
  490. $formNode = $this->getFormNode($fieldNode);
  491. $formId = $this->getFormNodeId($formNode);
  492. if (!isset($this->forms[$formId])) {
  493. $this->forms[$formId] = new Form($formNode, $this->getCurrentUrl());
  494. }
  495. if (is_array($this->forms[$formId][$fieldName])) {
  496. return $this->forms[$formId][$fieldName][$this->getFieldPosition($fieldNode)];
  497. }
  498. return $this->forms[$formId][$fieldName];
  499. }
  500. /**
  501. * Returns the checkbox field from xpath query, ensuring it is valid.
  502. *
  503. * @param string $xpath
  504. *
  505. * @return ChoiceFormField
  506. *
  507. * @throws DriverException when the field is not a checkbox
  508. */
  509. private function getCheckboxField($xpath)
  510. {
  511. $field = $this->getFormField($xpath);
  512. if (!$field instanceof ChoiceFormField) {
  513. throw new DriverException(sprintf('Impossible to check the element with XPath "%s" as it is not a checkbox', $xpath));
  514. }
  515. return $field;
  516. }
  517. /**
  518. * @param \DOMElement $element
  519. *
  520. * @return \DOMElement
  521. *
  522. * @throws DriverException if the form node cannot be found
  523. */
  524. private function getFormNode(\DOMElement $element)
  525. {
  526. if ($element->hasAttribute('form')) {
  527. $formId = $element->getAttribute('form');
  528. $formNode = $element->ownerDocument->getElementById($formId);
  529. if (null === $formNode || 'form' !== $formNode->nodeName) {
  530. throw new DriverException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
  531. }
  532. return $formNode;
  533. }
  534. $formNode = $element;
  535. do {
  536. // use the ancestor form element
  537. if (null === $formNode = $formNode->parentNode) {
  538. throw new DriverException('The selected node does not have a form ancestor.');
  539. }
  540. } while ('form' !== $formNode->nodeName);
  541. return $formNode;
  542. }
  543. /**
  544. * Gets the position of the field node among elements with the same name
  545. *
  546. * BrowserKit uses the field name as index to find the field in its Form object.
  547. * When multiple fields have the same name (checkboxes for instance), it will return
  548. * an array of elements in the order they appear in the DOM.
  549. *
  550. * @param \DOMElement $fieldNode
  551. *
  552. * @return integer
  553. */
  554. private function getFieldPosition(\DOMElement $fieldNode)
  555. {
  556. $elements = $this->getCrawler()->filterXPath('//*[@name=\''.$fieldNode->getAttribute('name').'\']');
  557. if (count($elements) > 1) {
  558. // more than one element contains this name !
  559. // so we need to find the position of $fieldNode
  560. foreach ($elements as $key => $element) {
  561. /** @var \DOMElement $element */
  562. if ($element->getNodePath() === $fieldNode->getNodePath()) {
  563. return $key;
  564. }
  565. }
  566. }
  567. return 0;
  568. }
  569. private function submit(Form $form)
  570. {
  571. $formId = $this->getFormNodeId($form->getFormNode());
  572. if (isset($this->forms[$formId])) {
  573. $this->mergeForms($form, $this->forms[$formId]);
  574. }
  575. // remove empty file fields from request
  576. foreach ($form->getFiles() as $name => $field) {
  577. if (empty($field['name']) && empty($field['tmp_name'])) {
  578. $form->remove($name);
  579. }
  580. }
  581. foreach ($form->all() as $field) {
  582. // Add a fix for https://github.com/symfony/symfony/pull/10733 to support Symfony versions which are not fixed
  583. if ($field instanceof TextareaFormField && null === $field->getValue()) {
  584. $field->setValue('');
  585. }
  586. }
  587. $this->client->submit($form);
  588. $this->forms = array();
  589. }
  590. private function resetForm(\DOMElement $fieldNode)
  591. {
  592. $formNode = $this->getFormNode($fieldNode);
  593. $formId = $this->getFormNodeId($formNode);
  594. unset($this->forms[$formId]);
  595. }
  596. /**
  597. * Determines if a node can submit a form.
  598. *
  599. * @param \DOMElement $node Node.
  600. *
  601. * @return boolean
  602. */
  603. private function canSubmitForm(\DOMElement $node)
  604. {
  605. $type = $node->hasAttribute('type') ? $node->getAttribute('type') : null;
  606. if ('input' === $node->nodeName && in_array($type, array('submit', 'image'), true)) {
  607. return true;
  608. }
  609. return 'button' === $node->nodeName && (null === $type || 'submit' === $type);
  610. }
  611. /**
  612. * Determines if a node can reset a form.
  613. *
  614. * @param \DOMElement $node Node.
  615. *
  616. * @return boolean
  617. */
  618. private function canResetForm(\DOMElement $node)
  619. {
  620. $type = $node->hasAttribute('type') ? $node->getAttribute('type') : null;
  621. return in_array($node->nodeName, array('input', 'button'), true) && 'reset' === $type;
  622. }
  623. /**
  624. * Returns form node unique identifier.
  625. *
  626. * @param \DOMElement $form
  627. *
  628. * @return string
  629. */
  630. private function getFormNodeId(\DOMElement $form)
  631. {
  632. return md5($form->getLineNo() . $form->getNodePath() . $form->nodeValue);
  633. }
  634. /**
  635. * Gets the value of an option element
  636. *
  637. * @param \DOMElement $option
  638. *
  639. * @return string
  640. *
  641. * @see \Symfony\Component\DomCrawler\Field\ChoiceFormField::buildOptionValue
  642. */
  643. private function getOptionValue(\DOMElement $option)
  644. {
  645. if ($option->hasAttribute('value')) {
  646. return $option->getAttribute('value');
  647. }
  648. if (!empty($option->nodeValue)) {
  649. return $option->nodeValue;
  650. }
  651. return '1'; // DomCrawler uses 1 by default if there is no text in the option
  652. }
  653. /**
  654. * Merges second form values into first one.
  655. *
  656. * @param Form $to merging target
  657. * @param Form $from merging source
  658. */
  659. private function mergeForms(Form $to, Form $from)
  660. {
  661. foreach ($from->all() as $name => $field) {
  662. $fieldReflection = new \ReflectionObject($field);
  663. $nodeReflection = $fieldReflection->getProperty('node');
  664. $valueReflection = $fieldReflection->getProperty('value');
  665. $nodeReflection->setAccessible(true);
  666. $valueReflection->setAccessible(true);
  667. $isIgnoredField = $field instanceof InputFormField &&
  668. in_array($nodeReflection->getValue($field)->getAttribute('type'), array('submit', 'button', 'image'), true);
  669. if (!$isIgnoredField) {
  670. $valueReflection->setValue($to[$name], $valueReflection->getValue($field));
  671. }
  672. }
  673. }
  674. /**
  675. * Returns DOMElement from crawler instance.
  676. *
  677. * @param Crawler $crawler
  678. *
  679. * @return \DOMElement
  680. *
  681. * @throws DriverException when the node does not exist
  682. */
  683. private function getCrawlerNode(Crawler $crawler)
  684. {
  685. $node = null;
  686. if ($crawler instanceof \Iterator) {
  687. // for symfony 2.3 compatibility as getNode is not public before symfony 2.4
  688. $crawler->rewind();
  689. $node = $crawler->current();
  690. } else {
  691. $node = $crawler->getNode(0);
  692. }
  693. if (null !== $node) {
  694. return $node;
  695. }
  696. throw new DriverException('The element does not exist');
  697. }
  698. /**
  699. * Returns a crawler filtered for the given XPath, requiring at least 1 result.
  700. *
  701. * @param string $xpath
  702. *
  703. * @return Crawler
  704. *
  705. * @throws DriverException when no matching elements are found
  706. */
  707. private function getFilteredCrawler($xpath)
  708. {
  709. if (!count($crawler = $this->getCrawler()->filterXPath($xpath))) {
  710. throw new DriverException(sprintf('There is no element matching XPath "%s"', $xpath));
  711. }
  712. return $crawler;
  713. }
  714. /**
  715. * Returns crawler instance (got from client).
  716. *
  717. * @return Crawler
  718. *
  719. * @throws DriverException
  720. */
  721. private function getCrawler()
  722. {
  723. $crawler = $this->client->getCrawler();
  724. if (null === $crawler) {
  725. throw new DriverException('Unable to access the response content before visiting a page');
  726. }
  727. return $crawler;
  728. }
  729. }