Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WebDriver 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 WebDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
282 | class WebDriver extends CodeceptionModule implements |
||
283 | WebInterface, |
||
284 | RemoteInterface, |
||
285 | MultiSessionInterface, |
||
286 | SessionSnapshot, |
||
287 | ScreenshotSaver, |
||
288 | PageSourceSaver, |
||
289 | ElementLocator, |
||
290 | ConflictsWithModule, |
||
291 | RequiresPackage |
||
292 | { |
||
293 | protected $requiredFields = ['browser', 'url']; |
||
294 | protected $config = [ |
||
295 | 'protocol' => 'http', |
||
296 | 'host' => '127.0.0.1', |
||
297 | 'port' => '4444', |
||
298 | 'path' => '/wd/hub', |
||
299 | 'start' => true, |
||
300 | 'restart' => false, |
||
301 | 'wait' => 0, |
||
302 | 'clear_cookies' => true, |
||
303 | 'window_size' => false, |
||
304 | 'capabilities' => [], |
||
305 | 'connection_timeout' => null, |
||
306 | 'request_timeout' => null, |
||
307 | 'pageload_timeout' => null, |
||
308 | 'http_proxy' => null, |
||
309 | 'http_proxy_port' => null, |
||
310 | 'ssl_proxy' => null, |
||
311 | 'ssl_proxy_port' => null, |
||
312 | 'debug_log_entries' => 15, |
||
313 | 'log_js_errors' => false |
||
314 | ]; |
||
315 | |||
316 | protected $wdHost; |
||
317 | protected $capabilities; |
||
318 | protected $connectionTimeoutInMs; |
||
319 | protected $requestTimeoutInMs; |
||
320 | protected $test; |
||
321 | protected $sessions = []; |
||
322 | protected $sessionSnapshots = []; |
||
323 | protected $httpProxy; |
||
324 | protected $httpProxyPort; |
||
325 | protected $sslProxy; |
||
326 | protected $sslProxyPort; |
||
327 | |||
328 | /** |
||
329 | * @var RemoteWebDriver |
||
330 | */ |
||
331 | public $webDriver; |
||
332 | |||
333 | /** |
||
334 | * @var RemoteWebElement |
||
335 | */ |
||
336 | protected $baseElement; |
||
337 | |||
338 | public function _requires() |
||
342 | |||
343 | public function _initialize() |
||
355 | |||
356 | /** |
||
357 | * Change capabilities of WebDriver. Should be executed before starting a new browser session. |
||
358 | * This method expects a function to be passed which returns array or [WebDriver Desired Capabilities](https://github.com/facebook/php-webdriver/blob/community/lib/Remote/DesiredCapabilities.php) object. |
||
359 | * Additional [Chrome options](https://github.com/facebook/php-webdriver/wiki/ChromeOptions) (like adding extensions) can be passed as well. |
||
360 | * |
||
361 | * ```php |
||
362 | * <?php // in helper |
||
363 | * public function _before(TestInterface $test) |
||
364 | * { |
||
365 | * $this->getModule('WebDriver')->_capabilities(function($currentCapabilities) { |
||
366 | * // or new \Facebook\WebDriver\Remote\DesiredCapabilities(); |
||
367 | * return \Facebook\WebDriver\Remote\DesiredCapabilities::firefox(); |
||
368 | * }); |
||
369 | * } |
||
370 | * ``` |
||
371 | * |
||
372 | * to make this work load `\Helper\Acceptance` before `WebDriver` in `acceptance.suite.yml`: |
||
373 | * |
||
374 | * ```yaml |
||
375 | * modules: |
||
376 | * enabled: |
||
377 | * - \Helper\Acceptance |
||
378 | * - WebDriver |
||
379 | * ``` |
||
380 | * |
||
381 | * For instance, [**BrowserStack** cloud service](https://www.browserstack.com/automate/capabilities) may require a test name to be set in capabilities. |
||
382 | * This is how it can be done via `_capabilities` method from `Helper\Acceptance`: |
||
383 | * |
||
384 | * ```php |
||
385 | * <?php // inside Helper\Acceptance |
||
386 | * public function _before(TestInterface $test) |
||
387 | * { |
||
388 | * $name = $test->getMetadata()->getName(); |
||
389 | * $this->getModule('WebDriver')->_capabilities(function($currentCapabilities) use ($name) { |
||
390 | * $currentCapabilities['name'] = $name; |
||
391 | * return $currentCapabilities; |
||
392 | * }); |
||
393 | * } |
||
394 | * ``` |
||
395 | * In this case, please ensure that `\Helper\Acceptance` is loaded before WebDriver so new capabilities could be applied. |
||
396 | * |
||
397 | * @api |
||
398 | * @param \Closure $capabilityFunction |
||
399 | */ |
||
400 | public function _capabilities(\Closure $capabilityFunction) |
||
404 | |||
405 | public function _conflicts() |
||
409 | |||
410 | public function _before(TestInterface $test) |
||
430 | |||
431 | /** |
||
432 | * Restarts a web browser. |
||
433 | * Can be used with `_reconfigure` to open browser with different configuration |
||
434 | * |
||
435 | * ```php |
||
436 | * <?php |
||
437 | * // inside a Helper |
||
438 | * $this->getModule('WebDriver')->_restart(); // just restart |
||
439 | * $this->getModule('WebDriver')->_restart(['browser' => $browser]); // reconfigure + restart |
||
440 | * ``` |
||
441 | * |
||
442 | * @param array $config |
||
443 | * @api |
||
444 | */ |
||
445 | public function _restart($config = []) |
||
453 | |||
454 | protected function onReconfigure() |
||
458 | |||
459 | protected function loadFirefoxProfile() |
||
475 | |||
476 | protected function initialWindowSize() |
||
487 | |||
488 | public function _after(TestInterface $test) |
||
498 | |||
499 | public function _failed(TestInterface $test, $fail) |
||
510 | |||
511 | /** |
||
512 | * Print out latest Selenium Logs in debug mode |
||
513 | * |
||
514 | * @param TestInterface $test |
||
515 | */ |
||
516 | public function debugWebDriverLogs(TestInterface $test = null) |
||
547 | |||
548 | /** |
||
549 | * Turns an array of log entries into a human-readable string. |
||
550 | * Each log entry is an array with the keys "timestamp", "level", and "message". |
||
551 | * See https://code.google.com/p/selenium/wiki/JsonWireProtocol#Log_Entry_JSON_Object |
||
552 | * |
||
553 | * @param array $logEntries |
||
554 | * @return string |
||
555 | */ |
||
556 | protected function formatLogEntries(array $logEntries) |
||
569 | |||
570 | /** |
||
571 | * Logs JavaScript errors as comments. |
||
572 | * |
||
573 | * @param ScenarioDriven $test |
||
574 | * @param array $browserLogEntries |
||
575 | */ |
||
576 | protected function logJSErrors(ScenarioDriven $test, array $browserLogEntries) |
||
591 | |||
592 | /** |
||
593 | * Determines if the log entry is an error. |
||
594 | * The decision is made depending on browser and log-level. |
||
595 | * |
||
596 | * @param string $logEntryLevel |
||
597 | * @param string $message |
||
598 | * @return bool |
||
599 | */ |
||
600 | protected function isJSError($logEntryLevel, $message) |
||
609 | |||
610 | public function _afterSuite() |
||
615 | |||
616 | protected function stopAllSessions() |
||
624 | |||
625 | View Code Duplication | public function amOnSubdomain($subdomain) |
|
632 | |||
633 | /** |
||
634 | * Returns URL of a host. |
||
635 | * |
||
636 | * @api |
||
637 | * @return mixed |
||
638 | * @throws ModuleConfigException |
||
639 | */ |
||
640 | public function _getUrl() |
||
650 | |||
651 | protected function getProxy() |
||
672 | |||
673 | /** |
||
674 | * Uri of currently opened page. |
||
675 | * @return string |
||
676 | * @api |
||
677 | * @throws ModuleException |
||
678 | */ |
||
679 | public function _getCurrentUri() |
||
687 | |||
688 | View Code Duplication | public function _saveScreenshot($filename) |
|
700 | |||
701 | public function _findElements($locator) |
||
705 | |||
706 | /** |
||
707 | * Saves HTML source of a page to a file |
||
708 | * @param $filename |
||
709 | */ |
||
710 | View Code Duplication | public function _savePageSource($filename) |
|
722 | |||
723 | /** |
||
724 | * Takes a screenshot of the current window and saves it to `tests/_output/debug`. |
||
725 | * |
||
726 | * ``` php |
||
727 | * <?php |
||
728 | * $I->amOnPage('/user/edit'); |
||
729 | * $I->makeScreenshot('edit_page'); |
||
730 | * // saved to: tests/_output/debug/edit_page.png |
||
731 | * $I->makeScreenshot(); |
||
732 | * // saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.png |
||
733 | * ``` |
||
734 | * |
||
735 | * @param $name |
||
736 | */ |
||
737 | public function makeScreenshot($name = null) |
||
750 | |||
751 | /** |
||
752 | * Resize the current window. |
||
753 | * |
||
754 | * ``` php |
||
755 | * <?php |
||
756 | * $I->resizeWindow(800, 600); |
||
757 | * |
||
758 | * ``` |
||
759 | * |
||
760 | * @param int $width |
||
761 | * @param int $height |
||
762 | */ |
||
763 | public function resizeWindow($width, $height) |
||
767 | |||
768 | View Code Duplication | public function seeCookie($cookie, array $params = []) |
|
780 | |||
781 | View Code Duplication | public function dontSeeCookie($cookie, array $params = []) |
|
793 | |||
794 | public function setCookie($cookie, $value, array $params = []) |
||
810 | |||
811 | public function resetCookie($cookie, array $params = []) |
||
816 | |||
817 | public function grabCookie($cookie, array $params = []) |
||
827 | |||
828 | /** |
||
829 | * Grabs current page source code. |
||
830 | * |
||
831 | * @throws ModuleException if no page was opened. |
||
832 | * |
||
833 | * @return string Current page source code. |
||
834 | */ |
||
835 | public function grabPageSource() |
||
842 | |||
843 | protected function filterCookies($cookies, $params = []) |
||
858 | |||
859 | public function amOnUrl($url) |
||
866 | |||
867 | public function amOnPage($page) |
||
873 | |||
874 | public function see($text, $selector = null) |
||
884 | |||
885 | public function dontSee($text, $selector = null) |
||
893 | |||
894 | public function seeInSource($raw) |
||
898 | |||
899 | public function dontSeeInSource($raw) |
||
903 | |||
904 | /** |
||
905 | * Checks that the page source contains the given string. |
||
906 | * |
||
907 | * ```php |
||
908 | * <?php |
||
909 | * $I->seeInPageSource('<link rel="apple-touch-icon"'); |
||
910 | * ``` |
||
911 | * |
||
912 | * @param $text |
||
913 | */ |
||
914 | public function seeInPageSource($text) |
||
922 | |||
923 | /** |
||
924 | * Checks that the page source doesn't contain the given string. |
||
925 | * |
||
926 | * @param $text |
||
927 | */ |
||
928 | public function dontSeeInPageSource($text) |
||
936 | |||
937 | public function click($link, $context = null) |
||
957 | |||
958 | /** |
||
959 | * Locates a clickable element. |
||
960 | * |
||
961 | * Use it in Helpers or GroupObject or Extension classes: |
||
962 | * |
||
963 | * ```php |
||
964 | * <?php |
||
965 | * $module = $this->getModule('WebDriver'); |
||
966 | * $page = $module->webDriver; |
||
967 | * |
||
968 | * // search a link or button on a page |
||
969 | * $el = $module->_findClickable($page, 'Click Me'); |
||
970 | * |
||
971 | * // search a link or button within an element |
||
972 | * $topBar = $module->_findElements('.top-bar')[0]; |
||
973 | * $el = $module->_findClickable($topBar, 'Click Me'); |
||
974 | * |
||
975 | * ``` |
||
976 | * @api |
||
977 | * @param $page WebDriver instance or an element to search within |
||
978 | * @param $link a link text or locator to click |
||
979 | * @return WebDriverElement |
||
980 | */ |
||
981 | public function _findClickable($page, $link) |
||
1024 | |||
1025 | /** |
||
1026 | * @param $selector |
||
1027 | * @return WebDriverElement[] |
||
1028 | * @throws \Codeception\Exception\ElementNotFound |
||
1029 | */ |
||
1030 | protected function findFields($selector) |
||
1072 | |||
1073 | /** |
||
1074 | * @param $selector |
||
1075 | * @return WebDriverElement |
||
1076 | * @throws \Codeception\Exception\ElementNotFound |
||
1077 | */ |
||
1078 | protected function findField($selector) |
||
1083 | |||
1084 | public function seeLink($text, $url = null) |
||
1099 | |||
1100 | public function dontSeeLink($text, $url = null) |
||
1111 | |||
1112 | /** |
||
1113 | * @param string $url |
||
1114 | * @param $nodes |
||
1115 | * @return array |
||
1116 | */ |
||
1117 | private function filterNodesByHref($url, $nodes) |
||
1132 | |||
1133 | public function seeInCurrentUrl($uri) |
||
1137 | |||
1138 | public function seeCurrentUrlEquals($uri) |
||
1142 | |||
1143 | public function seeCurrentUrlMatches($uri) |
||
1147 | |||
1148 | public function dontSeeInCurrentUrl($uri) |
||
1152 | |||
1153 | public function dontSeeCurrentUrlEquals($uri) |
||
1157 | |||
1158 | public function dontSeeCurrentUrlMatches($uri) |
||
1162 | |||
1163 | View Code Duplication | public function grabFromCurrentUrl($uri = null) |
|
1178 | |||
1179 | public function seeCheckboxIsChecked($checkbox) |
||
1183 | |||
1184 | public function dontSeeCheckboxIsChecked($checkbox) |
||
1188 | |||
1189 | public function seeInField($field, $value) |
||
1194 | |||
1195 | public function dontSeeInField($field, $value) |
||
1200 | |||
1201 | public function seeInFormFields($formSelector, array $params) |
||
1205 | |||
1206 | public function dontSeeInFormFields($formSelector, array $params) |
||
1210 | |||
1211 | protected function proceedSeeInFormFields($formSelector, array $params, $assertNot) |
||
1241 | |||
1242 | /** |
||
1243 | * Map an array element passed to seeInFormFields to its corresponding WebDriver element, |
||
1244 | * recursing through array values if the field is not found. |
||
1245 | * |
||
1246 | * @param array $els The previously found elements. |
||
1247 | * @param RemoteWebElement $form The form in which to search for fields. |
||
1248 | * @param string $name The field's name. |
||
1249 | * @param mixed $values |
||
1250 | * @return void |
||
1251 | */ |
||
1252 | protected function pushFormField(&$els, $form, $name, $values) |
||
1266 | |||
1267 | /** |
||
1268 | * @param RemoteWebElement[] $elements |
||
1269 | * @param $value |
||
1270 | * @return array |
||
1271 | */ |
||
1272 | protected function proceedSeeInField(array $elements, $value) |
||
1325 | |||
1326 | public function selectOption($select, $option) |
||
1400 | |||
1401 | /** |
||
1402 | * Manually starts a new browser session. |
||
1403 | * |
||
1404 | * ```php |
||
1405 | * <?php |
||
1406 | * $this->getModule('WebDriver')->_initializeSession(); |
||
1407 | * ``` |
||
1408 | * |
||
1409 | * @api |
||
1410 | */ |
||
1411 | public function _initializeSession() |
||
1432 | |||
1433 | /** |
||
1434 | * Loads current RemoteWebDriver instance as a session |
||
1435 | * |
||
1436 | * @api |
||
1437 | * @param RemoteWebDriver $session |
||
1438 | */ |
||
1439 | public function _loadSession($session) |
||
1444 | |||
1445 | /** |
||
1446 | * Returns current WebDriver session for saving |
||
1447 | * |
||
1448 | * @api |
||
1449 | * @return RemoteWebDriver |
||
1450 | */ |
||
1451 | public function _backupSession() |
||
1455 | |||
1456 | /** |
||
1457 | * Manually closes current WebDriver session. |
||
1458 | * |
||
1459 | * ```php |
||
1460 | * <?php |
||
1461 | * $this->getModule('WebDriver')->_closeSession(); |
||
1462 | * |
||
1463 | * // close a specific session |
||
1464 | * $webDriver = $this->getModule('WebDriver')->webDriver; |
||
1465 | * $this->getModule('WebDriver')->_closeSession($webDriver); |
||
1466 | * ``` |
||
1467 | * |
||
1468 | * @api |
||
1469 | * @param $webDriver (optional) a specific webdriver session instance |
||
1470 | */ |
||
1471 | public function _closeSession($webDriver = null) |
||
1486 | |||
1487 | /** |
||
1488 | * Unselect an option in the given select box. |
||
1489 | * |
||
1490 | * @param $select |
||
1491 | * @param $option |
||
1492 | */ |
||
1493 | public function unselectOption($select, $option) |
||
1526 | |||
1527 | /** |
||
1528 | * @param $context |
||
1529 | * @param $radioOrCheckbox |
||
1530 | * @param bool $byValue |
||
1531 | * @return mixed|null |
||
1532 | */ |
||
1533 | protected function findCheckable($context, $radioOrCheckbox, $byValue = false) |
||
1586 | |||
1587 | protected function matchCheckables($selector) |
||
1595 | |||
1596 | View Code Duplication | public function checkOption($option) |
|
1607 | |||
1608 | View Code Duplication | public function uncheckOption($option) |
|
1619 | |||
1620 | public function fillField($field, $value) |
||
1626 | |||
1627 | public function attachFile($field, $filename) |
||
1647 | |||
1648 | /** |
||
1649 | * Grabs all visible text from the current page. |
||
1650 | * |
||
1651 | * @return string |
||
1652 | */ |
||
1653 | protected function getVisibleText() |
||
1664 | |||
1665 | public function grabTextFrom($cssOrXPathOrRegex) |
||
1676 | |||
1677 | public function grabAttributeFrom($cssOrXpath, $attribute) |
||
1682 | |||
1683 | public function grabValueFrom($field) |
||
1693 | |||
1694 | public function grabMultiple($cssOrXpath, $attribute = null) |
||
1707 | |||
1708 | |||
1709 | protected function filterByAttributes($els, array $attributes) |
||
1721 | |||
1722 | public function seeElement($selector, $attributes = []) |
||
1730 | |||
1731 | public function dontSeeElement($selector, $attributes = []) |
||
1737 | |||
1738 | /** |
||
1739 | * Checks that the given element exists on the page, even it is invisible. |
||
1740 | * |
||
1741 | * ``` php |
||
1742 | * <?php |
||
1743 | * $I->seeElementInDOM('//form/input[type=hidden]'); |
||
1744 | * ?> |
||
1745 | * ``` |
||
1746 | * |
||
1747 | * @param $selector |
||
1748 | * @param array $attributes |
||
1749 | */ |
||
1750 | public function seeElementInDOM($selector, $attributes = []) |
||
1758 | |||
1759 | |||
1760 | /** |
||
1761 | * Opposite of `seeElementInDOM`. |
||
1762 | * |
||
1763 | * @param $selector |
||
1764 | * @param array $attributes |
||
1765 | */ |
||
1766 | public function dontSeeElementInDOM($selector, $attributes = []) |
||
1772 | |||
1773 | View Code Duplication | public function seeNumberOfElements($selector, $expected) |
|
1790 | |||
1791 | View Code Duplication | public function seeNumberOfElementsInDOM($selector, $expected) |
|
1792 | { |
||
1793 | $counted = count($this->match($this->baseElement, $selector)); |
||
1794 | if (is_array($expected)) { |
||
1795 | list($floor, $ceil) = $expected; |
||
1796 | $this->assertTrue( |
||
1797 | $floor <= $counted && $ceil >= $counted, |
||
1798 | 'Number of elements counted differs from expected range' |
||
1799 | ); |
||
1800 | } else { |
||
1801 | $this->assertEquals( |
||
1802 | $expected, |
||
1803 | $counted, |
||
1804 | 'Number of elements counted differs from expected number' |
||
1805 | ); |
||
1806 | } |
||
1807 | } |
||
1808 | |||
1809 | View Code Duplication | public function seeOptionIsSelected($selector, $optionText) |
|
1830 | |||
1831 | View Code Duplication | public function dontSeeOptionIsSelected($selector, $optionText) |
|
1852 | |||
1853 | public function seeInTitle($title) |
||
1857 | |||
1858 | public function dontSeeInTitle($title) |
||
1862 | |||
1863 | /** |
||
1864 | * Accepts the active JavaScript native popup window, as created by `window.alert`|`window.confirm`|`window.prompt`. |
||
1865 | * Don't confuse popups with modal windows, |
||
1866 | * as created by [various libraries](http://jster.net/category/windows-modals-popups). |
||
1867 | */ |
||
1868 | public function acceptPopup() |
||
1875 | |||
1876 | /** |
||
1877 | * Dismisses the active JavaScript popup, as created by `window.alert`, `window.confirm`, or `window.prompt`. |
||
1878 | */ |
||
1879 | public function cancelPopup() |
||
1886 | |||
1887 | /** |
||
1888 | * Checks that the active JavaScript popup, |
||
1889 | * as created by `window.alert`|`window.confirm`|`window.prompt`, contains the given string. |
||
1890 | * |
||
1891 | * @param $text |
||
1892 | * |
||
1893 | * @throws \Codeception\Exception\ModuleException |
||
1894 | */ |
||
1895 | View Code Duplication | public function seeInPopup($text) |
|
1908 | |||
1909 | /** |
||
1910 | * Checks that the active JavaScript popup, |
||
1911 | * as created by `window.alert`|`window.confirm`|`window.prompt`, does NOT contain the given string. |
||
1912 | * |
||
1913 | * @param $text |
||
1914 | * |
||
1915 | * @throws \Codeception\Exception\ModuleException |
||
1916 | */ |
||
1917 | View Code Duplication | public function dontSeeInPopup($text) |
|
1930 | |||
1931 | /** |
||
1932 | * Enters text into a native JavaScript prompt popup, as created by `window.prompt`. |
||
1933 | * |
||
1934 | * @param $keys |
||
1935 | * |
||
1936 | * @throws \Codeception\Exception\ModuleException |
||
1937 | */ |
||
1938 | public function typeInPopup($keys) |
||
1945 | |||
1946 | /** |
||
1947 | * Reloads the current page. |
||
1948 | */ |
||
1949 | public function reloadPage() |
||
1953 | |||
1954 | /** |
||
1955 | * Moves back in history. |
||
1956 | */ |
||
1957 | public function moveBack() |
||
1962 | |||
1963 | /** |
||
1964 | * Moves forward in history. |
||
1965 | */ |
||
1966 | public function moveForward() |
||
1971 | |||
1972 | View Code Duplication | protected function getSubmissionFormFieldName($name) |
|
1979 | |||
1980 | /** |
||
1981 | * Submits the given form on the page, optionally with the given form |
||
1982 | * values. Give the form fields values as an array. Note that hidden fields |
||
1983 | * can't be accessed. |
||
1984 | * |
||
1985 | * Skipped fields will be filled by their values from the page. |
||
1986 | * You don't need to click the 'Submit' button afterwards. |
||
1987 | * This command itself triggers the request to form's action. |
||
1988 | * |
||
1989 | * You can optionally specify what button's value to include |
||
1990 | * in the request with the last parameter as an alternative to |
||
1991 | * explicitly setting its value in the second parameter, as |
||
1992 | * button values are not otherwise included in the request. |
||
1993 | * |
||
1994 | * Examples: |
||
1995 | * |
||
1996 | * ``` php |
||
1997 | * <?php |
||
1998 | * $I->submitForm('#login', [ |
||
1999 | * 'login' => 'davert', |
||
2000 | * 'password' => '123456' |
||
2001 | * ]); |
||
2002 | * // or |
||
2003 | * $I->submitForm('#login', [ |
||
2004 | * 'login' => 'davert', |
||
2005 | * 'password' => '123456' |
||
2006 | * ], 'submitButtonName'); |
||
2007 | * |
||
2008 | * ``` |
||
2009 | * |
||
2010 | * For example, given this sample "Sign Up" form: |
||
2011 | * |
||
2012 | * ``` html |
||
2013 | * <form action="/sign_up"> |
||
2014 | * Login: |
||
2015 | * <input type="text" name="user[login]" /><br/> |
||
2016 | * Password: |
||
2017 | * <input type="password" name="user[password]" /><br/> |
||
2018 | * Do you agree to our terms? |
||
2019 | * <input type="checkbox" name="user[agree]" /><br/> |
||
2020 | * Select pricing plan: |
||
2021 | * <select name="plan"> |
||
2022 | * <option value="1">Free</option> |
||
2023 | * <option value="2" selected="selected">Paid</option> |
||
2024 | * </select> |
||
2025 | * <input type="submit" name="submitButton" value="Submit" /> |
||
2026 | * </form> |
||
2027 | * ``` |
||
2028 | * |
||
2029 | * You could write the following to submit it: |
||
2030 | * |
||
2031 | * ``` php |
||
2032 | * <?php |
||
2033 | * $I->submitForm( |
||
2034 | * '#userForm', |
||
2035 | * [ |
||
2036 | * 'user[login]' => 'Davert', |
||
2037 | * 'user[password]' => '123456', |
||
2038 | * 'user[agree]' => true |
||
2039 | * ], |
||
2040 | * 'submitButton' |
||
2041 | * ); |
||
2042 | * ``` |
||
2043 | * Note that "2" will be the submitted value for the "plan" field, as it is |
||
2044 | * the selected option. |
||
2045 | * |
||
2046 | * Also note that this differs from PhpBrowser, in that |
||
2047 | * ```'user' => [ 'login' => 'Davert' ]``` is not supported at the moment. |
||
2048 | * Named array keys *must* be included in the name as above. |
||
2049 | * |
||
2050 | * Pair this with seeInFormFields for quick testing magic. |
||
2051 | * |
||
2052 | * ``` php |
||
2053 | * <?php |
||
2054 | * $form = [ |
||
2055 | * 'field1' => 'value', |
||
2056 | * 'field2' => 'another value', |
||
2057 | * 'checkbox1' => true, |
||
2058 | * // ... |
||
2059 | * ]; |
||
2060 | * $I->submitForm('//form[@id=my-form]', $form, 'submitButton'); |
||
2061 | * // $I->amOnPage('/path/to/form-page') may be needed |
||
2062 | * $I->seeInFormFields('//form[@id=my-form]', $form); |
||
2063 | * ?> |
||
2064 | * ``` |
||
2065 | * |
||
2066 | * Parameter values must be set to arrays for multiple input fields |
||
2067 | * of the same name, or multi-select combo boxes. For checkboxes, |
||
2068 | * either the string value can be used, or boolean values which will |
||
2069 | * be replaced by the checkbox's value in the DOM. |
||
2070 | * |
||
2071 | * ``` php |
||
2072 | * <?php |
||
2073 | * $I->submitForm('#my-form', [ |
||
2074 | * 'field1' => 'value', |
||
2075 | * 'checkbox' => [ |
||
2076 | * 'value of first checkbox', |
||
2077 | * 'value of second checkbox, |
||
2078 | * ], |
||
2079 | * 'otherCheckboxes' => [ |
||
2080 | * true, |
||
2081 | * false, |
||
2082 | * false |
||
2083 | * ], |
||
2084 | * 'multiselect' => [ |
||
2085 | * 'first option value', |
||
2086 | * 'second option value' |
||
2087 | * ] |
||
2088 | * ]); |
||
2089 | * ?> |
||
2090 | * ``` |
||
2091 | * |
||
2092 | * Mixing string and boolean values for a checkbox's value is not supported |
||
2093 | * and may produce unexpected results. |
||
2094 | * |
||
2095 | * Field names ending in "[]" must be passed without the trailing square |
||
2096 | * bracket characters, and must contain an array for its value. This allows |
||
2097 | * submitting multiple values with the same name, consider: |
||
2098 | * |
||
2099 | * ```php |
||
2100 | * $I->submitForm('#my-form', [ |
||
2101 | * 'field[]' => 'value', |
||
2102 | * 'field[]' => 'another value', // 'field[]' is already a defined key |
||
2103 | * ]); |
||
2104 | * ``` |
||
2105 | * |
||
2106 | * The solution is to pass an array value: |
||
2107 | * |
||
2108 | * ```php |
||
2109 | * // this way both values are submitted |
||
2110 | * $I->submitForm('#my-form', [ |
||
2111 | * 'field' => [ |
||
2112 | * 'value', |
||
2113 | * 'another value', |
||
2114 | * ] |
||
2115 | * ]); |
||
2116 | * ``` |
||
2117 | * |
||
2118 | * The `$button` parameter can be either a string, an array or an instance |
||
2119 | * of Facebook\WebDriver\WebDriverBy. When it is a string, the |
||
2120 | * button will be found by its "name" attribute. If $button is an |
||
2121 | * array then it will be treated as a strict selector and a WebDriverBy |
||
2122 | * will be used verbatim. |
||
2123 | * |
||
2124 | * For example, given the following HTML: |
||
2125 | * |
||
2126 | * ``` html |
||
2127 | * <input type="submit" name="submitButton" value="Submit" /> |
||
2128 | * ``` |
||
2129 | * |
||
2130 | * `$button` could be any one of the following: |
||
2131 | * - 'submitButton' |
||
2132 | * - ['name' => 'submitButton'] |
||
2133 | * - WebDriverBy::name('submitButton') |
||
2134 | * |
||
2135 | * @param $selector |
||
2136 | * @param $params |
||
2137 | * @param $button |
||
2138 | */ |
||
2139 | public function submitForm($selector, array $params, $button = null) |
||
2217 | |||
2218 | /** |
||
2219 | * Waits up to $timeout seconds for the given element to change. |
||
2220 | * Element "change" is determined by a callback function which is called repeatedly |
||
2221 | * until the return value evaluates to true. |
||
2222 | * |
||
2223 | * ``` php |
||
2224 | * <?php |
||
2225 | * use \Facebook\WebDriver\WebDriverElement |
||
2226 | * $I->waitForElementChange('#menu', function(WebDriverElement $el) { |
||
2227 | * return $el->isDisplayed(); |
||
2228 | * }, 100); |
||
2229 | * ?> |
||
2230 | * ``` |
||
2231 | * |
||
2232 | * @param $element |
||
2233 | * @param \Closure $callback |
||
2234 | * @param int $timeout seconds |
||
2235 | * @throws \Codeception\Exception\ElementNotFound |
||
2236 | */ |
||
2237 | public function waitForElementChange($element, \Closure $callback, $timeout = 30) |
||
2245 | |||
2246 | /** |
||
2247 | * Waits up to $timeout seconds for an element to appear on the page. |
||
2248 | * If the element doesn't appear, a timeout exception is thrown. |
||
2249 | * |
||
2250 | * ``` php |
||
2251 | * <?php |
||
2252 | * $I->waitForElement('#agree_button', 30); // secs |
||
2253 | * $I->click('#agree_button'); |
||
2254 | * ?> |
||
2255 | * ``` |
||
2256 | * |
||
2257 | * @param $element |
||
2258 | * @param int $timeout seconds |
||
2259 | * @throws \Exception |
||
2260 | */ |
||
2261 | public function waitForElement($element, $timeout = 10) |
||
2266 | |||
2267 | /** |
||
2268 | * Waits up to $timeout seconds for the given element to be visible on the page. |
||
2269 | * If element doesn't appear, a timeout exception is thrown. |
||
2270 | * |
||
2271 | * ``` php |
||
2272 | * <?php |
||
2273 | * $I->waitForElementVisible('#agree_button', 30); // secs |
||
2274 | * $I->click('#agree_button'); |
||
2275 | * ?> |
||
2276 | * ``` |
||
2277 | * |
||
2278 | * @param $element |
||
2279 | * @param int $timeout seconds |
||
2280 | * @throws \Exception |
||
2281 | */ |
||
2282 | public function waitForElementVisible($element, $timeout = 10) |
||
2287 | |||
2288 | /** |
||
2289 | * Waits up to $timeout seconds for the given element to become invisible. |
||
2290 | * If element stays visible, a timeout exception is thrown. |
||
2291 | * |
||
2292 | * ``` php |
||
2293 | * <?php |
||
2294 | * $I->waitForElementNotVisible('#agree_button', 30); // secs |
||
2295 | * ?> |
||
2296 | * ``` |
||
2297 | * |
||
2298 | * @param $element |
||
2299 | * @param int $timeout seconds |
||
2300 | * @throws \Exception |
||
2301 | */ |
||
2302 | public function waitForElementNotVisible($element, $timeout = 10) |
||
2307 | |||
2308 | /** |
||
2309 | * Waits up to $timeout seconds for the given string to appear on the page. |
||
2310 | * |
||
2311 | * Can also be passed a selector to search in, be as specific as possible when using selectors. |
||
2312 | * waitForText() will only watch the first instance of the matching selector / text provided. |
||
2313 | * If the given text doesn't appear, a timeout exception is thrown. |
||
2314 | * |
||
2315 | * ``` php |
||
2316 | * <?php |
||
2317 | * $I->waitForText('foo', 30); // secs |
||
2318 | * $I->waitForText('foo', 30, '.title'); // secs |
||
2319 | * ?> |
||
2320 | * ``` |
||
2321 | * |
||
2322 | * @param string $text |
||
2323 | * @param int $timeout seconds |
||
2324 | * @param string $selector optional |
||
2325 | * @throws \Exception |
||
2326 | */ |
||
2327 | public function waitForText($text, $timeout = 10, $selector = null) |
||
2343 | |||
2344 | /** |
||
2345 | * Wait for $timeout seconds. |
||
2346 | * |
||
2347 | * @param int|float $timeout secs |
||
2348 | * @throws \Codeception\Exception\TestRuntimeException |
||
2349 | */ |
||
2350 | public function wait($timeout) |
||
2361 | |||
2362 | /** |
||
2363 | * Low-level API method. |
||
2364 | * If Codeception commands are not enough, this allows you to use Selenium WebDriver methods directly: |
||
2365 | * |
||
2366 | * ``` php |
||
2367 | * $I->executeInSelenium(function(\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) { |
||
2368 | * $webdriver->get('http://google.com'); |
||
2369 | * }); |
||
2370 | * ``` |
||
2371 | * |
||
2372 | * This runs in the context of the |
||
2373 | * [RemoteWebDriver class](https://github.com/facebook/php-webdriver/blob/master/lib/remote/RemoteWebDriver.php). |
||
2374 | * Try not to use this command on a regular basis. |
||
2375 | * If Codeception lacks a feature you need, please implement it and submit a patch. |
||
2376 | * |
||
2377 | * @param callable $function |
||
2378 | */ |
||
2379 | public function executeInSelenium(\Closure $function) |
||
2383 | |||
2384 | /** |
||
2385 | * Switch to another window identified by name. |
||
2386 | * |
||
2387 | * The window can only be identified by name. If the $name parameter is blank, the parent window will be used. |
||
2388 | * |
||
2389 | * Example: |
||
2390 | * ``` html |
||
2391 | * <input type="button" value="Open window" onclick="window.open('http://example.com', 'another_window')"> |
||
2392 | * ``` |
||
2393 | * |
||
2394 | * ``` php |
||
2395 | * <?php |
||
2396 | * $I->click("Open window"); |
||
2397 | * # switch to another window |
||
2398 | * $I->switchToWindow("another_window"); |
||
2399 | * # switch to parent window |
||
2400 | * $I->switchToWindow(); |
||
2401 | * ?> |
||
2402 | * ``` |
||
2403 | * |
||
2404 | * If the window has no name, match it by switching to next active tab using `switchToNextTab` method. |
||
2405 | * |
||
2406 | * Or use native Selenium functions to get access to all opened windows: |
||
2407 | * |
||
2408 | * ``` php |
||
2409 | * <?php |
||
2410 | * $I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) { |
||
2411 | * $handles=$webdriver->getWindowHandles(); |
||
2412 | * $last_window = end($handles); |
||
2413 | * $webdriver->switchTo()->window($last_window); |
||
2414 | * }); |
||
2415 | * ?> |
||
2416 | * ``` |
||
2417 | * |
||
2418 | * @param string|null $name |
||
2419 | */ |
||
2420 | public function switchToWindow($name = null) |
||
2424 | |||
2425 | /** |
||
2426 | * Switch to another frame on the page. |
||
2427 | * |
||
2428 | * Example: |
||
2429 | * ``` html |
||
2430 | * <iframe name="another_frame" src="http://example.com"> |
||
2431 | * |
||
2432 | * ``` |
||
2433 | * |
||
2434 | * ``` php |
||
2435 | * <?php |
||
2436 | * # switch to iframe |
||
2437 | * $I->switchToIFrame("another_frame"); |
||
2438 | * # switch to parent page |
||
2439 | * $I->switchToIFrame(); |
||
2440 | * |
||
2441 | * ``` |
||
2442 | * |
||
2443 | * @param string|null $name |
||
2444 | */ |
||
2445 | public function switchToIFrame($name = null) |
||
2453 | |||
2454 | /** |
||
2455 | * Executes JavaScript and waits up to $timeout seconds for it to return true. |
||
2456 | * |
||
2457 | * In this example we will wait up to 60 seconds for all jQuery AJAX requests to finish. |
||
2458 | * |
||
2459 | * ``` php |
||
2460 | * <?php |
||
2461 | * $I->waitForJS("return $.active == 0;", 60); |
||
2462 | * ?> |
||
2463 | * ``` |
||
2464 | * |
||
2465 | * @param string $script |
||
2466 | * @param int $timeout seconds |
||
2467 | */ |
||
2468 | public function waitForJS($script, $timeout = 5) |
||
2480 | |||
2481 | /** |
||
2482 | * Executes custom JavaScript. |
||
2483 | * |
||
2484 | * This example uses jQuery to get a value and assigns that value to a PHP variable: |
||
2485 | * |
||
2486 | * ```php |
||
2487 | * <?php |
||
2488 | * $myVar = $I->executeJS('return $("#myField").val()'); |
||
2489 | * ?> |
||
2490 | * ``` |
||
2491 | * |
||
2492 | * @param $script |
||
2493 | * @return mixed |
||
2494 | */ |
||
2495 | public function executeJS($script) |
||
2499 | |||
2500 | /** |
||
2501 | * Maximizes the current window. |
||
2502 | */ |
||
2503 | public function maximizeWindow() |
||
2507 | |||
2508 | /** |
||
2509 | * Performs a simple mouse drag-and-drop operation. |
||
2510 | * |
||
2511 | * ``` php |
||
2512 | * <?php |
||
2513 | * $I->dragAndDrop('#drag', '#drop'); |
||
2514 | * ?> |
||
2515 | * ``` |
||
2516 | * |
||
2517 | * @param string $source (CSS ID or XPath) |
||
2518 | * @param string $target (CSS ID or XPath) |
||
2519 | */ |
||
2520 | public function dragAndDrop($source, $target) |
||
2528 | |||
2529 | /** |
||
2530 | * Move mouse over the first element matched by the given locator. |
||
2531 | * If the first parameter null then the page is used. |
||
2532 | * If the second and third parameters are given, |
||
2533 | * then the mouse is moved to an offset of the element's top-left corner. |
||
2534 | * Otherwise, the mouse is moved to the center of the element. |
||
2535 | * |
||
2536 | * ``` php |
||
2537 | * <?php |
||
2538 | * $I->moveMouseOver(['css' => '.checkout']); |
||
2539 | * $I->moveMouseOver(null, 20, 50); |
||
2540 | * $I->moveMouseOver(['css' => '.checkout'], 20, 50); |
||
2541 | * ?> |
||
2542 | * ``` |
||
2543 | * |
||
2544 | * @param string $cssOrXPath css or xpath of the web element |
||
2545 | * @param int $offsetX |
||
2546 | * @param int $offsetY |
||
2547 | * |
||
2548 | * @throws \Codeception\Exception\ElementNotFound |
||
2549 | */ |
||
2550 | public function moveMouseOver($cssOrXPath = null, $offsetX = null, $offsetY = null) |
||
2560 | |||
2561 | /** |
||
2562 | * Performs click with the left mouse button on an element. |
||
2563 | * If the first parameter `null` then the offset is relative to the actual mouse position. |
||
2564 | * If the second and third parameters are given, |
||
2565 | * then the mouse is moved to an offset of the element's top-left corner. |
||
2566 | * Otherwise, the mouse is moved to the center of the element. |
||
2567 | * |
||
2568 | * ``` php |
||
2569 | * <?php |
||
2570 | * $I->clickWithLeftButton(['css' => '.checkout']); |
||
2571 | * $I->clickWithLeftButton(null, 20, 50); |
||
2572 | * $I->clickWithLeftButton(['css' => '.checkout'], 20, 50); |
||
2573 | * ?> |
||
2574 | * ``` |
||
2575 | * |
||
2576 | * @param string $cssOrXPath css or xpath of the web element (body by default). |
||
2577 | * @param int $offsetX |
||
2578 | * @param int $offsetY |
||
2579 | * |
||
2580 | * @throws \Codeception\Exception\ElementNotFound |
||
2581 | */ |
||
2582 | public function clickWithLeftButton($cssOrXPath = null, $offsetX = null, $offsetY = null) |
||
2587 | |||
2588 | /** |
||
2589 | * Performs contextual click with the right mouse button on an element. |
||
2590 | * If the first parameter `null` then the offset is relative to the actual mouse position. |
||
2591 | * If the second and third parameters are given, |
||
2592 | * then the mouse is moved to an offset of the element's top-left corner. |
||
2593 | * Otherwise, the mouse is moved to the center of the element. |
||
2594 | * |
||
2595 | * ``` php |
||
2596 | * <?php |
||
2597 | * $I->clickWithRightButton(['css' => '.checkout']); |
||
2598 | * $I->clickWithRightButton(null, 20, 50); |
||
2599 | * $I->clickWithRightButton(['css' => '.checkout'], 20, 50); |
||
2600 | * ?> |
||
2601 | * ``` |
||
2602 | * |
||
2603 | * @param string $cssOrXPath css or xpath of the web element (body by default). |
||
2604 | * @param int $offsetX |
||
2605 | * @param int $offsetY |
||
2606 | * |
||
2607 | * @throws \Codeception\Exception\ElementNotFound |
||
2608 | */ |
||
2609 | public function clickWithRightButton($cssOrXPath = null, $offsetX = null, $offsetY = null) |
||
2614 | |||
2615 | /** |
||
2616 | * Pauses test execution in debug mode. |
||
2617 | * To proceed test press "ENTER" in console. |
||
2618 | * |
||
2619 | * This method is useful while writing tests, |
||
2620 | * since it allows you to inspect the current page in the middle of a test case. |
||
2621 | */ |
||
2622 | public function pauseExecution() |
||
2626 | |||
2627 | /** |
||
2628 | * Performs a double-click on an element matched by CSS or XPath. |
||
2629 | * |
||
2630 | * @param $cssOrXPath |
||
2631 | * @throws \Codeception\Exception\ElementNotFound |
||
2632 | */ |
||
2633 | public function doubleClick($cssOrXPath) |
||
2638 | |||
2639 | /** |
||
2640 | * @param $page |
||
2641 | * @param $selector |
||
2642 | * @param bool $throwMalformed |
||
2643 | * @return array |
||
2644 | */ |
||
2645 | protected function match($page, $selector, $throwMalformed = true) |
||
2706 | |||
2707 | /** |
||
2708 | * @param array $by |
||
2709 | * @return WebDriverBy |
||
2710 | */ |
||
2711 | protected function getStrictLocator(array $by) |
||
2735 | |||
2736 | /** |
||
2737 | * @param $page |
||
2738 | * @param $selector |
||
2739 | * @return WebDriverElement |
||
2740 | * @throws \Codeception\Exception\ElementNotFound |
||
2741 | */ |
||
2742 | protected function matchFirstOrFail($page, $selector) |
||
2752 | |||
2753 | /** |
||
2754 | * Presses the given key on the given element. |
||
2755 | * To specify a character and modifier (e.g. ctrl, alt, shift, meta), pass an array for $char with |
||
2756 | * the modifier as the first element and the character as the second. |
||
2757 | * For special keys use key constants from WebDriverKeys class. |
||
2758 | * |
||
2759 | * ``` php |
||
2760 | * <?php |
||
2761 | * // <input id="page" value="old" /> |
||
2762 | * $I->pressKey('#page','a'); // => olda |
||
2763 | * $I->pressKey('#page',array('ctrl','a'),'new'); //=> new |
||
2764 | * $I->pressKey('#page',array('shift','111'),'1','x'); //=> old!!!1x |
||
2765 | * $I->pressKey('descendant-or-self::*[@id='page']','u'); //=> oldu |
||
2766 | * $I->pressKey('#name', array('ctrl', 'a'), \Facebook\WebDriver\WebDriverKeys::DELETE); //=>'' |
||
2767 | * ?> |
||
2768 | * ``` |
||
2769 | * |
||
2770 | * @param $element |
||
2771 | * @param $char string|array Can be char or array with modifier. You can provide several chars. |
||
2772 | * @throws \Codeception\Exception\ElementNotFound |
||
2773 | */ |
||
2774 | public function pressKey($element, $char) |
||
2785 | |||
2786 | protected function convertKeyModifier($keys) |
||
2809 | |||
2810 | protected function assertNodesContain($text, $nodes, $selector = null) |
||
2814 | |||
2815 | protected function assertNodesNotContain($text, $nodes, $selector = null) |
||
2819 | |||
2820 | protected function assertPageContains($needle, $message = '') |
||
2828 | |||
2829 | protected function assertPageNotContains($needle, $message = '') |
||
2837 | |||
2838 | protected function assertPageSourceContains($needle, $message = '') |
||
2846 | |||
2847 | protected function assertPageSourceNotContains($needle, $message = '') |
||
2855 | |||
2856 | /** |
||
2857 | * Append the given text to the given element. |
||
2858 | * Can also add a selection to a select box. |
||
2859 | * |
||
2860 | * ``` php |
||
2861 | * <?php |
||
2862 | * $I->appendField('#mySelectbox', 'SelectValue'); |
||
2863 | * $I->appendField('#myTextField', 'appended'); |
||
2864 | * ?> |
||
2865 | * ``` |
||
2866 | * |
||
2867 | * @param string $field |
||
2868 | * @param string $value |
||
2869 | * @throws \Codeception\Exception\ElementNotFound |
||
2870 | */ |
||
2871 | public function appendField($field, $value) |
||
2931 | |||
2932 | /** |
||
2933 | * @param $selector |
||
2934 | * @return array |
||
2935 | */ |
||
2936 | protected function matchVisible($selector) |
||
2947 | |||
2948 | /** |
||
2949 | * @param $selector |
||
2950 | * @return WebDriverBy |
||
2951 | * @throws \InvalidArgumentException |
||
2952 | */ |
||
2953 | protected function getLocator($selector) |
||
2972 | |||
2973 | /** |
||
2974 | * @param string $name |
||
2975 | */ |
||
2976 | public function saveSessionSnapshot($name) |
||
2992 | |||
2993 | /** |
||
2994 | * @param string $name |
||
2995 | * @return bool |
||
2996 | */ |
||
2997 | public function loadSessionSnapshot($name) |
||
3009 | |||
3010 | /** |
||
3011 | * Check if the cookie domain matches the config URL. |
||
3012 | * |
||
3013 | * @param array|Cookie $cookie |
||
3014 | * @return bool |
||
3015 | */ |
||
3016 | private function cookieDomainMatchesConfigUrl($cookie) |
||
3027 | |||
3028 | /** |
||
3029 | * @return bool |
||
3030 | */ |
||
3031 | protected function isPhantom() |
||
3035 | |||
3036 | /** |
||
3037 | * Move to the middle of the given element matched by the given locator. |
||
3038 | * Extra shift, calculated from the top-left corner of the element, |
||
3039 | * can be set by passing $offsetX and $offsetY parameters. |
||
3040 | * |
||
3041 | * ``` php |
||
3042 | * <?php |
||
3043 | * $I->scrollTo(['css' => '.checkout'], 20, 50); |
||
3044 | * ?> |
||
3045 | * ``` |
||
3046 | * |
||
3047 | * @param $selector |
||
3048 | * @param int $offsetX |
||
3049 | * @param int $offsetY |
||
3050 | */ |
||
3051 | public function scrollTo($selector, $offsetX = null, $offsetY = null) |
||
3058 | |||
3059 | /** |
||
3060 | * Opens a new browser tab (wherever it is possible) and switches to it. |
||
3061 | * |
||
3062 | * ```php |
||
3063 | * <?php |
||
3064 | * $I->openNewTab(); |
||
3065 | * ``` |
||
3066 | * Tab is opened by using `window.open` javascript in a browser. |
||
3067 | * Please note, that adblock can restrict creating such tabs. |
||
3068 | * |
||
3069 | * Can't be used with PhantomJS |
||
3070 | * |
||
3071 | */ |
||
3072 | public function openNewTab() |
||
3077 | |||
3078 | /** |
||
3079 | * Closes current browser tab and switches to previous active tab. |
||
3080 | * |
||
3081 | * ```php |
||
3082 | * <?php |
||
3083 | * $I->closeTab(); |
||
3084 | * ``` |
||
3085 | * |
||
3086 | * Can't be used with PhantomJS |
||
3087 | */ |
||
3088 | public function closeTab() |
||
3094 | |||
3095 | /** |
||
3096 | * Switches to next browser tab. |
||
3097 | * An offset can be specified. |
||
3098 | * |
||
3099 | * ```php |
||
3100 | * <?php |
||
3101 | * // switch to next tab |
||
3102 | * $I->switchToNextTab(); |
||
3103 | * // switch to 2nd next tab |
||
3104 | * $I->switchToNextTab(2); |
||
3105 | * ``` |
||
3106 | * |
||
3107 | * Can't be used with PhantomJS |
||
3108 | * |
||
3109 | * @param int $offset 1 |
||
3110 | */ |
||
3111 | public function switchToNextTab($offset = 1) |
||
3116 | |||
3117 | /** |
||
3118 | * Switches to previous browser tab. |
||
3119 | * An offset can be specified. |
||
3120 | * |
||
3121 | * ```php |
||
3122 | * <?php |
||
3123 | * // switch to previous tab |
||
3124 | * $I->switchToPreviousTab(); |
||
3125 | * // switch to 2nd previous tab |
||
3126 | * $I->switchToPreviousTab(2); |
||
3127 | * ``` |
||
3128 | * |
||
3129 | * Can't be used with PhantomJS |
||
3130 | * |
||
3131 | * @param int $offset 1 |
||
3132 | */ |
||
3133 | public function switchToPreviousTab($offset = 1) |
||
3137 | |||
3138 | protected function getRelativeTabHandle($offset) |
||
3148 | |||
3149 | /** |
||
3150 | * Waits for element and runs a sequence of actions inside its context. |
||
3151 | * Actions can be defined with array, callback, or `Codeception\Util\ActionSequence` instance. |
||
3152 | * |
||
3153 | * Actions as array are recommended for simple to combine "waitForElement" with assertions; |
||
3154 | * `waitForElement($el)` and `see('text', $el)` can be simplified to: |
||
3155 | * |
||
3156 | * ```php |
||
3157 | * <?php |
||
3158 | * $I->performOn($el, ['see' => 'text']); |
||
3159 | * ``` |
||
3160 | * |
||
3161 | * List of actions can be pragmatically build using `Codeception\Util\ActionSequence`: |
||
3162 | * |
||
3163 | * ```php |
||
3164 | * <?php |
||
3165 | * $I->performOn('.model', ActionSequence::build() |
||
3166 | * ->see('Warning') |
||
3167 | * ->see('Are you sure you want to delete this?') |
||
3168 | * ->click('Yes') |
||
3169 | * ); |
||
3170 | * ``` |
||
3171 | * |
||
3172 | * Actions executed from array or ActionSequence will print debug output for actions, and adds an action name to |
||
3173 | * exception on failure. |
||
3174 | * |
||
3175 | * Whenever you need to define more actions a callback can be used. A WebDriver module is passed for argument: |
||
3176 | * |
||
3177 | * ```php |
||
3178 | * <?php |
||
3179 | * $I->performOn('.rememberMe', function (WebDriver $I) { |
||
3180 | * $I->see('Remember me next time'); |
||
3181 | * $I->seeElement('#LoginForm_rememberMe'); |
||
3182 | * $I->dontSee('Login'); |
||
3183 | * }); |
||
3184 | * ``` |
||
3185 | * |
||
3186 | * In 3rd argument you can set number a seconds to wait for element to appear |
||
3187 | * |
||
3188 | * @param $element |
||
3189 | * @param $actions |
||
3190 | * @param int $timeout |
||
3191 | */ |
||
3192 | public function performOn($element, $actions, $timeout = 10) |
||
3214 | |||
3215 | protected function setBaseElement($element = null) |
||
3223 | |||
3224 | protected function enableImplicitWait() |
||
3231 | |||
3232 | protected function disableImplicitWait() |
||
3239 | } |
||
3240 |