Reputation: 63
Hi people from StackOverflow,
I'm taking over someone's work, and my predecessor created a testset using Cucumber&Capybara&Selenium. I'm familiar with the lot, but I've got a question concerning his way of finding text on a page.
His implementation:
expect(page).to have_content(text)
My implementation:
page.has_content?(text)
What I've noticed is that the first implementation often fails because the automation is 'too quick' for the website to load its page. The latter seems a more robust implementation, perhaps because of its simplicity?
Can someone tell me if there's a right or a wrong, or whether these two are fundamentally different. Because I've been trying to search the web but have not really found a solid conclusion..
Thanks in advance!
Upvotes: 1
Views: 7195
Reputation: 9
Ummm... I think that there is one more thing. expect(page).to_have_content(text) is Rspec method while page.has_content?(text) is Minitest method. It is just depend what type of testing you use in your project. I guess.
Upvotes: 1
Reputation: 49910
have_content
raises an exception when it's expectation fails and should be used much more often in most test suites than has_content?. has_content?
is basically just a wrapper around have_content that catches the exception and returns true or false, and is for use with conditionals
if page.has_content?(...)
# click something
else
# click something else
end
Your predecessor is using Capybara correctly since if you are testing to make sure a page has specific content you should be using have_content
. has_content?
will never fail the test (just silently return false and continue on). If your have_content
assertions are failing because the site is too slow you probably need to increase Capybara.default_max_wait_time
(or figure out why page load times are so long)
Upvotes: 9