View source
<?php
namespace Behat\Mink\Driver;
use Behat\Mink\Exception\DriverException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\Response;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
use Symfony\Component\DomCrawler\Field\FileFormField;
use Symfony\Component\DomCrawler\Field\FormField;
use Symfony\Component\DomCrawler\Field\InputFormField;
use Symfony\Component\DomCrawler\Field\TextareaFormField;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\HttpKernel\Client as HttpKernelClient;
class BrowserKitDriver extends CoreDriver {
private $client;
private $forms = array();
private $serverParameters = array();
private $started = false;
private $removeScriptFromUrl = false;
private $removeHostFromUrl = false;
public function __construct(Client $client, $baseUrl = null) {
$this->client = $client;
$this->client
->followRedirects(true);
if ($baseUrl !== null && $client instanceof HttpKernelClient) {
$client
->setServerParameter('SCRIPT_FILENAME', parse_url($baseUrl, PHP_URL_PATH));
}
}
public function getClient() {
return $this->client;
}
public function setRemoveHostFromUrl($remove = true) {
trigger_error('setRemoveHostFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.', E_USER_DEPRECATED);
$this->removeHostFromUrl = (bool) $remove;
}
public function setRemoveScriptFromUrl($remove = true) {
trigger_error('setRemoveScriptFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.', E_USER_DEPRECATED);
$this->removeScriptFromUrl = (bool) $remove;
}
public function start() {
$this->started = true;
}
public function isStarted() {
return $this->started;
}
public function stop() {
$this
->reset();
$this->started = false;
}
public function reset() {
$this->client
->restart();
$this->forms = array();
$this->serverParameters = array();
}
public function visit($url) {
$this->client
->request('GET', $this
->prepareUrl($url), array(), array(), $this->serverParameters);
$this->forms = array();
}
public function getCurrentUrl() {
$request = $this->client
->getInternalRequest();
if ($request === null) {
throw new DriverException('Unable to access the request before visiting a page');
}
return $request
->getUri();
}
public function reload() {
$this->client
->reload();
$this->forms = array();
}
public function forward() {
$this->client
->forward();
$this->forms = array();
}
public function back() {
$this->client
->back();
$this->forms = array();
}
public function setBasicAuth($user, $password) {
if (false === $user) {
unset($this->serverParameters['PHP_AUTH_USER'], $this->serverParameters['PHP_AUTH_PW']);
return;
}
$this->serverParameters['PHP_AUTH_USER'] = $user;
$this->serverParameters['PHP_AUTH_PW'] = $password;
}
public function setRequestHeader($name, $value) {
$contentHeaders = array(
'CONTENT_LENGTH' => true,
'CONTENT_MD5' => true,
'CONTENT_TYPE' => true,
);
$name = str_replace('-', '_', strtoupper($name));
if (!isset($contentHeaders[$name])) {
$name = 'HTTP_' . $name;
}
$this->serverParameters[$name] = $value;
}
public function getResponseHeaders() {
return $this
->getResponse()
->getHeaders();
}
public function setCookie($name, $value = null) {
if (null === $value) {
$this
->deleteCookie($name);
return;
}
$jar = $this->client
->getCookieJar();
$jar
->set(new Cookie($name, $value));
}
private function deleteCookie($name) {
$path = $this
->getCookiePath();
$jar = $this->client
->getCookieJar();
do {
if (null !== $jar
->get($name, $path)) {
$jar
->expire($name, $path);
}
$path = preg_replace('/.$/', '', $path);
} while ($path);
}
private function getCookiePath() {
$path = dirname(parse_url($this
->getCurrentUrl(), PHP_URL_PATH));
if ('\\' === DIRECTORY_SEPARATOR) {
$path = str_replace('\\', '/', $path);
}
return $path;
}
public function getCookie($name) {
$allValues = $this->client
->getCookieJar()
->allValues($this
->getCurrentUrl());
if (isset($allValues[$name])) {
return $allValues[$name];
}
return null;
}
public function getStatusCode() {
return $this
->getResponse()
->getStatus();
}
public function getContent() {
return $this
->getResponse()
->getContent();
}
public function findElementXpaths($xpath) {
$nodes = $this
->getCrawler()
->filterXPath($xpath);
$elements = array();
foreach ($nodes as $i => $node) {
$elements[] = sprintf('(%s)[%d]', $xpath, $i + 1);
}
return $elements;
}
public function getTagName($xpath) {
return $this
->getCrawlerNode($this
->getFilteredCrawler($xpath))->nodeName;
}
public function getText($xpath) {
$text = $this
->getFilteredCrawler($xpath)
->text();
$text = str_replace("\n", ' ', $text);
$text = preg_replace('/ {2,}/', ' ', $text);
return trim($text);
}
public function getHtml($xpath) {
return preg_replace('/^\\<[^\\>]+\\>|\\<[^\\>]+\\>$/', '', $this
->getOuterHtml($xpath));
}
public function getOuterHtml($xpath) {
$node = $this
->getCrawlerNode($this
->getFilteredCrawler($xpath));
return $node->ownerDocument
->saveHTML($node);
}
public function getAttribute($xpath, $name) {
$node = $this
->getFilteredCrawler($xpath);
if ($this
->getCrawlerNode($node)
->hasAttribute($name)) {
return $node
->attr($name);
}
return null;
}
public function getValue($xpath) {
if (in_array($this
->getAttribute($xpath, 'type'), array(
'submit',
'image',
'button',
), true)) {
return $this
->getAttribute($xpath, 'value');
}
$node = $this
->getCrawlerNode($this
->getFilteredCrawler($xpath));
if ('option' === $node->tagName) {
return $this
->getOptionValue($node);
}
try {
$field = $this
->getFormField($xpath);
} catch (\InvalidArgumentException $e) {
return $this
->getAttribute($xpath, 'value');
}
return $field
->getValue();
}
public function setValue($xpath, $value) {
$this
->getFormField($xpath)
->setValue($value);
}
public function check($xpath) {
$this
->getCheckboxField($xpath)
->tick();
}
public function uncheck($xpath) {
$this
->getCheckboxField($xpath)
->untick();
}
public function selectOption($xpath, $value, $multiple = false) {
$field = $this
->getFormField($xpath);
if (!$field instanceof ChoiceFormField) {
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));
}
if ($multiple) {
$oldValue = (array) $field
->getValue();
$oldValue[] = $value;
$value = $oldValue;
}
$field
->select($value);
}
public function isSelected($xpath) {
$optionValue = $this
->getOptionValue($this
->getCrawlerNode($this
->getFilteredCrawler($xpath)));
$selectField = $this
->getFormField('(' . $xpath . ')/ancestor-or-self::*[local-name()="select"]');
$selectValue = $selectField
->getValue();
return is_array($selectValue) ? in_array($optionValue, $selectValue, true) : $optionValue === $selectValue;
}
public function click($xpath) {
$crawler = $this
->getFilteredCrawler($xpath);
$node = $this
->getCrawlerNode($crawler);
$tagName = $node->nodeName;
if ('a' === $tagName) {
$this->client
->click($crawler
->link());
$this->forms = array();
}
elseif ($this
->canSubmitForm($node)) {
$this
->submit($crawler
->form());
}
elseif ($this
->canResetForm($node)) {
$this
->resetForm($node);
}
else {
$message = sprintf('%%s supports clicking on links and submit or reset buttons only. But "%s" provided', $tagName);
throw new UnsupportedDriverActionException($message, $this);
}
}
public function isChecked($xpath) {
$field = $this
->getFormField($xpath);
if (!$field instanceof ChoiceFormField || 'select' === $field
->getType()) {
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));
}
if ('checkbox' === $field
->getType()) {
return $field
->hasValue();
}
$radio = $this
->getCrawlerNode($this
->getFilteredCrawler($xpath));
return $radio
->getAttribute('value') === $field
->getValue();
}
public function attachFile($xpath, $path) {
$field = $this
->getFormField($xpath);
if (!$field instanceof FileFormField) {
throw new DriverException(sprintf('Impossible to attach a file on the element with XPath "%s" as it is not a file input', $xpath));
}
$field
->upload($path);
}
public function submitForm($xpath) {
$crawler = $this
->getFilteredCrawler($xpath);
$this
->submit($crawler
->form());
}
protected function getResponse() {
$response = $this->client
->getInternalResponse();
if (null === $response) {
throw new DriverException('Unable to access the response before visiting a page');
}
return $response;
}
protected function prepareUrl($url) {
$replacement = ($this->removeHostFromUrl ? '' : '$1') . ($this->removeScriptFromUrl ? '' : '$2');
return preg_replace('#(https?\\://[^/]+)(/[^/\\.]+\\.php)?#', $replacement, $url);
}
protected function getFormField($xpath) {
$fieldNode = $this
->getCrawlerNode($this
->getFilteredCrawler($xpath));
$fieldName = str_replace('[]', '', $fieldNode
->getAttribute('name'));
$formNode = $this
->getFormNode($fieldNode);
$formId = $this
->getFormNodeId($formNode);
if (!isset($this->forms[$formId])) {
$this->forms[$formId] = new Form($formNode, $this
->getCurrentUrl());
}
if (is_array($this->forms[$formId][$fieldName])) {
return $this->forms[$formId][$fieldName][$this
->getFieldPosition($fieldNode)];
}
return $this->forms[$formId][$fieldName];
}
private function getCheckboxField($xpath) {
$field = $this
->getFormField($xpath);
if (!$field instanceof ChoiceFormField) {
throw new DriverException(sprintf('Impossible to check the element with XPath "%s" as it is not a checkbox', $xpath));
}
return $field;
}
private function getFormNode(\DOMElement $element) {
if ($element
->hasAttribute('form')) {
$formId = $element
->getAttribute('form');
$formNode = $element->ownerDocument
->getElementById($formId);
if (null === $formNode || 'form' !== $formNode->nodeName) {
throw new DriverException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
}
return $formNode;
}
$formNode = $element;
do {
if (null === ($formNode = $formNode->parentNode)) {
throw new DriverException('The selected node does not have a form ancestor.');
}
} while ('form' !== $formNode->nodeName);
return $formNode;
}
private function getFieldPosition(\DOMElement $fieldNode) {
$elements = $this
->getCrawler()
->filterXPath('//*[@name=\'' . $fieldNode
->getAttribute('name') . '\']');
if (count($elements) > 1) {
foreach ($elements as $key => $element) {
if ($element
->getNodePath() === $fieldNode
->getNodePath()) {
return $key;
}
}
}
return 0;
}
private function submit(Form $form) {
$formId = $this
->getFormNodeId($form
->getFormNode());
if (isset($this->forms[$formId])) {
$this
->mergeForms($form, $this->forms[$formId]);
}
foreach ($form
->getFiles() as $name => $field) {
if (empty($field['name']) && empty($field['tmp_name'])) {
$form
->remove($name);
}
}
foreach ($form
->all() as $field) {
if ($field instanceof TextareaFormField && null === $field
->getValue()) {
$field
->setValue('');
}
}
$this->client
->submit($form);
$this->forms = array();
}
private function resetForm(\DOMElement $fieldNode) {
$formNode = $this
->getFormNode($fieldNode);
$formId = $this
->getFormNodeId($formNode);
unset($this->forms[$formId]);
}
private function canSubmitForm(\DOMElement $node) {
$type = $node
->hasAttribute('type') ? $node
->getAttribute('type') : null;
if ('input' === $node->nodeName && in_array($type, array(
'submit',
'image',
), true)) {
return true;
}
return 'button' === $node->nodeName && (null === $type || 'submit' === $type);
}
private function canResetForm(\DOMElement $node) {
$type = $node
->hasAttribute('type') ? $node
->getAttribute('type') : null;
return in_array($node->nodeName, array(
'input',
'button',
), true) && 'reset' === $type;
}
private function getFormNodeId(\DOMElement $form) {
return md5($form
->getLineNo() . $form
->getNodePath() . $form->nodeValue);
}
private function getOptionValue(\DOMElement $option) {
if ($option
->hasAttribute('value')) {
return $option
->getAttribute('value');
}
if (!empty($option->nodeValue)) {
return $option->nodeValue;
}
return '1';
}
private function mergeForms(Form $to, Form $from) {
foreach ($from
->all() as $name => $field) {
$fieldReflection = new \ReflectionObject($field);
$nodeReflection = $fieldReflection
->getProperty('node');
$valueReflection = $fieldReflection
->getProperty('value');
$nodeReflection
->setAccessible(true);
$valueReflection
->setAccessible(true);
$isIgnoredField = $field instanceof InputFormField && in_array($nodeReflection
->getValue($field)
->getAttribute('type'), array(
'submit',
'button',
'image',
), true);
if (!$isIgnoredField) {
$valueReflection
->setValue($to[$name], $valueReflection
->getValue($field));
}
}
}
private function getCrawlerNode(Crawler $crawler) {
$crawler
->rewind();
$node = $crawler
->current();
if (null !== $node) {
return $node;
}
throw new DriverException('The element does not exist');
}
private function getFilteredCrawler($xpath) {
if (!count($crawler = $this
->getCrawler()
->filterXPath($xpath))) {
throw new DriverException(sprintf('There is no element matching XPath "%s"', $xpath));
}
return $crawler;
}
private function getCrawler() {
$crawler = $this->client
->getCrawler();
if (null === $crawler) {
throw new DriverException('Unable to access the response content before visiting a page');
}
return $crawler;
}
}