Laravel Dusk provides various assertions to interact with your browser tests, but as of my knowledge cutoff in early 2023, there isn't a direct assertNotVisible method. However, you can achieve the same result by using the assertMissing or assertDontSee methods if the element should not be present in the DOM at all, or by using JavaScript to check the visibility of an element that is still in the DOM but not visible.
Here's an example of how you might implement an assertNotVisible check using JavaScript within a Dusk test:
$browser->visit('/your-page')
->assertScript('return window.getComputedStyle(document.querySelector(".your-element-class")).display') === 'none';
This code uses JavaScript to get the computed style of the element and checks if its display property is set to 'none', which would mean the element is not visible.
Alternatively, you can create a custom assertion method in your Dusk test case:
// In your DuskTestCase.php or a base test class
/**
* Assert that the given selector is not visible on the page.
*
* @param string $selector
* @return $this
*/
public function assertNotVisible($browser, $selector)
{
$style = $browser->script("return window.getComputedStyle(document.querySelector('{$selector}')).display")[0];
PHPUnit::assertTrue($style === 'none', "The element '{$selector}' is visible, but it was expected to be hidden.");
return $this;
}
// Usage in your test method
$browser->visit('/your-page')
->assertNotVisible('.your-element-class');
This custom assertion method uses PHPUnit to assert that the element with the given selector is not visible. You can call this method in your Dusk tests to check for non-visible elements.
Remember to replace .your-element-class with the actual selector of the element you want to check for visibility.