Search code examples
rubycapybarasite-prism

How to wait until an element disappears in Capybara?


Note: by the time I finished writing up this question, the solution presented itself, but maybe others may find this useful, or could offer alternatives.

I have read the following articles:

but still cannot piece together an answer for this particular problem.

I want to wait until an element on the page disappears before continuing.

This element overlaps another element I want to click. When I try to click the target element anyway, I get this error:

Capybara::Poltergeist::MouseEventFailed:
   Firing a click at co-ordinates [1006, 322] failed. Poltergeist detected another element with CSS selector 'html body.widgets div#select2-drop-mask.select2-drop-mask' at this position. It may be overlapping the element you are trying to interact with. If you don't care about overlapping elements, try using node.trigger('click').

I tried using the .trigger('click') and .trigger(:click) even, but it seems to have no effect.

Some other things I have tried ('thing' is the disappearing element):

@page.wait_until_thing_invisible
 SitePrism::TimeOutWaitingForElementInvisibility:
   execution expired

Solution

  • After more investigation, it seems that the element was rightly not disappearing, because of another issue.

         Failure/Error: expect(@page.thing.results.map(&:text)).to include a_string_matching 'search_query'
       expected ["Searching…"] to include (a string matching "search_query")
       Diff:
       @@ -1,2 +1,2 @@
       -[(a string matching "search_query")]
       +["Searching…"]
    

    That "Searching..." display happens for a split second, and needs to be waited on before trying to click a result.

    So now I would like to wait until I don't see the "Searching...", and then continuing with the next click.

    I ended up making this while statement:

    while search_complete = @page.thing.results.map(&:text).reduce.include?('Searching')
      sleep 1
    end
    

    And this seems to wait until the "Searching..." disappears and the real results appear.