laravelseleniumselenium-chromedriverlaravel-dusklaravel-dusk2

Laravel Dusk how to check if element is visible/clickable?


I have a scenario where I'd like not check IF an element exists and is visible/clickable. If not, script processing continues.

While Laravel Dusk provides $browser->assertVisible($selector) method, this ends up in an exception if the element is not visible. Or $browser->waitFor('.selector'); but this also ends the script processing if element doesn't appear.

Here is the criteria Selenium uses to check for an element being visible is found here: How to force Selenium WebDriver to click on element which is not currently visible?

Apparently Dusk doesn't provide this kind of method. What would be the best way to implement it?


Solution

  • Better late than never I suppose.

    isDisplayed() works pretty well (though not if it's covered up by other elements)...

    if($browser->driver->findElement(WebDriverBy::cssSelector('#my-selector'))->isDisplayed()) {
        // do something
    }
    

    if I have overlays covering my elements, I use ->waitUntilMissing(), or in extreme cases I call on $browser->driver->executeScript() and run some jQuery to temporarily manipulate an element that is "in the way".