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

5.4k Views Asked by At

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?

4

There are 4 best solutions below

0
James Allen On BEST ANSWER

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".

1
jahsome On

Without a good idea of what you're trying to accomplish you can always wait until the element is visible:

https://laravel.com/docs/5.5/dusk#waiting-for-elements

0
Sten Govaerts On

You can try to find the element and try to retrieve properties of it. If the properties are empty, the element is not visible. E.g.

$hiddenBtn = $browser->element('@show-more');
if($hiddenBtn && $hiddenBtn->getText()){
    $browser->click('@show-more');    
}

This worked for me.

0
Hugo Falcão On
$browser->whenAvailable('.modal', function (Browser $modal) {
    $modal->assertSee('Hello World')
          ->press('OK');
});