11<?php
22
3+ //phpcs:disable Generic.Files.LineLength.TooLong
34declare (strict_types=1 );
45
56namespace Codeception \Module ;
6566 *
6667 * * Java is required
6768 * * NodeJS is required
68- *
69+ *
6970 * The fastest way to get started is to [Install and launch Selenium using selenium-standalone NodeJS package](https://www.npmjs.com/package/selenium-standalone).
70- *
71+ *
7172 * Launch selenium standalone in separate console window:
72- *
73+ *
7374 * ```
7475 * selenium-standalone start
7576 * ```
8384 * url: 'http://localhost/'
8485 * browser: chrome # 'chrome' or 'firefox'
8586 * ```
86- *
87+ *
8788 * ## Headless Chrome Browser
88- *
89+ *
8990 * To enable headless mode (launch tests without showing a window) for Chrome browser using Selenium use this config in `acceptance.suite.yml`:
90- *
91+ *
9192 * ```yaml
9293 * modules:
9394 * enabled:
9899 * chromeOptions:
99100 * args: ["--headless", "--disable-gpu"]
100101 * ```
101- *
102+ *
102103 * ## Headless Selenium in Docker
103104 *
104105 * Docker can ship Selenium Server with all its dependencies and browsers inside a single container.
113114 * ## Local Chrome and/or Firefox
114115 *
115116 * Tests can be executed directly through ChromeDriver or GeckoDriver (for Firefox). Consider using this option if you don't plan to use Selenium.
116- *
117+ *
117118 * ### ChromeDriver
118- *
119+ *
119120 * * Download and install [ChromeDriver](https://sites.google.com/chromium.org/driver/downloads?authuser=0)
120121 * * Launch ChromeDriver in a separate console window: `chromedriver --url-base=/wd/hub`.
121- *
122+ *
122123 * Configuration in `acceptance.suite.yml`:
123124 *
124125 * ```yaml
139140 *
140141 *
141142 * ### GeckoDriver
142- *
143+ *
143144 * * [GeckoDriver](https://github.com/mozilla/geckodriver/releases) must be installed
144145 * * Start GeckoDriver in a separate console window: `geckodriver`.
145- *
146+ *
146147 * Configuration in `acceptance.suite.yml`:
147148 *
148149 * ```yaml
160161 * intl.accept_languages: "de-AT" # Set HTTP-Header `Accept-Language: de-AT` for requests
161162 * ```
162163 * See here for [Firefox capabilities](https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#List_of_capabilities)
163- *
164+ *
164165 * ## Cloud Testing
165166 *
166167 * Cloud Testing services can run your WebDriver tests in the cloud.
@@ -460,15 +461,24 @@ public function _requires(): array
460461 protected function getBaseElement (): WebDriverSearchContext
461462 {
462463 if (!$ this ->baseElement ) {
463- throw new ModuleException ($ this , "Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it " );
464+ throw new ModuleException (
465+ $ this ,
466+ "Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it "
467+ );
464468 }
465469
466470 return $ this ->baseElement ;
467471 }
468472
469473 public function _initialize ()
470474 {
471- $ this ->wdHost = sprintf ('%s://%s:%s%s ' , $ this ->config ['protocol ' ], $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['path ' ]);
475+ $ this ->wdHost = sprintf (
476+ '%s://%s:%s%s ' ,
477+ $ this ->config ['protocol ' ],
478+ $ this ->config ['host ' ],
479+ $ this ->config ['port ' ],
480+ $ this ->config ['path ' ]
481+ );
472482 $ this ->capabilities = $ this ->config ['capabilities ' ];
473483 $ this ->capabilities [WebDriverCapabilityType::BROWSER_NAME ] = $ this ->config ['browser ' ];
474484 if ($ proxy = $ this ->getProxy ()) {
@@ -674,7 +684,8 @@ public function debugWebDriverLogs(TestInterface $test = null): void
674684
675685 $ this ->debugSection ("Selenium {$ logType } Logs " , "\n" . $ this ->formatLogEntries ($ logEntries ));
676686
677- if ($ logType === 'browser ' && $ this ->config ['log_js_errors ' ]
687+ if (
688+ $ logType === 'browser ' && $ this ->config ['log_js_errors ' ]
678689 && ($ test instanceof ScenarioDriven)
679690 ) {
680691 $ this ->logJSErrors ($ test , $ logEntries );
@@ -711,7 +722,8 @@ protected function formatLogEntries(array $logEntries): string
711722 protected function logJSErrors (ScenarioDriven $ test , array $ browserLogEntries ): void
712723 {
713724 foreach ($ browserLogEntries as $ logEntry ) {
714- if (isset ($ logEntry ['level ' ])
725+ if (
726+ isset ($ logEntry ['level ' ])
715727 && isset ($ logEntry ['message ' ])
716728 && $ this ->isJSError ($ logEntry ['level ' ], $ logEntry ['message ' ])
717729 ) {
@@ -1167,7 +1179,10 @@ public function click($link, $context = null): void
11671179 try {
11681180 $ els = $ this ->match ($ page , $ link );
11691181 } catch (MalformedLocatorException $ exception ) {
1170- throw new ElementNotFound ("name= {$ link }" , "' {$ link }' is invalid CSS and XPath selector and Link or Button " );
1182+ throw new ElementNotFound (
1183+ "name= {$ link }" ,
1184+ "' {$ link }' is invalid CSS and XPath selector and Link or Button "
1185+ );
11711186 }
11721187
11731188 $ el = reset ($ els );
@@ -1237,7 +1252,6 @@ public function _findClickable(WebDriverSearchContext $page, $link): ?WebDriverE
12371252 ".//input[./@type = 'submit' or ./@type = 'image' or ./@type = 'button'][./@name = {$ locator } or ./@title = {$ locator }] " ,
12381253 ".//button[./@name = {$ locator } or ./@title = {$ locator }] "
12391254 );
1240-
12411255 $ els = $ page ->findElements (WebDriverBy::xpath ($ xpath ));
12421256 if (count ($ els ) > 0 ) {
12431257 return reset ($ els );
@@ -1270,10 +1284,8 @@ protected function findFields($selector): array
12701284 $ locator = static ::xPathLiteral (trim ((string ) $ selector ));
12711285 // by text or label
12721286 $ xpath = Locator::combine (
1273- // @codingStandardsIgnoreStart
12741287 ".//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][(((./@name = {$ locator }) or ./@id = //label[contains(normalize-space(string(.)), {$ locator })]/@for) or ./@placeholder = {$ locator })] " ,
12751288 ".//label[contains(normalize-space(string(.)), {$ locator })]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')] "
1276- // @codingStandardsIgnoreEnd
12771289 );
12781290 $ fields = $ this ->getBaseElement ()->findElements (WebDriverBy::xpath ($ xpath ));
12791291 if (!empty ($ fields )) {
@@ -1321,7 +1333,10 @@ public function seeLink(string $text, string $url = null): void
13211333 $ nodes = $ this ->filterNodesByHref ($ url , $ nodes );
13221334 }
13231335
1324- $ this ->assertNotEmpty ($ nodes , "No links containing text ' {$ text }' and URL ' {$ url }' were found in page {$ currentUri }" );
1336+ $ this ->assertNotEmpty (
1337+ $ nodes ,
1338+ "No links containing text ' {$ text }' and URL ' {$ url }' were found in page {$ currentUri }"
1339+ );
13251340 }
13261341
13271342 public function dontSeeLink (string $ text , string $ url = '' ): void
@@ -1332,7 +1347,10 @@ public function dontSeeLink(string $text, string $url = ''): void
13321347 $ this ->assertEmpty ($ nodes , "Link containing text ' {$ text }' was found in page {$ currentUri }" );
13331348 } else {
13341349 $ nodes = $ this ->filterNodesByHref ($ url , $ nodes );
1335- $ this ->assertEmpty ($ nodes , "Link containing text ' {$ text }' and URL ' {$ url }' was found in page {$ currentUri }" );
1350+ $ this ->assertEmpty (
1351+ $ nodes ,
1352+ "Link containing text ' {$ text }' and URL ' {$ url }' was found in page {$ currentUri }"
1353+ );
13361354 }
13371355 }
13381356
@@ -1529,15 +1547,15 @@ protected function proceedSeeInField(array $elements, $value): array
15291547
15301548 break ;
15311549 case 'option ' :
1532- // no break we need the trim text and the value also
15331550 if (!$ el ->isSelected ()) {
15341551 break ;
15351552 }
15361553
15371554 $ currentValues [] = $ el ->getText ();
1555+ // no break we need the trim text and the value also
15381556 case 'textarea ' :
1539- // we include trimmed and real value of textarea for check
15401557 $ currentValues [] = trim ($ el ->getText ());
1558+ // we include trimmed and real value of textarea for check
15411559 default :
15421560 $ currentValues [] = $ el ->getAttribute ('value ' ); // raw value
15431561 break ;
@@ -1629,7 +1647,10 @@ public function selectOption($select, $option): void
16291647 return ;
16301648 }
16311649
1632- throw new ElementNotFound (json_encode ($ option , JSON_THROW_ON_ERROR ), "Option inside {$ select } matched by name or value " );
1650+ throw new ElementNotFound (
1651+ json_encode ($ option , JSON_THROW_ON_ERROR ),
1652+ "Option inside {$ select } matched by name or value "
1653+ );
16331654 }
16341655
16351656 /**
@@ -1662,7 +1683,10 @@ public function _initializeSession(): void
16621683 $ this ->initialWindowSize ();
16631684 } catch (WebDriverCurlException $ exception ) {
16641685 codecept_debug ('Curl error: ' . $ exception ->getMessage ());
1665- throw new ConnectionException ("Can't connect to WebDriver at {$ this ->wdHost }. Make sure that ChromeDriver, GeckoDriver or Selenium Server is running. " );
1686+ throw new ConnectionException (
1687+ "Can't connect to WebDriver at {$ this ->wdHost }.' "
1688+ . ' Make sure that ChromeDriver, GeckoDriver or Selenium Server is running. '
1689+ );
16661690 }
16671691 }
16681692
@@ -1765,8 +1789,11 @@ public function unselectOption($select, $option): void
17651789 /**
17661790 * @param string|array|WebDriverBy|WebDriverElement $radioOrCheckbox
17671791 */
1768- protected function findCheckable (WebDriverSearchContext $ context , $ radioOrCheckbox , bool $ byValue = false ): ?WebDriverElement
1769- {
1792+ protected function findCheckable (
1793+ WebDriverSearchContext $ context ,
1794+ $ radioOrCheckbox ,
1795+ bool $ byValue = false
1796+ ): ?WebDriverElement {
17701797 if ($ radioOrCheckbox instanceof WebDriverElement) {
17711798 return $ radioOrCheckbox ;
17721799 }
@@ -1786,23 +1813,22 @@ protected function findCheckable(WebDriverSearchContext $context, $radioOrCheckb
17861813 $ typeLiteral = static ::xPathLiteral ($ contextType );
17871814 $ inputLocatorFragment = "input[@type = {$ typeLiteral }][@name = {$ nameLiteral }] " ;
17881815 $ xpath = Locator::combine (
1789- // @codingStandardsIgnoreStart
17901816 "ancestor::form// {$ inputLocatorFragment }[(@id = ancestor::form//label[contains(normalize-space(string(.)), {$ locator })]/@for) or @placeholder = {$ locator }] " ,
1791- // @codingStandardsIgnoreEnd
17921817 "ancestor::form//label[contains(normalize-space(string(.)), {$ locator })]// {$ inputLocatorFragment }"
17931818 );
17941819 if ($ byValue ) {
17951820 $ xpath = Locator::combine ($ xpath , "ancestor::form// {$ inputLocatorFragment }[@value = {$ locator }] " );
17961821 }
17971822 } else {
17981823 $ xpath = Locator::combine (
1799- // @codingStandardsIgnoreStart
18001824 "//input[@type = 'checkbox' or @type = 'radio'][(@id = //label[contains(normalize-space(string(.)), {$ locator })]/@for) or @placeholder = {$ locator } or @name = {$ locator }] " ,
1801- // @codingStandardsIgnoreEnd
18021825 "//label[contains(normalize-space(string(.)), {$ locator })]//input[@type = 'radio' or @type = 'checkbox'] "
18031826 );
18041827 if ($ byValue ) {
1805- $ xpath = Locator::combine ($ xpath , sprintf ("//input[@type = 'checkbox' or @type = 'radio'][@value = %s] " , $ locator ));
1828+ $ xpath = Locator::combine (
1829+ $ xpath ,
1830+ sprintf ("//input[@type = 'checkbox' or @type = 'radio'][@value = %s] " , $ locator )
1831+ );
18061832 }
18071833 }
18081834
@@ -2446,7 +2472,9 @@ public function submitForm($selector, array $params, $button = null): void
24462472 $ form = $ this ->matchFirstOrFail ($ this ->getBaseElement (), $ selector );
24472473
24482474 $ fields = $ form ->findElements (
2449- WebDriverBy::cssSelector ('input:enabled[name],textarea:enabled[name],select:enabled[name],input[type=hidden][name] ' )
2475+ WebDriverBy::cssSelector (
2476+ 'input:enabled[name],textarea:enabled[name],select:enabled[name],input[type=hidden][name] '
2477+ )
24502478 );
24512479 foreach ($ fields as $ field ) {
24522480 $ fieldName = $ this ->getSubmissionFormFieldName ($ field ->getAttribute ('name ' ) ?? '' );
@@ -3278,7 +3306,10 @@ public function appendField($field, string $value): void
32783306 return ;
32793307 }
32803308
3281- throw new ElementNotFound (json_encode ($ value , JSON_THROW_ON_ERROR ), "Option inside {$ field } matched by name or value " );
3309+ throw new ElementNotFound (
3310+ json_encode ($ value , JSON_THROW_ON_ERROR ),
3311+ "Option inside {$ field } matched by name or value "
3312+ );
32823313 case "textarea " :
32833314 $ el ->sendKeys ($ value );
32843315 return ;
@@ -3491,7 +3522,7 @@ public function openNewTab(): void
34913522 public function seeNumberOfTabs ($ number ): void
34923523 {
34933524 $ this ->assertSame (count ($ this ->webDriver ->getWindowHandles ()), $ number );
3494- }
3525+ }
34953526
34963527 /**
34973528 * Closes current browser tab and switches to previous active tab.
@@ -3697,5 +3728,4 @@ private static function xPathLiteral($s): string
36973728
36983729 return sprintf ('concat(%s) ' , implode (', ' , $ parts ));
36993730 }
3700-
37013731}
0 commit comments