Total Complexity | 122 |
Total Lines | 953 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PantherDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PantherDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class PantherDriver extends CoreDriver |
||
40 | { |
||
41 | use PantherTestCaseTrait; |
||
42 | |||
43 | /** @var Client */ |
||
44 | private $client; |
||
45 | private $started = false; |
||
46 | private $removeScriptFromUrl = false; |
||
47 | private $removeHostFromUrl = false; |
||
48 | /** @var string */ |
||
49 | private $clientType; |
||
50 | /** @var array */ |
||
51 | private $clientOptions; |
||
52 | /** @var array */ |
||
53 | private $clientKernelOptions; |
||
54 | |||
55 | /** |
||
56 | * Initializes Panther driver. |
||
57 | * external_base_uri |
||
58 | * webServerDir PANTHER_WEB_SERVER_DIR |
||
59 | * port PANTHER_WEB_SERVER_PORT |
||
60 | * router PANTHER_WEB_SERVER_ROUTER |
||
61 | * protected static $defaultOptions = [ |
||
62 | * 'webServerDir' => __DIR__.'/../../../../public', // the Flex directory structure |
||
63 | * 'hostname' => '127.0.0.1', |
||
64 | * 'port' => 9080, |
||
65 | * 'router' => '', |
||
66 | * 'external_base_uri' => null, |
||
67 | * ]; |
||
68 | * |
||
69 | * @param string $clientType BrowserKit client instance |
||
70 | * @param array $options |
||
71 | * @param array $kernelOptions |
||
72 | */ |
||
73 | public function __construct( |
||
74 | string $clientType = 'panther', |
||
75 | array $options = [], |
||
76 | array $kernelOptions = [] |
||
77 | ) { |
||
78 | $this->clientType = $clientType; |
||
79 | $this->clientOptions = $options; |
||
80 | $this->clientKernelOptions = $kernelOptions; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns BrowserKit HTTP client instance. |
||
85 | * |
||
86 | * @return Client |
||
87 | */ |
||
88 | public function getClient() |
||
89 | { |
||
90 | return $this->client; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Tells driver to remove hostname from URL. |
||
95 | * |
||
96 | * @param Boolean $remove |
||
97 | * |
||
98 | * @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead. |
||
99 | */ |
||
100 | public function setRemoveHostFromUrl($remove = true) |
||
101 | { |
||
102 | @trigger_error( |
||
103 | 'setRemoveHostFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.', |
||
104 | E_USER_DEPRECATED |
||
105 | ); |
||
106 | $this->removeHostFromUrl = (bool)$remove; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Tells driver to remove script name from URL. |
||
111 | * |
||
112 | * @param Boolean $remove |
||
113 | * |
||
114 | * @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead. |
||
115 | */ |
||
116 | public function setRemoveScriptFromUrl($remove = true) |
||
117 | { |
||
118 | @trigger_error( |
||
119 | 'setRemoveScriptFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.', |
||
120 | E_USER_DEPRECATED |
||
121 | ); |
||
122 | $this->removeScriptFromUrl = (bool)$remove; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function start() |
||
129 | { |
||
130 | if ($this->clientType === 'panther') { |
||
131 | $this->client = self::createPantherClient($this->clientOptions, $this->clientKernelOptions); |
||
132 | } elseif ($this->clientType === 'goutte') { |
||
133 | $this->client = self::createGoutteClient($this->clientOptions, $this->clientKernelOptions); |
||
134 | } else { |
||
135 | throw new \InvalidArgumentException('$clientType has to be "panther" or "goutte".'); |
||
136 | } |
||
137 | |||
138 | $this->started = true; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function isStarted() |
||
145 | { |
||
146 | return $this->started; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function stop() |
||
153 | { |
||
154 | $this->client->quit(); |
||
155 | self::stopWebServer(); |
||
156 | $this->started = false; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function reset() |
||
163 | { |
||
164 | // experimental |
||
165 | // $useSpeedUp = false; |
||
166 | $useSpeedUp = true; |
||
167 | if ($useSpeedUp) { |
||
|
|||
168 | $this->client->getWebDriver()->manage()->deleteAllCookies(); |
||
169 | $history = $this->client->getHistory(); |
||
170 | if ($history) { |
||
171 | $history->clear(); |
||
172 | } |
||
173 | // not sure if we should also close all windows |
||
174 | // $lastWindowHandle = \end($this->client->getWindowHandles()); |
||
175 | // if ($lastWindowHandle) { |
||
176 | // $this->client->switchTo()->window($lastWindowHandle); |
||
177 | // } |
||
178 | // $this->client->getWebDriver()->navigate()->refresh(); |
||
179 | // $this->client->refreshCrawler(); |
||
180 | // if (\count($this->client->getWindowHandles()) > 1) { |
||
181 | // $this->client->getWebDriver()->close(); |
||
182 | // } |
||
183 | } else { |
||
184 | // Restarting the client resets the cookies and the history |
||
185 | $this->client->restart(); |
||
186 | } |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function visit($url) |
||
194 | { |
||
195 | $this->client->get($this->prepareUrl($url)); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function getCurrentUrl() |
||
202 | { |
||
203 | return $this->client->getCurrentURL(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function reload() |
||
210 | { |
||
211 | $this->client->reload(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | public function forward() |
||
218 | { |
||
219 | $this->client->forward(); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function back() |
||
226 | { |
||
227 | $this->client->back(); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function switchToWindow($name = null) |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function switchToIFrame($name = null) |
||
243 | { |
||
244 | if (null === $name) { |
||
245 | $this->client->switchTo()->defaultContent(); |
||
246 | } else { |
||
247 | $this->client->switchTo()->frame($name); |
||
248 | } |
||
249 | $this->client->refreshCrawler(); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function setCookie($name, $value = null) |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Deletes a cookie by name. |
||
270 | * |
||
271 | * @param string $name Cookie name. |
||
272 | */ |
||
273 | private function deleteCookie($name) |
||
274 | { |
||
275 | $path = $this->getCookiePath(); |
||
276 | $jar = $this->client->getCookieJar(); |
||
277 | |||
278 | do { |
||
279 | if (null !== $jar->get($name, $path)) { |
||
280 | $jar->expire($name, $path); |
||
281 | } |
||
282 | |||
283 | $path = preg_replace('/.$/', '', $path); |
||
284 | } while ($path); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Returns current cookie path. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | private function getCookiePath() |
||
293 | { |
||
294 | $path = dirname(parse_url($this->getCurrentUrl(), PHP_URL_PATH)); |
||
295 | |||
296 | if ('\\' === DIRECTORY_SEPARATOR) { |
||
297 | $path = str_replace('\\', '/', $path); |
||
298 | } |
||
299 | |||
300 | return $path; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function getCookie($name) |
||
307 | { |
||
308 | $cookies = $this->client->getCookieJar()->all(); |
||
309 | |||
310 | foreach ($cookies as $cookie) { |
||
311 | if ($cookie->getName() === $name) { |
||
312 | return \urldecode($cookie->getValue()); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return null; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function getContent() |
||
323 | { |
||
324 | return $this->client->getWebDriver()->getPageSource(); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function getScreenshot($saveAs = null): string |
||
331 | { |
||
332 | return $this->client->takeScreenshot($saveAs); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function getWindowNames() |
||
339 | { |
||
340 | return $this->client->getWindowHandles(); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function getWindowName() |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function isVisible($xpath) |
||
355 | { |
||
356 | return $this->getCrawlerElement($this->getFilteredCrawler($xpath))->isDisplayed(); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc}. |
||
361 | */ |
||
362 | public function mouseOver($xpath) |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | */ |
||
370 | public function focus($xpath) |
||
371 | { |
||
372 | $jsNode = $this->getJsNode($xpath); |
||
373 | $script = \sprintf('%s.focus()', $jsNode); |
||
374 | $this->executeScript($script); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | public function blur($xpath) |
||
381 | { |
||
382 | $jsNode = $this->getJsNode($xpath); |
||
383 | $script = \sprintf('%s.blur();', $jsNode); |
||
384 | // ensure element had active state; just for passing EventsTest::testBlur |
||
385 | if ($this->evaluateScript(\sprintf('document.activeElement !== %s', $jsNode))) { |
||
386 | $script = \sprintf('%s.focus();%s', $jsNode, $script); |
||
387 | } |
||
388 | $this->executeScript($script); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | public function keyPress($xpath, $char, $modifier = null) |
||
395 | { |
||
396 | $webDriverActions = $this->getWebDriverActions(); |
||
397 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
398 | $key = $this->geWebDriverKeyValue($char, $modifier); |
||
399 | $webDriverActions->sendKeys($element, $key.WebDriverKeys::NULL)->perform(); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function keyDown($xpath, $char, $modifier = null) |
||
406 | { |
||
407 | $webDriverActions = $this->getWebDriverActions(); |
||
408 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
409 | $key = $this->geWebDriverKeyValue($char, $modifier); |
||
410 | $webDriverActions->keyDown($element, $key.WebDriverKeys::NULL)->perform(); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * {@inheritdoc} |
||
415 | */ |
||
416 | public function keyUp($xpath, $char, $modifier = null) |
||
417 | { |
||
418 | $webDriverActions = $this->getWebDriverActions(); |
||
419 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
420 | $key = $this->geWebDriverKeyValue($char, $modifier); |
||
421 | $webDriverActions->keyUp($element, $key)->perform(); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function isSelected($xpath) |
||
428 | { |
||
429 | return $this->getCrawlerElement($this->getFilteredCrawler($xpath))->isSelected(); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function findElementXpaths($xpath) |
||
436 | { |
||
437 | $nodes = $this->getCrawler()->filterXPath($xpath); |
||
438 | |||
439 | $elements = array(); |
||
440 | foreach ($nodes as $i => $node) { |
||
441 | $elements[] = sprintf('(%s)[%d]', $xpath, $i + 1); |
||
442 | } |
||
443 | |||
444 | return $elements; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | */ |
||
450 | public function getTagName($xpath) |
||
451 | { |
||
452 | return $this->getCrawlerElement($this->getFilteredCrawler($xpath))->getTagName(); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * {@inheritdoc} |
||
457 | */ |
||
458 | public function getText($xpath) |
||
459 | { |
||
460 | $text = $this->getFilteredCrawler($xpath)->text(); |
||
461 | $text = str_replace("\n", ' ', $text); |
||
462 | $text = preg_replace('/ {2,}/', ' ', $text); |
||
463 | |||
464 | return trim($text); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * {@inheritdoc} |
||
469 | */ |
||
470 | public function getHtml($xpath) |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function getOuterHtml($xpath) |
||
480 | { |
||
481 | $crawler = $this->getFilteredCrawler($xpath); |
||
482 | |||
483 | return $crawler->html(); |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function getAttribute($xpath, $name) |
||
490 | { |
||
491 | $crawler = $this->getFilteredCrawler($xpath); |
||
492 | |||
493 | $attribute = $this->getCrawlerElement($crawler)->getAttribute($name); |
||
494 | |||
495 | // let's get hacky |
||
496 | if ('' === $attribute) { |
||
497 | $html = \strtolower($crawler->html()); |
||
498 | $name = \strtolower($name).'='; |
||
499 | if (0 === \substr_count($html, $name)) { |
||
500 | $attribute = null; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | return $attribute; |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * {@inheritdoc} |
||
509 | */ |
||
510 | public function getValue($xpath) |
||
511 | { |
||
512 | try { |
||
513 | $formField = $this->getFormField($xpath); |
||
514 | $value = $formField->getValue(); |
||
515 | if ('' === $value && $formField instanceof ChoiceFormField) { |
||
516 | $value = null; |
||
517 | } |
||
518 | } catch (DriverException $e) { |
||
519 | // e.g. element is an option |
||
520 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
521 | $value = $element->getAttribute('value'); |
||
522 | } |
||
523 | |||
524 | return $value; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * {@inheritdoc} |
||
529 | */ |
||
530 | public function setValue($xpath, $value) |
||
531 | { |
||
532 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
533 | $jsNode = $this->getJsNode($xpath); |
||
534 | |||
535 | // add workaround for now in case value is not a canonical path |
||
536 | // also see: https://github.com/minkphp/driver-testsuite/pull/32 |
||
537 | if ('input' === $element->getTagName() && 'file' === $element->getAttribute('type')) { |
||
538 | $realpathValue = \realpath($value); |
||
539 | $value = \is_string($realpathValue) ? $realpathValue : $value; |
||
540 | } |
||
541 | |||
542 | if ('input' === $element->getTagName() && \in_array($element->getAttribute('type'), ['date', 'color'])) { |
||
543 | $this->executeScript(\sprintf('%s.value = \'%s\'', $jsNode, $value)); |
||
544 | } else { |
||
545 | try { |
||
546 | $formField = $this->getFormField($xpath); |
||
547 | $formField->setValue($value); |
||
548 | } catch (DriverException $e) { |
||
549 | // e.g. element is on option |
||
550 | $element->sendKeys($value); |
||
551 | } |
||
552 | } |
||
553 | |||
554 | // Remove the focus from the element if the field still has focus in |
||
555 | // order to trigger the change event. By doing this instead of simply |
||
556 | // triggering the change event for the given xpath we ensure that the |
||
557 | // change event will not be triggered twice for the same element if it |
||
558 | // has lost focus in the meanwhile. If the element has lost focus |
||
559 | // already then there is nothing to do as this will already have caused |
||
560 | // the triggering of the change event for that element. |
||
561 | if ($this->evaluateScript(\sprintf('document.activeElement === %s', $jsNode))) { |
||
562 | $this->executeScript('document.activeElement.blur();'); |
||
563 | } |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * {@inheritdoc} |
||
568 | */ |
||
569 | public function check($xpath) |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * {@inheritdoc} |
||
576 | */ |
||
577 | public function uncheck($xpath) |
||
578 | { |
||
579 | $this->getChoiceFormField($xpath)->untick(); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * {@inheritdoc} |
||
584 | */ |
||
585 | public function selectOption($xpath, $value, $multiple = false) |
||
586 | { |
||
587 | $field = $this->getFormField($xpath); |
||
588 | |||
589 | if (!$field instanceof ChoiceFormField) { |
||
590 | throw new DriverException( |
||
591 | sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath) |
||
592 | ); |
||
593 | } |
||
594 | |||
595 | $field->select($value); |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * {@inheritdoc} |
||
600 | */ |
||
601 | public function click($xpath) |
||
602 | { |
||
603 | $this->client->getMouse()->click($this->toCoordinates($xpath)); |
||
604 | $this->client->refreshCrawler(); |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * {@inheritdoc} |
||
609 | */ |
||
610 | public function doubleClick($xpath) |
||
611 | { |
||
612 | $this->client->getMouse()->doubleClick($this->toCoordinates($xpath)); |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * {@inheritdoc} |
||
617 | */ |
||
618 | public function rightClick($xpath) |
||
619 | { |
||
620 | $this->client->getMouse()->contextClick($this->toCoordinates($xpath)); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * {@inheritdoc} |
||
625 | */ |
||
626 | public function isChecked($xpath) |
||
627 | { |
||
628 | return $this->getChoiceFormField($xpath)->hasValue(); |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * {@inheritdoc} |
||
633 | */ |
||
634 | public function attachFile($xpath, $path) |
||
635 | { |
||
636 | $field = $this->getFormField($xpath); |
||
637 | |||
638 | if (!$field instanceof FileFormField) { |
||
639 | throw new DriverException( |
||
640 | sprintf('Impossible to attach a file on the element with XPath "%s" as it is not a file input', $xpath) |
||
641 | ); |
||
642 | } |
||
643 | |||
644 | $field->upload($path); |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * {@inheritdoc} |
||
649 | */ |
||
650 | public function dragTo($sourceXpath, $destinationXpath) |
||
651 | { |
||
652 | $webDriverActions = $this->getWebDriverActions(); |
||
653 | $source = $this->getCrawlerElement($this->getFilteredCrawler($sourceXpath)); |
||
654 | $target = $this->getCrawlerElement($this->getFilteredCrawler($destinationXpath)); |
||
655 | $webDriverActions->dragAndDrop($source, $target)->perform(); |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * {@inheritdoc} |
||
660 | */ |
||
661 | public function executeScript($script) |
||
662 | { |
||
663 | if (\preg_match('/^function[\s\(]/', $script)) { |
||
664 | $script = \preg_replace('/;$/', '', $script); |
||
665 | $script = '(' . $script . ')'; |
||
666 | } |
||
667 | |||
668 | return $this->client->executeScript($script); |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * {@inheritdoc} |
||
673 | */ |
||
674 | public function evaluateScript($script) |
||
675 | { |
||
676 | if (0 !== \strpos(\trim($script), 'return ')) { |
||
677 | $script = 'return ' . $script; |
||
678 | } |
||
679 | |||
680 | return $this->client->executeScript($script); |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * {@inheritdoc} |
||
685 | */ |
||
686 | public function wait($timeout, $condition) |
||
687 | { |
||
688 | $script = "return $condition;"; |
||
689 | $start = microtime(true); |
||
690 | $end = $start + $timeout / 1000.0; |
||
691 | |||
692 | do { |
||
693 | $result = $this->evaluateScript($script); |
||
694 | \usleep(100000); |
||
695 | } while (\microtime(true) < $end && !$result); |
||
696 | |||
697 | return (bool) $result; |
||
698 | } |
||
699 | |||
700 | /** |
||
701 | * {@inheritdoc} |
||
702 | */ |
||
703 | public function resizeWindow($width, $height, $name = null) |
||
704 | { |
||
705 | $size = new WebDriverDimension($width, $height); |
||
706 | $this->client->getWebDriver()->manage()->window()->setSize($size); |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * {@inheritdoc} |
||
711 | */ |
||
712 | public function maximizeWindow($name = null) |
||
713 | { |
||
714 | $width = $this->evaluateScript('screen.width'); |
||
715 | $height = $this->evaluateScript('screen.height'); |
||
716 | $this->resizeWindow($width, $height, $name); |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * {@inheritdoc} |
||
721 | */ |
||
722 | public function submitForm($xpath) |
||
723 | { |
||
724 | $crawler = $this->getFilteredCrawler($xpath); |
||
725 | |||
726 | $this->client->submit($crawler->form()); |
||
727 | $this->client->refreshCrawler(); |
||
728 | } |
||
729 | |||
730 | /** |
||
731 | * @return Response |
||
732 | * |
||
733 | * @throws DriverException If there is not response yet |
||
734 | */ |
||
735 | protected function getResponse() |
||
736 | { |
||
737 | $response = $this->client->getInternalResponse(); |
||
738 | |||
739 | if (null === $response) { |
||
740 | throw new DriverException('Unable to access the response before visiting a page'); |
||
741 | } |
||
742 | |||
743 | return $response; |
||
744 | } |
||
745 | |||
746 | /** |
||
747 | * Prepares URL for visiting. |
||
748 | * Removes "*.php/" from urls and then passes it to BrowserKitDriver::visit(). |
||
749 | * |
||
750 | * @param string $url |
||
751 | * |
||
752 | * @return string |
||
753 | */ |
||
754 | protected function prepareUrl($url) |
||
755 | { |
||
756 | $replacement = ($this->removeHostFromUrl ? '' : '$1').($this->removeScriptFromUrl ? '' : '$2'); |
||
757 | |||
758 | return preg_replace('#(https?\://[^/]+)(/[^/\.]+\.php)?#', $replacement, $url); |
||
759 | } |
||
760 | |||
761 | /** |
||
762 | * Returns form field from XPath query. |
||
763 | * |
||
764 | * @param string $xpath |
||
765 | * |
||
766 | * @return FormField |
||
767 | * |
||
768 | * @throws DriverException |
||
769 | */ |
||
770 | private function getFormField($xpath) |
||
771 | { |
||
772 | try { |
||
773 | $formField = $this->getChoiceFormField($xpath); |
||
774 | } catch (DriverException $e) { |
||
775 | $formField = null; |
||
776 | } |
||
777 | if (!$formField) { |
||
778 | try { |
||
779 | $formField = $this->getInputFormField($xpath); |
||
780 | } catch (DriverException $e) { |
||
781 | $formField = null; |
||
782 | } |
||
783 | } |
||
784 | if (!$formField) { |
||
785 | try { |
||
786 | $formField = $this->getFileFormField($xpath); |
||
787 | } catch (DriverException $e) { |
||
788 | $formField = null; |
||
789 | } |
||
790 | } |
||
791 | if (!$formField) { |
||
792 | $formField = $this->getTextareaFormField($xpath); |
||
793 | } |
||
794 | |||
795 | return $formField; |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * Returns the checkbox field from xpath query, ensuring it is valid. |
||
800 | * |
||
801 | * @param string $xpath |
||
802 | * |
||
803 | * @return ChoiceFormField |
||
804 | * |
||
805 | * @throws DriverException when the field is not a checkbox |
||
806 | */ |
||
807 | private function getChoiceFormField($xpath) |
||
808 | { |
||
809 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
810 | try { |
||
811 | $choiceFormField = new ChoiceFormField($element); |
||
812 | } catch (\LogicException $e) { |
||
813 | throw new DriverException( |
||
814 | sprintf( |
||
815 | 'Impossible to get the element with XPath "%s" as it is not a choice form field. %s', |
||
816 | $xpath, |
||
817 | $e->getMessage() |
||
818 | ) |
||
819 | ); |
||
820 | } |
||
821 | |||
822 | return $choiceFormField; |
||
823 | } |
||
824 | |||
825 | /** |
||
826 | * Returns the input field from xpath query, ensuring it is valid. |
||
827 | * |
||
828 | * @param string $xpath |
||
829 | * |
||
830 | * @return InputFormField |
||
831 | * |
||
832 | * @throws DriverException when the field is not a checkbox |
||
833 | */ |
||
834 | private function getInputFormField($xpath) |
||
835 | { |
||
836 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
837 | try { |
||
838 | $inputFormField = new InputFormField($element); |
||
839 | } catch (\LogicException $e) { |
||
840 | throw new DriverException(sprintf('Impossible to check the element with XPath "%s" as it is not an input form field.', $xpath)); |
||
841 | } |
||
842 | |||
843 | return $inputFormField; |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * Returns the input field from xpath query, ensuring it is valid. |
||
848 | * |
||
849 | * @param string $xpath |
||
850 | * |
||
851 | * @return FileFormField |
||
852 | * |
||
853 | * @throws DriverException when the field is not a checkbox |
||
854 | */ |
||
855 | private function getFileFormField($xpath) |
||
856 | { |
||
857 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
858 | try { |
||
859 | $fileFormField = new FileFormField($element); |
||
860 | } catch (\LogicException $e) { |
||
861 | throw new DriverException(sprintf('Impossible to check the element with XPath "%s" as it is not a file form field.', $xpath)); |
||
862 | } |
||
863 | |||
864 | return $fileFormField; |
||
865 | } |
||
866 | |||
867 | /** |
||
868 | * Returns the textarea field from xpath query, ensuring it is valid. |
||
869 | * |
||
870 | * @param string $xpath |
||
871 | * |
||
872 | * @return TextareaFormField |
||
873 | * |
||
874 | * @throws DriverException when the field is not a checkbox |
||
875 | */ |
||
876 | private function getTextareaFormField($xpath) |
||
877 | { |
||
878 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
879 | try { |
||
880 | $textareaFormField = new TextareaFormField($element); |
||
881 | } catch (\LogicException $e) { |
||
882 | throw new DriverException(sprintf('Impossible to check the element with XPath "%s" as it is not a textarea.', $xpath)); |
||
883 | } |
||
884 | |||
885 | return $textareaFormField; |
||
886 | } |
||
887 | |||
888 | /** |
||
889 | * Returns WebDriverElement from crawler instance. |
||
890 | * |
||
891 | * @param Crawler $crawler |
||
892 | * |
||
893 | * @return WebDriverElement |
||
894 | * |
||
895 | * @throws DriverException when the node does not exist |
||
896 | */ |
||
897 | private function getCrawlerElement(Crawler $crawler): WebDriverElement |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * Returns a crawler filtered for the given XPath, requiring at least 1 result. |
||
910 | * |
||
911 | * @param string $xpath |
||
912 | * |
||
913 | * @return Crawler |
||
914 | * |
||
915 | * @throws DriverException when no matching elements are found |
||
916 | */ |
||
917 | private function getFilteredCrawler($xpath): Crawler |
||
918 | { |
||
919 | if (!count($crawler = $this->getCrawler()->filterXPath($xpath))) { |
||
920 | throw new DriverException(sprintf('There is no element matching XPath "%s"', $xpath)); |
||
921 | } |
||
922 | |||
923 | return $crawler; |
||
924 | } |
||
925 | |||
926 | /** |
||
927 | * Returns crawler instance (got from client). |
||
928 | * |
||
929 | * @return Crawler |
||
930 | * |
||
931 | * @throws DriverException |
||
932 | */ |
||
933 | private function getCrawler(): Crawler |
||
934 | { |
||
935 | $crawler = $this->client->getCrawler(); |
||
936 | |||
937 | if (null === $crawler) { |
||
938 | throw new DriverException('Unable to access the response content before visiting a page'); |
||
939 | } |
||
940 | |||
941 | return $crawler; |
||
942 | } |
||
943 | |||
944 | private function getJsNode(string $xpath): string |
||
947 | } |
||
948 | |||
949 | private function toCoordinates(string $xpath): WebDriverCoordinates |
||
950 | { |
||
951 | $element = $this->getCrawlerElement($this->getFilteredCrawler($xpath)); |
||
952 | |||
953 | if (!$element instanceof WebDriverLocatable) { |
||
954 | throw new \RuntimeException( |
||
955 | sprintf('The element of "%s" xpath selector does not implement "%s".', $xpath, WebDriverLocatable::class) |
||
956 | ); |
||
957 | } |
||
958 | |||
959 | return $element->getCoordinates(); |
||
960 | } |
||
961 | |||
962 | private function getWebDriverActions(): WebDriverActions |
||
963 | { |
||
964 | $webDriver = $this->client->getWebDriver(); |
||
965 | if (!$webDriver instanceof WebDriverHasInputDevices) { |
||
966 | throw new UnsupportedDriverActionException('Mouse manipulations are not supported by %s', $this); |
||
967 | } |
||
968 | $webDriverActions = new WebDriverActions($webDriver); |
||
969 | |||
970 | return $webDriverActions; |
||
971 | } |
||
972 | |||
973 | private function geWebDriverKeyValue($char, $modifier = null) |
||
992 | } |
||
993 | } |
||
994 | } |
||
995 |