Skip to content

Commit 3346943

Browse files
authored
Merge pull request #93 from Codeception/move-constraints-from-codeception
Extract constraints from codeception/codeception
2 parents 3d81a96 + af7f943 commit 3346943

File tree

6 files changed

+167
-74
lines changed

6 files changed

+167
-74
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"php": "^8.0",
2323
"ext-json": "*",
2424
"ext-mbstring": "*",
25-
"codeception/codeception": "^5.0.0-alpha2",
25+
"codeception/codeception": "^5.0.0-alpha3",
26+
"codeception/lib-web": "^1.0",
2627
"codeception/stub": "^4.0",
2728
"php-webdriver/webdriver": "^1.8.0"
2829
},
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Constraint;
6+
7+
use Codeception\Exception\ElementNotFound;
8+
use Codeception\Lib\Console\Message;
9+
use Codeception\PHPUnit\Constraint\Page;
10+
use Codeception\Util\Locator;
11+
use Facebook\WebDriver\WebDriverBy;
12+
use Facebook\WebDriver\WebDriverElement;
13+
use PHPUnit\Framework\ExpectationFailedException;
14+
use SebastianBergmann\Comparator\ComparisonFailure;
15+
16+
use function count;
17+
use function htmlspecialchars_decode;
18+
use function strpos;
19+
20+
class WebDriver extends Page
21+
{
22+
/**
23+
* @param WebDriverElement[] $nodes
24+
* @return bool
25+
*/
26+
protected function matches($nodes): bool
27+
{
28+
if (count($nodes) === 0) {
29+
return false;
30+
}
31+
if ($this->string === '') {
32+
return true;
33+
}
34+
35+
foreach ($nodes as $node) {
36+
if (!$node->isDisplayed()) {
37+
continue;
38+
}
39+
if (parent::matches(htmlspecialchars_decode($node->getText(), ENT_QUOTES | ENT_SUBSTITUTE))) {
40+
return true;
41+
}
42+
}
43+
return false;
44+
}
45+
46+
/**
47+
* @param WebDriverElement[] $nodes
48+
* @param string|array|WebDriverBy $selector
49+
* @param ComparisonFailure|null $comparisonFailure
50+
*/
51+
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void
52+
{
53+
if (count($nodes) === 0) {
54+
throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath');
55+
}
56+
57+
$output = "Failed asserting that any element by " . Locator::humanReadableString($selector);
58+
$output .= $this->uriMessage('on page');
59+
60+
if (count($nodes) < 5) {
61+
$output .= "\nElements: ";
62+
$output .= $this->nodesList($nodes);
63+
} else {
64+
$message = new Message("[total %s elements]");
65+
$output .= $message->with(count($nodes));
66+
}
67+
$output .= "\ncontains text '" . $this->string . "'";
68+
69+
throw new ExpectationFailedException(
70+
$output,
71+
$comparisonFailure
72+
);
73+
}
74+
75+
/**
76+
* @param WebDriverElement[] $nodes
77+
* @return string
78+
*/
79+
protected function failureDescription($nodes): string
80+
{
81+
$desc = '';
82+
foreach ($nodes as $node) {
83+
$desc .= parent::failureDescription($node->getText());
84+
}
85+
return $desc;
86+
}
87+
88+
/**
89+
* @param WebDriverElement[] $nodes
90+
* @param string|null $contains
91+
* @return string
92+
*/
93+
protected function nodesList(array $nodes, string $contains = null): string
94+
{
95+
$output = "";
96+
foreach ($nodes as $node) {
97+
if ($contains && strpos($node->getText(), $contains) === false) {
98+
continue;
99+
}
100+
$message = new Message("\n+ <%s> %s");
101+
$output .= $message->with($node->getTagName(), $node->getText());
102+
}
103+
return $output;
104+
}
105+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Constraint;
6+
7+
use Codeception\Util\Locator;
8+
use Facebook\WebDriver\WebDriverBy;
9+
use Facebook\WebDriver\WebDriverElement;
10+
use PHPUnit\Framework\ExpectationFailedException;
11+
use SebastianBergmann\Comparator\ComparisonFailure;
12+
13+
use function is_string;
14+
use function strpos;
15+
16+
class WebDriverNot extends WebDriver
17+
{
18+
protected function matches($nodes): bool
19+
{
20+
return !parent::matches($nodes);
21+
}
22+
23+
/**
24+
* @param WebDriverElement[] $nodes
25+
* @param string|array|WebDriverBy $selector
26+
* @param ComparisonFailure|null $comparisonFailure
27+
*/
28+
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void
29+
{
30+
if (!is_string($selector) || strpos($selector, "'") === false) {
31+
$selector = Locator::humanReadableString($selector);
32+
}
33+
if (!$this->string) {
34+
throw new ExpectationFailedException(
35+
"Element {$selector} was found",
36+
$comparisonFailure
37+
);
38+
}
39+
40+
$output = "There was {$selector} element";
41+
$output .= $this->uriMessage("on page");
42+
$output .= $this->nodesList($nodes, $this->string);
43+
$output .= "\ncontaining '{$this->string}'";
44+
45+
throw new ExpectationFailedException(
46+
$output,
47+
$comparisonFailure
48+
);
49+
}
50+
51+
public function toString(): string
52+
{
53+
if ($this->string) {
54+
return 'that contains text "' . $this->string . '"';
55+
}
56+
return '';
57+
}
58+
}

src/Codeception/Lib/Interfaces/ScreenshotSaver.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Codeception/Lib/Interfaces/SessionSnapshot.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Codeception/Module/WebDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
use Codeception\Lib\Interfaces\Web as WebInterface;
2424
use Codeception\Module as CodeceptionModule;
2525
use Codeception\PHPUnit\Constraint\Page as PageConstraint;
26-
use Codeception\PHPUnit\Constraint\WebDriver as WebDriverConstraint;
27-
use Codeception\PHPUnit\Constraint\WebDriverNot as WebDriverConstraintNot;
26+
use Codeception\Constraint\WebDriver as WebDriverConstraint;
27+
use Codeception\Constraint\WebDriverNot as WebDriverConstraintNot;
2828
use Codeception\Test\Descriptor;
2929
use Codeception\Test\Interfaces\ScenarioDriven;
3030
use Codeception\TestInterface;

0 commit comments

Comments
 (0)