Home > Blockchain >  Why does assert_no_selector() wait until the wait time is over even if the element is found?
Why does assert_no_selector() wait until the wait time is over even if the element is found?

Time:11-23

I am checking a webpage for a certain element using assert_selector and assert_no_selector.

In one context, the element is present, and in one of them it is not. My tests are working properly. However, if I use assert_no_selector when the element is on the page, instead of the test failing immediately, it waits until the default_max_wait_time specified in my helper file before it fails and moves on to the next test. For example

scenario 'expect element to appear' do
   login(email, password)
   assert_no_selector('a[id="elementID"]', minimum: 1)
end

I would assume that if this code is run and the element is in fact located on the page, then it would immediately fail. Why does it wait until the max wait time has been reached even though the element is probably found very quickly? Is there a better way to do this? I would like to avoid having the test wait the entire max wait time whenever this test fails.

CodePudding user response:

The purpose of the waiting behavior in Capybaras assertions is to deal with asynchronous behavior in a browser. If you use assert_no_selector you are telling capybara that you expect the page not to have that element, so it will wait up to the maximum wait time for that element to go away before raising an exception (because you're telling it you expect that element not to be there and the page may be processing something that would cause that element to be removed). If you want an immediate check then specify a wait of 0 - but that will generally lead to flaky tests once you have JS behaviors in the page.

assert_no_selector('a#elementID', wait: 0)    
  • Related