SitePrism how to wait for any of elements

2k views Asked by At

I am testing UI flow with Capybara and SitePrism stack. Unfortunately, my UI flow is not fully deterministic and sometimes a warning message pops up in the flow. Therefore I need to have a conditional flow in the test.

To wait for a single element, SitePrism provides the

@page.wait_for_<element name>

In my case there are two elements that can appear, :button_submit or :warning_popup. What I need to achieve is something like:

element_name = @page.wait_for_any_of(:button_submit, :warning_popup)

Is there some more elegant way other than running a loop like this?

element_name = nil
while (element_name.nil?) do
  element_name = :button_submit if app.page.has_button_submit?
  element_name = :warning_popup if app.page.has_warning_popup?
  sleep 0.1
end

I know this loop can end up in infinite loop, I'll keep it here simplified for ilustration.

2

There are 2 answers

0
Thomas Walpole On

I don't believe site-prism provides any clean way for waiting for one of multiple elements, however a workaround would be to add an element to the page that would find either of the elements by using the CSS comma

element :button_submit_or_warning_popup, "#id_of_button, #id_of_warning_popup" 

and then you could do

@page.wait_for_button_submit_or_warning_popup
0
Luke Hill On

SitePrism does have a waiter class that you can call from the top level namespace.

So you could use something like

SitePrism::Waiter.wait_until_true { has_button_submit? || has_warning_popup? }

Furthermore, with the release of SitePrism v3, you no longer need to call wait_for that is done automatically.

If you feel as though this is something you want to get added/work on with site_prism, the waiting abilities are something which are definitely under-utilised.

Both a message on GH here: https://github.com/natritmeyer/site_prism/issues with your thoughts