WebAssert and JsWebAssert have new methods that can be used to assert the presence or absence of status messages during Functional and FunctionalJavascript tests.
Using these methods will allow for more consistent and readable tests that are not implicitly reliant on any theme (specifically Classy).
New Methods
WebAssert::statusMessageExists(string $type = NULL)
WebAssert::statusMessageNotExists(string $type = NULL)
WebAssert::statusMessageContains(string $message, string $type = NULL)
WebAssert::statusMessageNotContains(string $message, string $type = NULL)
JsWebAssert::statusMessageExistsAfterWait(string $type = NULL, int $timeout = 10000)
JsWebAssert::statusMessageNotExistsAfterWait(string $type = NULL, int $timeout = 10000)
JsWebAssert::statusMessageContainsAfterWait(string $message, string $type = NULL, int $timeout = 10000)
JsWebAssert::statusMessageNotContainsAfterWait(string $message, string $type = NULL, int $timeout = 10000)
For all methods, the value of $type can be status, warning, or error. If the type is omitted or if NULL is passed, the assertion will not distinguish among messages of different types.
Examples
Functional Tests
Before:
$this->assertSession()->elementExists('xpath', '//div[contains(@class, "messages--error")]');
After:
$this->assertSession()->statusMessageExists('error');
Before:
$this->assertSession()->elementNotExists('css', '.messages--warning');
After:
$this->assertSession()->statusMessageNotExists('warning');
Before:
$arguments = [':message' => 'Only digits are allowed'];
$pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]';
$elements = $this->xpath($pattern, $arguments);
$this->assertNotEmpty($elements, 'Plugin error message found in parent form.');After:
$this->assertSession()->statusMessageContains('Only digits are allowed', 'error');
Before:
$this->assertSession()->elementTextContains('xpath', '//div[@aria-label="Status message"]', 'No pending updates.');
After:
$this->assertSession()->statusMessageContains('No pending updates.', 'status');
Before:
// Check if an error message is shown.
$this->assertSession()->pageTextNotContains("Minimum is not a valid number.");After:
$this->assertSession()->statusMessageNotContains("Minimum is not a valid number.", 'error')
FunctionalJavascript Tests
Before:
$this->assertNotEmpty($this->assertSession()->waitForElement('css', '.messages--status'));
$this->assertSession()->pageTextContains('The layout has been saved.');After:
$this->assertSession()->statusMessageContainsAfterWait('The layout has been saved.', 'status');
Before:
$this->assertNotEmpty($this->assertSession()->waitForElement('css', '.messages--error'));
After:
$this->assertSession()->statusMessageNotExistsAfterWait('error');