|
| 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 | +} |
0 commit comments