From 13c5855165f0cac1fd2ba4141afe3488ab97f5dc Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Mon, 17 May 2021 20:04:19 +0100 Subject: [PATCH] Issue #3211838 by mondrake, longwave: Convert assertions involving use of xpath on spans to WebAssert --- .../tests/src/Functional/NodeTitleTest.php | 4 +-- .../tests/src/Functional/NodeViewTest.php | 4 +-- .../Functional/Form/ElementsLabelsTest.php | 17 +++++----- .../Functional/Plugin/DisplayPageWebTest.php | 34 ++++++++++--------- .../Installer/InstallerTest.php | 5 +-- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/core/modules/node/tests/src/Functional/NodeTitleTest.php b/core/modules/node/tests/src/Functional/NodeTitleTest.php index 4f048e638394..ddd939c5799e 100644 --- a/core/modules/node/tests/src/Functional/NodeTitleTest.php +++ b/core/modules/node/tests/src/Functional/NodeTitleTest.php @@ -74,8 +74,8 @@ public function testNodeTitle() { $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node'); - // Test node title in comment preview. - $this->assertEqual($this->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]/h2/a/span', [':node-class' => ' node--type-' . $node->bundle() . ' '])[0]->getText(), $node->label(), 'Node preview title is equal to node title.', 'Node'); + // Verify that node preview title is equal to node title. + $this->assertSession()->elementTextEquals('xpath', "//article[contains(concat(' ', normalize-space(@class), ' '), ' node--type-{$node->bundle()} ')]/h2/a/span", $node->label()); // Test node title is clickable on teaser list (/node). $this->drupalGet('node'); diff --git a/core/modules/node/tests/src/Functional/NodeViewTest.php b/core/modules/node/tests/src/Functional/NodeViewTest.php index 730212e1e741..c063cc9a80ac 100644 --- a/core/modules/node/tests/src/Functional/NodeViewTest.php +++ b/core/modules/node/tests/src/Functional/NodeViewTest.php @@ -98,8 +98,8 @@ public function testMultiByteUtf8() { $this->assertLessThan(strlen($title), mb_strlen($title, 'utf-8')); $node = $this->drupalCreateNode(['title' => $title]); $this->drupalGet($node->toUrl()); - $result = $this->xpath('//span[contains(@class, "field--name-title")]'); - $this->assertEqual($title, $result[0]->getText(), 'The passed title was returned.'); + // Verify that the passed title was returned. + $this->assertSession()->elementTextEquals('xpath', '//span[contains(@class, "field--name-title")]', $title); } } diff --git a/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php b/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php index 093f41ab1469..973be481350c 100644 --- a/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php +++ b/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php @@ -65,11 +65,8 @@ public function testFormLabels() { $this->assertSession()->elementExists('xpath', '//div[contains(@class, "js-form-item-form-textfield-test-title-invisible") and contains(@class, "form-no-label")]'); // Check #field_prefix and #field_suffix placement. - $elements = $this->xpath('//span[@class="field-prefix"]/following-sibling::div[@id="edit-form-radios-test"]'); - $this->assertTrue(isset($elements[0]), 'Properly placed the #field_prefix element after the label and before the field.'); - - $elements = $this->xpath('//span[@class="field-suffix"]/preceding-sibling::div[@id="edit-form-radios-test"]'); - $this->assertTrue(isset($elements[0]), 'Properly places the #field_suffix element immediately after the form field.'); + $this->assertSession()->elementExists('xpath', '//span[@class="field-prefix"]/following-sibling::div[@id="edit-form-radios-test"]'); + $this->assertSession()->elementExists('xpath', '//span[@class="field-suffix"]/preceding-sibling::div[@id="edit-form-radios-test"]'); // Check #prefix and #suffix placement. Both elements placed before the form // item. @@ -80,11 +77,13 @@ public function testFormLabels() { $this->assertSession()->elementAttributeContains('css', '#edit-form-checkboxes-title-attribute', 'title', 'Checkboxes test (Required)'); $this->assertSession()->elementAttributeContains('css', '#edit-form-radios-title-attribute', 'title', 'Radios test (Required)'); - $elements = $this->xpath('//fieldset[@id="edit-form-checkboxes-title-invisible--wrapper"]/legend/span[contains(@class, "visually-hidden")]'); - $this->assertTrue(!empty($elements), "Title/Label not displayed when 'visually-hidden' attribute is set in checkboxes."); + // Check Title/Label not displayed when 'visually-hidden' attribute is set + // in checkboxes. + $this->assertSession()->elementExists('xpath', '//fieldset[@id="edit-form-checkboxes-title-invisible--wrapper"]/legend/span[contains(@class, "visually-hidden")]'); - $elements = $this->xpath('//fieldset[@id="edit-form-radios-title-invisible--wrapper"]/legend/span[contains(@class, "visually-hidden")]'); - $this->assertTrue(!empty($elements), "Title/Label not displayed when 'visually-hidden' attribute is set in radios."); + // Check Title/Label not displayed when 'visually-hidden' attribute is set + // in radios. + $this->assertSession()->elementExists('xpath', '//fieldset[@id="edit-form-radios-title-invisible--wrapper"]/legend/span[contains(@class, "visually-hidden")]'); } /** diff --git a/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php b/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php index f6f6c018a734..0ec4e2bbcd79 100644 --- a/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php @@ -48,43 +48,45 @@ protected function setUp($import_test_views = TRUE): void { * Tests arguments. */ public function testArguments() { + $xpath = '//span[@class="field-content"]'; + + // Ensure that all the entries are returned. $this->drupalGet('test_route_without_arguments'); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(5, $result, 'All entries was returned'); + $this->assertSession()->elementsCount('xpath', $xpath, 5); $this->drupalGet('test_route_without_arguments/1'); $this->assertSession()->statusCodeEquals(404); + // Ensure that just the filtered entry is returned. $this->drupalGet('test_route_with_argument/1'); $this->assertSession()->statusCodeEquals(200); $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url']); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.'); - $this->assertEqual(1, $result[0]->getText(), 'The passed ID was returned.'); + $this->assertSession()->elementsCount('xpath', $xpath, 1); + $this->assertSession()->elementTextEquals('xpath', $xpath, 1); + // Ensure that just the filtered entry is returned. $this->drupalGet('test_route_with_suffix/1/suffix'); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.'); - $this->assertEqual(1, $result[0]->getText(), 'The passed ID was returned.'); + $this->assertSession()->elementsCount('xpath', $xpath, 1); + $this->assertSession()->elementTextEquals('xpath', $xpath, 1); + // Ensure that no result is returned. $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/2'); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(0, $result, 'No result was returned.'); + $this->assertSession()->elementNotExists('xpath', $xpath); + // Ensure that just the filtered entry is returned. $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/1'); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.'); - $this->assertEqual(1, $result[0]->getText(), 'The passed ID was returned.'); + $this->assertSession()->elementsCount('xpath', $xpath, 1); + $this->assertSession()->elementTextEquals('xpath', $xpath, 1); + // Ensure that just the filtered entry is returned. $this->drupalGet('test_route_with_long_argument/1'); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//span[@class="field-content"]'); - $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.'); - $this->assertEqual(1, $result[0]->getText(), 'The passed ID was returned.'); + $this->assertSession()->elementsCount('xpath', $xpath, 1); + $this->assertSession()->elementTextEquals('xpath', $xpath, 1); } /** diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php index 397ff79211bd..41d9092c3942 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php @@ -72,8 +72,9 @@ protected function setUpProfile() { PerformanceTestRecorder::registerService($this->siteDirectory . '/services.yml', TRUE); // Assert that the expected title is present. $this->assertEqual('Select an installation profile', $this->cssSelect('main h2')[0]->getText()); - $result = $this->xpath('//span[contains(@class, :class) and contains(text(), :text)]', [':class' => 'visually-hidden', ':text' => 'Select an installation profile']); - $this->assertCount(1, $result, "Title/Label not displayed when '#title_display' => 'invisible' attribute is set"); + // Verify that Title/Label are not displayed when '#title_display' => + // 'invisible' attribute is set. + $this->assertSession()->elementsCount('xpath', "//span[contains(@class, 'visually-hidden') and contains(text(), 'Select an installation profile')]", 1); parent::setUpProfile(); } -- GitLab