Why doesn't my "wait" function work in Capybara? I get a "not string or proc" error

155 Views Asked by At

When I add a wait function to an expect(page).to_have_content statement, I get the following error: WARNING: ignoring the provided expectation message argument ({:wait=>20}) since it is not a string or a proc. For reference, here is how my wait statement looks:

When 'I change the language' do
  expect(page).to have_content 'Primary Text', wait: 10
  first('div[data-testid="SelectLanguage"]').click
  first('li[data-testid="SelectLanguage__Select__es"]').click
end

Thanks in advance!

1

There are 1 best solutions below

0
Thomas Walpole On

The problem with Rubys optional ()s in method calls is that it allows for unclear/ambiguous statements when passing the output of methods as parameters to other methods. In your

expect(page).to have_content 'Primary Text', wait: 10

the wait: 10 is actually being passed as the second argument to ‘to’ rather than have_content so it's being interpreted as

expect(page).to(have_content('Primary Text'), wait: 10)

To get your desired behavior you need to do

expect(page).to have_content('Primary Text', wait: 10)

One benefit of using something like rubocop is that it will flag situations like this for you