jaspercreel's avatar

Laravel Dusk assertNotVisible

EDIT:: It is assertMissing. Read the docs people! I need to learn that myself.

Any idea why there isn't an easy assertNotVisible assertion in dusk? Am I just missing it. Dusk knows when an element is not interactable, why wouldn't I be able to tell if it is visible?

0 likes
4 replies
LaryAI's avatar
Level 58

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.

jaspercreel's avatar
jaspercreel
OP
Best Answer
Level 8

And I am the silliest goose. It is assertMissing. Gonna leave this embarrassing post up in case anyone else is as silly as me and needs an answer.

jenskh's avatar

It seems to me that assertMissing() is not the same as a desired assertNotVisible(). In my use case I have a laravel blade that contains fieldsets, each containing elements. Depending on user choice, certain fieldsets are visible or not. This is achieved by toggling display:block and display:none. In my dusk test I want to verify that a specific element contained in a fieldset is visible or not. In this case the element is in the DOM and the elements display is block, so assertMissing() doesn't work.

jenskh's avatar

I hate being wrong...

$browser->assertPresent($selector)
->assertMissing($selector);

does behave as expected if a parent element has display:none.

Please or to participate in this conversation.