WebDriver.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Codeception\PHPUnit\Constraint;
  3. use Codeception\Exception\ElementNotFound;
  4. use Codeception\Lib\Console\Message;
  5. use Codeception\Util\Locator;
  6. use SebastianBergmann\Comparator\ComparisonFailure;
  7. class WebDriver extends Page
  8. {
  9. protected function matches($nodes)
  10. {
  11. if (!count($nodes)) {
  12. return false;
  13. }
  14. if ($this->string === '') {
  15. return true;
  16. }
  17. foreach ($nodes as $node) {
  18. /** @var $node \WebDriverElement * */
  19. if (!$node->isDisplayed()) {
  20. continue;
  21. }
  22. if (parent::matches(htmlspecialchars_decode($node->getText()))) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null)
  29. {
  30. if (!count($nodes)) {
  31. throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath');
  32. }
  33. $output = "Failed asserting that any element by " . Locator::humanReadableString($selector);
  34. $output .= $this->uriMessage('on page');
  35. if (count($nodes) < 5) {
  36. $output .= "\nElements: ";
  37. $output .= $this->nodesList($nodes);
  38. } else {
  39. $message = new Message("[total %s elements]");
  40. $output .= $message->with(count($nodes));
  41. }
  42. $output .= "\ncontains text '" . $this->string . "'";
  43. throw new \PHPUnit\Framework\ExpectationFailedException(
  44. $output,
  45. $comparisonFailure
  46. );
  47. }
  48. protected function failureDescription($nodes)
  49. {
  50. $desc = '';
  51. foreach ($nodes as $node) {
  52. $desc .= parent::failureDescription($node->getText());
  53. }
  54. return $desc;
  55. }
  56. protected function nodesList($nodes, $contains = null)
  57. {
  58. $output = "";
  59. foreach ($nodes as $node) {
  60. if ($contains && strpos($node->getText(), $contains) === false) {
  61. continue;
  62. }
  63. /** @var $node \WebDriverElement * */
  64. $message = new Message("\n+ <%s> %s");
  65. $output .= $message->with($node->getTagName(), $node->getText());
  66. }
  67. return $output;
  68. }
  69. }