From af7f94361b3eea851c703217f86e70265e157816 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Fri, 11 Mar 2022 10:41:21 +0200 Subject: [PATCH] Use lib-web, add WebDriver constraints --- composer.json | 3 +- src/Codeception/Constraint/WebDriver.php | 105 ++++++++++++++++++ src/Codeception/Constraint/WebDriverNot.php | 58 ++++++++++ .../Lib/Interfaces/ScreenshotSaver.php | 16 --- .../Lib/Interfaces/SessionSnapshot.php | 55 --------- src/Codeception/Module/WebDriver.php | 4 +- 6 files changed, 167 insertions(+), 74 deletions(-) create mode 100644 src/Codeception/Constraint/WebDriver.php create mode 100644 src/Codeception/Constraint/WebDriverNot.php delete mode 100644 src/Codeception/Lib/Interfaces/ScreenshotSaver.php delete mode 100644 src/Codeception/Lib/Interfaces/SessionSnapshot.php diff --git a/composer.json b/composer.json index 93b00e2..7d15142 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "php": "^8.0", "ext-json": "*", "ext-mbstring": "*", - "codeception/codeception": "^5.0.0-alpha2", + "codeception/codeception": "^5.0.0-alpha3", + "codeception/lib-web": "^1.0", "codeception/stub": "^4.0", "php-webdriver/webdriver": "^1.8.0" }, diff --git a/src/Codeception/Constraint/WebDriver.php b/src/Codeception/Constraint/WebDriver.php new file mode 100644 index 0000000..18a4ee3 --- /dev/null +++ b/src/Codeception/Constraint/WebDriver.php @@ -0,0 +1,105 @@ +string === '') { + return true; + } + + foreach ($nodes as $node) { + if (!$node->isDisplayed()) { + continue; + } + if (parent::matches(htmlspecialchars_decode($node->getText(), ENT_QUOTES | ENT_SUBSTITUTE))) { + return true; + } + } + return false; + } + + /** + * @param WebDriverElement[] $nodes + * @param string|array|WebDriverBy $selector + * @param ComparisonFailure|null $comparisonFailure + */ + protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void + { + if (count($nodes) === 0) { + throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath'); + } + + $output = "Failed asserting that any element by " . Locator::humanReadableString($selector); + $output .= $this->uriMessage('on page'); + + if (count($nodes) < 5) { + $output .= "\nElements: "; + $output .= $this->nodesList($nodes); + } else { + $message = new Message("[total %s elements]"); + $output .= $message->with(count($nodes)); + } + $output .= "\ncontains text '" . $this->string . "'"; + + throw new ExpectationFailedException( + $output, + $comparisonFailure + ); + } + + /** + * @param WebDriverElement[] $nodes + * @return string + */ + protected function failureDescription($nodes): string + { + $desc = ''; + foreach ($nodes as $node) { + $desc .= parent::failureDescription($node->getText()); + } + return $desc; + } + + /** + * @param WebDriverElement[] $nodes + * @param string|null $contains + * @return string + */ + protected function nodesList(array $nodes, string $contains = null): string + { + $output = ""; + foreach ($nodes as $node) { + if ($contains && strpos($node->getText(), $contains) === false) { + continue; + } + $message = new Message("\n+ <%s> %s"); + $output .= $message->with($node->getTagName(), $node->getText()); + } + return $output; + } +} diff --git a/src/Codeception/Constraint/WebDriverNot.php b/src/Codeception/Constraint/WebDriverNot.php new file mode 100644 index 0000000..da25e37 --- /dev/null +++ b/src/Codeception/Constraint/WebDriverNot.php @@ -0,0 +1,58 @@ +string) { + throw new ExpectationFailedException( + "Element {$selector} was found", + $comparisonFailure + ); + } + + $output = "There was {$selector} element"; + $output .= $this->uriMessage("on page"); + $output .= $this->nodesList($nodes, $this->string); + $output .= "\ncontaining '{$this->string}'"; + + throw new ExpectationFailedException( + $output, + $comparisonFailure + ); + } + + public function toString(): string + { + if ($this->string) { + return 'that contains text "' . $this->string . '"'; + } + return ''; + } +} diff --git a/src/Codeception/Lib/Interfaces/ScreenshotSaver.php b/src/Codeception/Lib/Interfaces/ScreenshotSaver.php deleted file mode 100644 index 2c91f8f..0000000 --- a/src/Codeception/Lib/Interfaces/ScreenshotSaver.php +++ /dev/null @@ -1,16 +0,0 @@ -getModule('{{MODULE_NAME}}')->_saveScreenshot(codecept_output_dir().'screenshot_1.png'); - * ``` - * @api - */ - public function _saveScreenshot(string $filename); -} diff --git a/src/Codeception/Lib/Interfaces/SessionSnapshot.php b/src/Codeception/Lib/Interfaces/SessionSnapshot.php deleted file mode 100644 index 74617d9..0000000 --- a/src/Codeception/Lib/Interfaces/SessionSnapshot.php +++ /dev/null @@ -1,55 +0,0 @@ -loadSessionSnapshot('login')) return; - * - * // logging in - * $I->amOnPage('/login'); - * $I->fillField('name', 'jon'); - * $I->fillField('password', '123345'); - * $I->click('Login'); - * - * // saving snapshot - * $I->saveSessionSnapshot('login'); - * } - * ``` - * - * @return mixed - */ - public function saveSessionSnapshot(string $name); - - /** - * Loads cookies from a saved snapshot. - * Allows to reuse same session across tests without additional login. - * - * See [saveSessionSnapshot](#saveSessionSnapshot) - * - * @return mixed - */ - public function loadSessionSnapshot(string $name); - - /** - * Deletes session snapshot. - * - * See [saveSessionSnapshot](#saveSessionSnapshot) - * - * @return mixed - */ - public function deleteSessionSnapshot(string $name); -} diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 76b28db..194055e 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -23,8 +23,8 @@ use Codeception\Lib\Interfaces\Web as WebInterface; use Codeception\Module as CodeceptionModule; use Codeception\PHPUnit\Constraint\Page as PageConstraint; -use Codeception\PHPUnit\Constraint\WebDriver as WebDriverConstraint; -use Codeception\PHPUnit\Constraint\WebDriverNot as WebDriverConstraintNot; +use Codeception\Constraint\WebDriver as WebDriverConstraint; +use Codeception\Constraint\WebDriverNot as WebDriverConstraintNot; use Codeception\Test\Descriptor; use Codeception\Test\Interfaces\ScenarioDriven; use Codeception\TestInterface;