WebDriverConstraintTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. require_once __DIR__.'/mocked_webelement.php';
  3. class WebDriverConstraintTest extends \PHPUnit\Framework\TestCase
  4. {
  5. /**
  6. * @var Codeception\PHPUnit\Constraint\WebDriver
  7. */
  8. protected $constraint;
  9. public function setUp()
  10. {
  11. $this->constraint = new Codeception\PHPUnit\Constraint\WebDriver('hello', '/user');
  12. }
  13. public function testEvaluation()
  14. {
  15. $nodes = array(new TestedWebElement('Hello world'), new TestedWebElement('Bye world'));
  16. $this->constraint->evaluate($nodes);
  17. }
  18. public function testFailMessageResponseWithStringSelector()
  19. {
  20. $nodes = array(new TestedWebElement('Bye warcraft'), new TestedWebElement('Bye world'));
  21. try {
  22. $this->constraint->evaluate($nodes, 'selector');
  23. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  24. $this->assertContains(
  25. "Failed asserting that any element by 'selector' on page /user",
  26. $fail->getMessage()
  27. );
  28. $this->assertContains('+ <p> Bye world', $fail->getMessage());
  29. $this->assertContains('+ <p> Bye warcraft', $fail->getMessage());
  30. return;
  31. }
  32. $this->fail("should have failed, but not");
  33. }
  34. public function testFailMessageResponseWithArraySelector()
  35. {
  36. $nodes = array(new TestedWebElement('Bye warcraft'));
  37. try {
  38. $this->constraint->evaluate($nodes, ['css' => 'p.mocked']);
  39. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  40. $this->assertContains(
  41. "Failed asserting that any element by css 'p.mocked' on page /user",
  42. $fail->getMessage()
  43. );
  44. $this->assertContains('+ <p> Bye warcraft', $fail->getMessage());
  45. return;
  46. }
  47. $this->fail("should have failed, but not");
  48. }
  49. public function testFailMessageResponseWhenMoreNodes()
  50. {
  51. $nodes = array();
  52. for ($i = 0; $i < 15; $i++) {
  53. $nodes[] = new TestedWebElement("item $i");
  54. }
  55. try {
  56. $this->constraint->evaluate($nodes, 'selector');
  57. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  58. $this->assertContains(
  59. "Failed asserting that any element by 'selector' on page /user",
  60. $fail->getMessage()
  61. );
  62. $this->assertNotContains('+ <p> item 0', $fail->getMessage());
  63. $this->assertNotContains('+ <p> item 14', $fail->getMessage());
  64. $this->assertContains('[total 15 elements]', $fail->getMessage());
  65. return;
  66. }
  67. $this->fail("should have failed, but not");
  68. }
  69. public function testFailMessageResponseWithoutUrl()
  70. {
  71. $this->constraint = new Codeception\PHPUnit\Constraint\WebDriver('hello');
  72. $nodes = array(new TestedWebElement('Bye warcraft'), new TestedWebElement('Bye world'));
  73. try {
  74. $this->constraint->evaluate($nodes, 'selector');
  75. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  76. $this->assertContains("Failed asserting that any element by 'selector'", $fail->getMessage());
  77. $this->assertNotContains("Failed asserting that any element by 'selector' on page", $fail->getMessage());
  78. return;
  79. }
  80. $this->fail("should have failed, but not");
  81. }
  82. }