Add methods to assert status messages to WebAssert

Created on 10 March 2022, about 3 years ago
Updated 21 August 2024, 8 months ago

Problem/Motivation

While working on 📌 [meta] Update tests that rely on Classy to not rely on it anymore Fixed it has become clear that there are many assertions in Functionl/Functional JS tests related to status messages, and they are written many different ways. Most of them depend on Classy being the theme. For example:

$this->assertSession()->elementNotExists('xpath', '//div[contains(@class, "messages--error")]');

$xpath_err = '//div[contains(@class, "error")]';
    $this->assertNotEmpty($this->xpath($xpath_err), 'Enabling translation only for entity bundles generates a form error.');

$this->assertSession()->elementNotExists('xpath', '//div[contains(@class, "messages--status")]');

$this->assertSession()
          ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.');

And a couple examples from Functional JS tests where we wait for messages.

$assert_session->assertNoElementAfterWait('css', '.messages--warning');

// Check that the 'content has been updated' message status appears to confirm we left the editor.
    $assert_session->waitForElementVisible('css', 'messages messages--status');

Proposed resolution

Make assertions of Status Messages more consistent and manageable by introducing an AssertStatusMessageTrait that can be used in Functional/Function JS tests.

The methods should be:

  1. assertStatusMessageExists
  2. assertStatusMessageNotExists
  3. assertStatusMessageVisibleAfterWait
  4. assertNoStatusMessageAfterWait

each accepting optional $type and $message parameters.

  /**
   * Whatever this method does.
   *
   * @param string|null $type
   *   The optional message type: status, error, or warning.
   * @param string|null $message
   *   The optional message or partial message to assert.
   */

After getting the Trait into the codebase, the Trait can then be used to make 📌 [meta] Update tests that rely on Classy to not rely on it anymore Fixed much smoother.

Another possibility, brought up in #28 is to add methods to WebAssert/JsWebAssert instead of making new traits.

Remaining tasks

Etc

User interface changes

None

API changes

New trait for tests, but essentially none

Data model changes

None

Release notes snippet

WebAssert and JsWebAssert have new methods that can be used to assert the presence or absence of status messages during Functional and FunctionalJavascript tests. The full Change Record is https://www.drupal.org/node/3270424

📌 Task
Status

Fixed

Version

9.4

Component
PHPUnit 

Last updated about 12 hours ago

Created by

🇺🇸United States danflanagan8 St. Louis, US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024