From bc8216e9033d5bf2ca7c8e59251b9bb75370f921 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Mon, 24 Feb 2020 00:39:09 +0100 Subject: [PATCH 1/8] Fix selenium tests Signed-off-by: William Desportes --- test/selenium/Database/ProceduresTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/selenium/Database/ProceduresTest.php b/test/selenium/Database/ProceduresTest.php index 95cea72357..8ec7ce30db 100644 --- a/test/selenium/Database/ProceduresTest.php +++ b/test/selenium/Database/ProceduresTest.php @@ -226,7 +226,7 @@ class ProceduresTest extends TestBase private function _executeProcedure($text, $length) { $this->waitAjax(); - $this->waitUntilElementIsVisible('linkText', ' Execute', 30)->click();// The space before Execute is because of   + $this->waitUntilElementIsVisible('partialLinkText', 'Execute', 30)->click();// The space before Execute is because of   $this->waitUntilElementIsVisible('name', "params[inp]", 30)->sendKeys($text); $this->byCssSelector("div.ui-dialog-buttonset button:nth-child(1)")->click(); From 29306ee18ba48d6cf1304ac475f4f701af26823b Mon Sep 17 00:00:00 2001 From: William Desportes Date: Mon, 24 Feb 2020 22:27:53 +0100 Subject: [PATCH 2/8] Add scrollToElement function Signed-off-by: William Desportes --- test/selenium/TestBase.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index 3368198e3f..d0306044f9 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -972,6 +972,20 @@ abstract class TestBase extends TestCase ); } + /** + * Scrolls to a coordinate such that the element + * + * @param WebDriverElement $element The element + * + * @return void + */ + public function scrollToElement(WebDriverElement $element, int $xOffset = 0, int $yOffset = 0): void + { + $this->webDriver->executeScript( + 'window.scrollBy(' . ($element->getLocation()->getX() + $xOffset) . ', ' . ($element->getLocation()->getY() + $yOffset) . ');' + ); + } + /** * Scroll to the bottom of page * From 3e6326a4bea7c7d440502e338db41735e212d1dd Mon Sep 17 00:00:00 2001 From: William Desportes Date: Tue, 25 Feb 2020 13:32:23 +0100 Subject: [PATCH 3/8] Stop webdriver session when the test failed Signed-off-by: William Desportes --- test/selenium/TestBase.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index d0306044f9..b12b8f986f 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -1056,6 +1056,10 @@ abstract class TestBase extends TestCase */ public function onNotSuccessfulTest(Throwable $t): void { + // End testing session + if ($this->webDriver !== null) { + $this->webDriver->quit(); + } $SESSION_REST_URL = 'https://api.browserstack.com/automate/sessions/'; // If this is being run on Browerstack, // mark the test on Browerstack as failure From e92badc3a6418024d282d0b1c98f6ac53fefb82f Mon Sep 17 00:00:00 2001 From: William Desportes Date: Thu, 27 Feb 2020 20:56:06 +0100 Subject: [PATCH 4/8] Fix/improve CreateDropDatabaseTest Signed-off-by: William Desportes --- test/selenium/CreateDropDatabaseTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/selenium/CreateDropDatabaseTest.php b/test/selenium/CreateDropDatabaseTest.php index 6f141a556a..8285e1198d 100644 --- a/test/selenium/CreateDropDatabaseTest.php +++ b/test/selenium/CreateDropDatabaseTest.php @@ -79,11 +79,16 @@ class CreateDropDatabaseTest extends TestBase $this->waitAjax(); $this->scrollToBottom(); - $this->byCssSelector( - "input[name='selected_dbs[]'][value='" . $this->database_name . "']" - )->click(); - $this->byCssSelector("button.mult_submit")->click(); + $dbElement = $this->byCssSelector( + "input[name='selected_dbs[]'][value='" . $this->database_name . "']" + ); + $this->scrollToElement($dbElement, 0, 20); + $dbElement->click(); + + $multSubmit = $this->byCssSelector("button.mult_submit"); + $this->scrollToElement($multSubmit); + $multSubmit->click(); $this->byCssSelector("button.submitOK")->click(); $this->waitForElementNotPresent( From 5e43765058f9c8269576f9ff37ee9db15afafe73 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Thu, 27 Feb 2020 21:15:52 +0100 Subject: [PATCH 5/8] Add access to console logs from chromedriver Add logging for chrome driver https://stackoverflow.com/a/53305034/5155484 Signed-off-by: William Desportes --- test/selenium/TestBase.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index b12b8f986f..4d26cf7015 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -318,6 +318,10 @@ abstract class TestBase extends TestCase ChromeOptions::CAPABILITY, $chromeOptions ); + $capabilities->setCapability( + 'loggingPrefs', + ['browser' => 'ALL'] + ); if ($this->hasCIConfig() && $this->hasBrowserstackConfig()) { $capabilities->setCapability( From 26d315b5e5736ad02650dd4fb458e2c5ba7a89e7 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Thu, 27 Feb 2020 22:17:22 +0100 Subject: [PATCH 6/8] Find an alternative to make scrolling work on testDropTable Ref: https://github.com/php-webdriver/php-webdriver/issues/773 Signed-off-by: William Desportes --- test/selenium/Table/OperationsTest.php | 7 ++++--- test/selenium/TestBase.php | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/test/selenium/Table/OperationsTest.php b/test/selenium/Table/OperationsTest.php index 2cbd1e93f5..77354a9a84 100644 --- a/test/selenium/Table/OperationsTest.php +++ b/test/selenium/Table/OperationsTest.php @@ -231,9 +231,10 @@ class OperationsTest extends TestBase */ public function testDropTable() { - $this->scrollToBottom(); - $this->waitUntilElementIsVisible('id', 'drop_tbl_anchor', 30); - $this->byId("drop_tbl_anchor")->click(); + $this->reloadPage(); + $dropLink = $this->waitUntilElementIsVisible('partialLinkText', 'Delete the table (DROP)', 30); + $this->scrollToElement($this->byId('selflink')); + $dropLink->click(); $this->byCssSelector("button.submitOK")->click(); $this->waitAjax(); diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index 4d26cf7015..c4b59d5f8f 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -995,13 +995,25 @@ abstract class TestBase extends TestCase * * @return void */ - public function scrollToBottom() + public function scrollToBottom(): void { $this->webDriver->executeScript( 'window.scrollTo(0,document.body.scrollHeight);' ); } + /** + * Reload the page + * + * @return void + */ + public function reloadPage(): void + { + $this->webDriver->executeScript( + 'window.location.reload();' + ); + } + /** * Wait for AJAX completion * From 0d3e6c1603e808da0cc8d6eb9d93d31077cdd404 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Thu, 27 Feb 2020 23:09:03 +0100 Subject: [PATCH 7/8] Fix phpdoc block Signed-off-by: William Desportes --- test/selenium/TestBase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index c4b59d5f8f..171f7a5e05 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -980,6 +980,8 @@ abstract class TestBase extends TestCase * Scrolls to a coordinate such that the element * * @param WebDriverElement $element The element + * @param int $xOffset The x offset to apply (defaults to 0) + * @param int $yOffset The y offset to apply (defaults to 0) * * @return void */ From 4c5fe772b9e1b8c09a58763d6104656a38d95e59 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Thu, 27 Feb 2020 23:51:28 +0100 Subject: [PATCH 8/8] Mark tests as passed on BrowserStack Signed-off-by: William Desportes --- test/selenium/TestBase.php | 93 ++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/test/selenium/TestBase.php b/test/selenium/TestBase.php index 171f7a5e05..94b9fc14d9 100644 --- a/test/selenium/TestBase.php +++ b/test/selenium/TestBase.php @@ -65,6 +65,8 @@ abstract class TestBase extends TestCase */ protected $sessionId; + private const SESSION_REST_URL = 'https://api.browserstack.com/automate/sessions/'; + /** * Configures the selenium and database link. * @@ -1061,10 +1063,59 @@ abstract class TestBase extends TestCase $this->_mysqli->close(); $this->_mysqli = null; } - + if (! $this->hasFailed()) { + $this->markTestAs('passed', ''); + } $this->webDriver->quit(); } + /** + * Mark test as failed or passed on BrowserStack + * + * @param string $status passed or failed + * @param string $message a message + * @return void + */ + private function markTestAs(string $status, string $message): void + { + // If this is being run on Browerstack, + // mark the test on Browerstack as failure + if ($this->hasBrowserstackConfig()) { + $payload = json_encode( + [ + 'status' => $status, + 'reason' => $message, + ] + ); + + $ch = curl_init(); + curl_setopt( + $ch, + CURLOPT_URL, + self::SESSION_REST_URL . $this->sessionId . '.json' + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt( + $ch, + CURLOPT_USERPWD, + $GLOBALS['TESTSUITE_BROWSERSTACK_USER'] + . ':' . $GLOBALS['TESTSUITE_BROWSERSTACK_KEY'] + ); + + $headers = []; + $headers[] = 'Content-Type: application/json'; + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error: ' . curl_error($ch) . PHP_EOL; + } + curl_close($ch); + } + } + /** * Mark unsuccessful tests as 'Failures' on Browerstack * @@ -1078,43 +1129,7 @@ abstract class TestBase extends TestCase if ($this->webDriver !== null) { $this->webDriver->quit(); } - $SESSION_REST_URL = 'https://api.browserstack.com/automate/sessions/'; - // If this is being run on Browerstack, - // mark the test on Browerstack as failure - if ($this->hasBrowserstackConfig()) { - $payload = json_encode( - [ - 'status' => 'failed', - 'reason' => $t->getMessage(), - ] - ); - - $ch = curl_init(); - curl_setopt( - $ch, - CURLOPT_URL, - $SESSION_REST_URL . $this->sessionId . ".json" - ); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt( - $ch, - CURLOPT_USERPWD, - $GLOBALS['TESTSUITE_BROWSERSTACK_USER'] - . ":" . $GLOBALS['TESTSUITE_BROWSERSTACK_KEY'] - ); - - $headers = []; - $headers[] = "Content-Type: application/json"; - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - - $result = curl_exec($ch); - if (curl_errno($ch)) { - echo 'Error: ' . curl_error($ch) . PHP_EOL; - } - curl_close($ch); - } + $this->markTestAs('failed', $t->getMessage()); if ($this->hasBrowserstackConfig()) { $ch = curl_init(); @@ -1122,7 +1137,7 @@ abstract class TestBase extends TestCase curl_setopt( $ch, CURLOPT_URL, - $SESSION_REST_URL . $this->sessionId . ".json" + self::SESSION_REST_URL . $this->sessionId . ".json" ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(