JSWebAssertTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Tests;
  3. use Behat\Mink\Element\NodeElement;
  4. use Behat\Mink\Exception\ElementHtmlException;
  5. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  6. /**
  7. * Tests for the JSWebAssert class.
  8. *
  9. * @group javascript
  10. */
  11. class JSWebAssertTest extends WebDriverTestBase {
  12. /**
  13. * Required modules.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['js_webassert_test'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected $defaultTheme = 'stark';
  22. /**
  23. * Tests that JSWebAssert assertions work correctly.
  24. */
  25. public function testJsWebAssert() {
  26. $this->drupalGet('js_webassert_test_form');
  27. $session = $this->getSession();
  28. $assert_session = $this->assertSession();
  29. $page = $session->getPage();
  30. $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]');
  31. $page->findButton('Test assertNoElementAfterWait: pass')->press();
  32. $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]', 1000);
  33. $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]');
  34. $page->findButton('Test assertNoElementAfterWait: fail')->press();
  35. try {
  36. $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 500, 'Element exists on page after too short wait.');
  37. $this->fail('Element not exists on page after too short wait.');
  38. }
  39. catch (ElementHtmlException $e) {
  40. $this->assertSame('Element exists on page after too short wait.', $e->getMessage());
  41. }
  42. $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 2500, 'Element remove after another wait.ss');
  43. $test_button = $page->findButton('Add button');
  44. $test_link = $page->findButton('Add link');
  45. $test_field = $page->findButton('Add field');
  46. $test_id = $page->findButton('Add ID');
  47. $test_wait_on_ajax = $page->findButton('Test assertWaitOnAjaxRequest');
  48. $test_wait_on_element_visible = $page->findButton('Test waitForElementVisible');
  49. // Test the wait...() methods by first checking the fields aren't available
  50. // and then are available after the wait method.
  51. $result = $page->findButton('Added button');
  52. $this->assertEmpty($result);
  53. $test_button->click();
  54. $result = $assert_session->waitForButton('Added button');
  55. $this->assertNotEmpty($result);
  56. $this->assertInstanceOf(NodeElement::class, $result);
  57. $result = $page->findLink('Added link');
  58. $this->assertEmpty($result);
  59. $test_link->click();
  60. $result = $assert_session->waitForLink('Added link');
  61. $this->assertNotEmpty($result);
  62. $this->assertInstanceOf(NodeElement::class, $result);
  63. $result = $page->findField('added_field');
  64. $this->assertEmpty($result);
  65. $test_field->click();
  66. $result = $assert_session->waitForField('added_field');
  67. $this->assertNotEmpty($result);
  68. $this->assertInstanceOf(NodeElement::class, $result);
  69. $result = $page->findById('js_webassert_test_field_id');
  70. $this->assertEmpty($result);
  71. $test_id->click();
  72. $result = $assert_session->waitForId('js_webassert_test_field_id');
  73. $this->assertNotEmpty($result);
  74. $this->assertInstanceOf(NodeElement::class, $result);
  75. // Test waitOnAjaxRequest. Verify the element is available after the wait
  76. // and the behaviors have run on completing by checking the value.
  77. $result = $page->findField('test_assert_wait_on_ajax_input');
  78. $this->assertEmpty($result);
  79. $test_wait_on_ajax->click();
  80. $assert_session->assertWaitOnAjaxRequest();
  81. $result = $page->findField('test_assert_wait_on_ajax_input');
  82. $this->assertNotEmpty($result);
  83. $this->assertInstanceOf(NodeElement::class, $result);
  84. $this->assertEquals('js_webassert_test', $result->getValue());
  85. $result = $page->findButton('Added WaitForElementVisible');
  86. $this->assertEmpty($result);
  87. $test_wait_on_element_visible->click();
  88. $result = $assert_session->waitForElementVisible('named', ['button', 'Added WaitForElementVisible']);
  89. $this->assertNotEmpty($result);
  90. $this->assertInstanceOf(NodeElement::class, $result);
  91. $this->assertEquals(TRUE, $result->isVisible());
  92. }
  93. }