Using Capybara and Selenium in a rails system / feature spec, I get random errors sometimes on CI (rarely locally) which come from either a StaleElementReferenceError or also sometimes just a Capybara ElementNotFound error (often because I have failed to explicitly look for some change).
Sometimes I can work around this with enough expect...to have or expect...not_to have, but sometimes I just can't. It feels awful (and actually slows down everything) to put in random sleep amounts.
But, inspired by https://accidentaltechnologist.com/ruby-on-rails/fixing-staleelementreferenceerror-when-using-capybara/, how about only sleeping when it fails?
Here's my response to this:
Sample usage:
describe "something that is temperamental", js: true do
visit "some/path"
click_on "some button"
succeed_or_sleep do
expect(page).to have_content("some new content")
end
end
Attached are the support file, and a spec for testing the support file