3

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?

ux.engineer
  • 10,082
  • 13
  • 67
  • 112

4 Answers4

3

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.

3

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

James Allen
  • 969
  • 4
  • 16
0

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

jahsome
  • 881
  • 8
  • 18
  • I need is to check if an element is there, so it is an IF scenario... Waiting for an element is a blocking scenario where the element absolutely needs to be present or the processing is halted after the wait time. – ux.engineer Jan 29 '18 at 19:34
0
$browser->whenAvailable('.modal', function (Browser $modal) {
    $modal->assertSee('Hello World')
          ->press('OK');
});