Skip to content

Extract constraints from codeception/codeception #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
105 changes: 105 additions & 0 deletions src/Codeception/Constraint/WebDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Codeception\Constraint;

use Codeception\Exception\ElementNotFound;
use Codeception\Lib\Console\Message;
use Codeception\PHPUnit\Constraint\Page;
use Codeception\Util\Locator;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Comparator\ComparisonFailure;

use function count;
use function htmlspecialchars_decode;
use function strpos;

class WebDriver extends Page
{
/**
* @param WebDriverElement[] $nodes
* @return bool
*/
protected function matches($nodes): bool
{
if (count($nodes) === 0) {
return false;
}
if ($this->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;
}
}
58 changes: 58 additions & 0 deletions src/Codeception/Constraint/WebDriverNot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Codeception\Constraint;

use Codeception\Util\Locator;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Comparator\ComparisonFailure;

use function is_string;
use function strpos;

class WebDriverNot extends WebDriver
{
protected function matches($nodes): bool
{
return !parent::matches($nodes);
}

/**
* @param WebDriverElement[] $nodes
* @param string|array|WebDriverBy $selector
* @param ComparisonFailure|null $comparisonFailure
*/
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void
{
if (!is_string($selector) || strpos($selector, "'") === false) {
$selector = Locator::humanReadableString($selector);
}
if (!$this->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 '';
}
}
16 changes: 0 additions & 16 deletions src/Codeception/Lib/Interfaces/ScreenshotSaver.php

This file was deleted.

55 changes: 0 additions & 55 deletions src/Codeception/Lib/Interfaces/SessionSnapshot.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down