Commit 9a28c1a7 authored by catch's avatar catch
Browse files

Issue #3174928 by alexpott, andypost: Improve the stability of core JS testing...

Issue #3174928 by alexpott, andypost: Improve the stability of core JS testing and prepare for update of MinkSelenium2Driver
parent 98fedb21
......@@ -82,7 +82,7 @@ public function testAddAjaxBlock() {
// Find the radio buttons.
$name = 'settings[ajax_test]';
/** @var \Behat\Mink\Element\NodeElement[] $radios */
$radios = $this->cssSelect('input[name="' . $name . '"]');
$radios = $this->assertSession()->fieldExists($name);
// Click them both a couple of times.
foreach ([1, 2] as $rounds) {
foreach ($radios as $radio) {
......@@ -91,7 +91,7 @@ public function testAddAjaxBlock() {
}
}
// Then add the block.
$page->pressButton('Add block');
$assert_session->waitForElementVisible('named', ['button', 'Add block'])->press();
$assert_session->assertWaitOnAjaxRequest();
$block_elements = $this->cssSelect('.block-layout-builder-test-testajax');
// Should be exactly one of these in there.
......
......@@ -141,10 +141,10 @@ protected function addFormBlock($label, $path, $expected_save_message) {
// Add the form block.
$assert_session->linkExists('Add block');
$this->clickLink('Add block');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['link', $label]);
$assert_session->linkExists($label);
$this->clickLink($label);
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['button', 'Add block']);
$page->pressButton('Add block');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextContains($label);
......
......@@ -102,11 +102,11 @@ public function testQuickEditIgnoresDuplicateFields() {
$this->drupalGet('admin/structure/types/manage/article/display/default/layout');
$page->clickLink('Add block');
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['link', 'Body']);
$page->clickLink('Body');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['button', 'Add block']);
$page->pressButton('Add block');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['button', 'Save layout']);
$page->pressButton('Save layout');
$this->assertNotEmpty($assert_session->waitForElement('css', '.messages--status'));
$assert_session->pageTextContains('The layout has been saved.');
......
......@@ -160,7 +160,7 @@ public function testLayoutBuilderUi() {
$this->assertNotEmpty($assert_session->waitForElementVisible('named', ['link', 'Two column']));
$this->clickLink('Two column');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['button', 'Add section']);
$page->pressButton('Add section');
$assert_session->assertWaitOnAjaxRequest();
......@@ -293,7 +293,7 @@ public function testConfigurableLayoutSections() {
// Add another section.
$assert_session->linkExists('Add section');
$this->clickLink('Add section');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['link', 'Layout plugin (with settings)']);
$assert_session->elementExists('css', '#drupal-off-canvas');
$assert_session->linkExists('Layout plugin (with settings)');
......@@ -301,7 +301,6 @@ public function testConfigurableLayoutSections() {
$this->assertOffCanvasFormAfterWait('layout_builder_configure_section');
$assert_session->fieldExists('layout_settings[setting_1]');
$page->pressButton('Add section');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert_session->pageTextContains('Default');
......@@ -313,7 +312,6 @@ public function testConfigurableLayoutSections() {
$this->assertOffCanvasFormAfterWait('layout_builder_configure_section');
$page->fillField('layout_settings[setting_1]', 'Test setting value');
$page->pressButton('Update');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert_session->pageTextContains('Test setting value');
$this->assertPageNotReloaded();
......
......@@ -115,10 +115,10 @@ protected function assertModifiedLayout($path) {
$this->drupalGet($path);
$page->clickLink('Add section');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['link', 'One column']);
$assert_session->pageTextNotContains('You have unsaved changes.');
$page->clickLink('One column');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->waitForElementVisible('named', ['button', 'Add section']);
$page->pressButton('Add section');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextContainsOnce('You have unsaved changes.');
......
......@@ -652,6 +652,8 @@ public function testAlt() {
$this->waitForEditor();
$this->assignNameToCkeditorIframe();
$this->getSession()->switchToIFrame('ckeditor');
// Wait for the media preview to load.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media img'));
// Test that by default no alt attribute is present on the drupal-media
// element.
$this->pressEditorButton('source');
......
......@@ -13,7 +13,7 @@ abstract class MediaLibraryTestBase extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['media_library_test'];
protected static $modules = ['media_library_test', 'hold_test'];
/**
* {@inheritdoc}
......@@ -416,9 +416,11 @@ protected function switchToMediaLibraryGrid() {
* Switches to the table display of the widget view.
*/
protected function switchToMediaLibraryTable() {
hold_test_response(TRUE);
$this->getSession()->getPage()->clickLink('Table');
// Assert the display change is correctly announced for screen readers.
$this->waitForText('Loading table view.');
hold_test_response(FALSE);
$this->waitForText('Changed to table view.');
$this->assertMediaLibraryTable();
}
......
......@@ -311,8 +311,8 @@ public function testWidgetUpload() {
// Remove the second file and assert the focus is shifted to the container
// of the next media item and field values are still correct.
$page->pressButton('media-1-remove-button');
$this->assertTrue($assert_session->waitForText('The media item ' . $filenames[1] . ' has been removed.'));
$this->assertJsCondition('jQuery("[data-media-library-added-delta=2]").is(":focus")');
$assert_session->pageTextContains('The media item ' . $filenames[1] . ' has been removed.');
// Assert the file was deleted.
$this->assertEmpty($file_storage->loadByProperties(['filename' => $filenames[1]]));
$this->assertFileNotExists($file_1_uri);
......@@ -582,6 +582,7 @@ public function testWidgetUploadAdvancedUi() {
// a file.
$this->getSession()->executeScript("jQuery('.js-media-library-add-form-current-selection').val('1,2,{$unpublished_media->id()}')");
$this->addMediaFileToField('Add files', $this->container->get('file_system')->realpath($png_uri_3));
$this->assertMediaAdded();
// Assert the pre-selected items are shown.
$this->getSelectionArea();
// Assert the published items are selected and the unpublished item is not
......@@ -602,9 +603,7 @@ public function testWidgetUploadAdvancedUi() {
$this->assertTrue($assert_session->fieldExists('Add files')->hasAttribute('multiple'));
$png_uri_5 = $file_system->copy($png_image->uri, 'public://');
$this->addMediaFileToField('Add files', $this->container->get('file_system')->realpath($png_uri_5));
// assertWaitOnAjaxRequest() required for input "id" attributes to
// consistently match their label's "for" attribute.
$assert_session->assertWaitOnAjaxRequest();
$this->assertMediaAdded();
$page->fillField('Alternative text', $this->randomString());
// Assert the pre-selected items are shown.
$selection_area = $this->getSelectionArea();
......@@ -723,6 +722,7 @@ public function testWidgetUploadAdvancedUi() {
// Remove the last file and assert the focus is shifted to the container
// of the first media item and field values are still correct.
$page->pressButton('media-2-remove-button');
$this->assertTrue($assert_session->waitForText('The media item ' . $filenames[2] . ' has been removed.'));
$this->assertJsCondition('jQuery("[data-media-library-added-delta=0]").is(":focus")');
$assert_session->pageTextContains('The media item ' . $filenames[2] . ' has been removed.');
$assert_session->elementNotExists('css', '[data-media-library-added-delta=1]');
......
......@@ -157,7 +157,6 @@ public function testUserPermissions() {
$this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
// Switch to body field.
$page->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')->click();
$assert->assertWaitOnAjaxRequest();
// Wait and update body field.
$body_field_locator = '[name="body[0][value]"]';
......@@ -166,12 +165,7 @@ public function testUserPermissions() {
// Wait and click by "Save" button after body field was changed.
$assert->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
$assert->assertWaitOnAjaxRequest();
$node = Node::load($nid);
$vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
$this->assertCount(1, $vids, 'The node has only one revision.');
$this->assertSame($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
$assert->waitForElementRemoved('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]');
// Ensure that the changes take effect.
$assert->responseMatches("|\s*$body_text\s*|");
......@@ -179,6 +173,12 @@ public function testUserPermissions() {
// Reload the page and check for updated body.
$this->drupalGet('node/' . $nid);
$assert->pageTextContains($body_text);
// Ensure that a new revision has not been created.
$node = Node::load($nid);
$vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
$this->assertCount(1, $vids, 'The node has only one revision.');
$this->assertSame($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
}
/**
......@@ -228,7 +228,6 @@ public function testTitleBaseField() {
$this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
// Switch to title field.
$page->find('css', '[data-quickedit-field-id="node/' . $nid . '/title/en/full"]')->click();
$assert->assertWaitOnAjaxRequest();
// Wait and update title field.
$field_locator = '.field--name-title';
......@@ -237,7 +236,7 @@ public function testTitleBaseField() {
// Wait and click by "Save" button after title field was changed.
$this->assertSession()->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
$assert->assertWaitOnAjaxRequest();
$assert->waitForElementRemoved('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]');
// Ensure that the changes take effect.
$assert->responseMatches("|\s*$text_new\s*|");
......@@ -294,7 +293,6 @@ public function testConcurrentEdit() {
$this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
// Switch to body field.
$page->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')->click();
$assert->assertWaitOnAjaxRequest();
// Wait and update body field.
$body_field_locator = '[name="body[0][value]"]';
......@@ -303,7 +301,7 @@ public function testConcurrentEdit() {
// Wait and click by "Save" button after body field was changed.
$assert->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
$assert->assertWaitOnAjaxRequest();
$assert->waitForElementRemoved('css', $body_field_locator);
// Ensure that the changes take effect.
$assert->responseMatches("|\s*$body_text\s*|");
......@@ -388,7 +386,6 @@ public function testImageField() {
$this->clickContextualLink('[data-quickedit-entity-id="node/1"]', 'Quick edit');
// Switch to body field.
$assert->waitForElement('css', '[data-quickedit-field-id="node/1/field_image/en/full"]')->click();
$assert->assertWaitOnAjaxRequest();
$field_locator = '.field--name-field-image';
$assert->waitForElementVisible('css', $field_locator);
......
......@@ -26,6 +26,8 @@ class JSWebAssert extends WebAssert {
* be displayed.
*/
public function assertWaitOnAjaxRequest($timeout = 10000, $message = 'Unable to complete AJAX request.') {
// Wait for a very short time to allow page state to update after clicking.
usleep(5000);
$condition = <<<JS
(function() {
function isAjaxing(instance) {
......
......@@ -4,6 +4,7 @@
use Behat\Mink\Exception\DriverException;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Runner\BaseTestRunner;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -81,6 +82,11 @@ protected function initFrontPage() {
*/
protected function tearDown() {
if ($this->mink) {
$status = $this->getStatus();
if ($status === BaseTestRunner::STATUS_ERROR || $status === BaseTestRunner::STATUS_WARNING || $status === BaseTestRunner::STATUS_FAILURE) {
// Ensure we capture the output at point of failure.
@$this->htmlOutput();
}
// Wait for all requests to finish. It is possible that an AJAX request is
// still on-going.
$result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment