Finding

Finding

finding

 

Let’s understand Finding. Full reference: Capybara::Node::Finders

You can also find specific elements, in order to manipulate them:

find_field(‘First Name’).value

find_link(‘Hello’, :visible => :all).visible?

find_button(‘Send’).click

 

find(:xpath, “//table/tr”).click

find(“#overlay”).find(“h1”).click

all(‘a’).each { |a| a[:href] }

 

find will wait for an element to appear on the page, as explained in the Ajax section. If the element does not appear it will raise an error.

These elements all have all the Capybara DSL methods available, so you can restrict them to specific parts of the page:

find(‘#navigation’).click_link(‘Home’)

expect(find(‘#navigation’)).to have_button(‘Sign out’)

Capybara also has finders like has_content?(“Dashboard”) which would check if “Dashboard” was on the page. But there’s a trick: this command assumes “Dashboard” should be on the page, and waits for it to appear. This is supposed to be used with an assertion, like assert page.has_content?(“Dashboard”).

If you want the opposite you need to flip the finder, not the assertion. So, assert page.has_no_content?(“Dashboard”) will look to see that the page doesn’t have “Dashboard”. But if the page does have “Dashboard” it will wait until “Dashboard” leaves the page via ajax.

That brings us to a common mistake: flipping the assertion instead of the finder, like this: refute page.has_content?(“Dashboard”). This will wait until the page has the content “Dashboard”, which it won’t, then return false, which passes the refute. So while this test passes, it always waits the full Capybara default wait time. Which means that this is 15 seconds (the default) slower than assert page.has_no_content?(“Dashboard”).

In summary, it’s really important to use the capybara finder that lines up with what you expect on the page.

Make your resume stand out and become a Certified Capybara Testing Professional. So, Try free practice tests here!

A great career is just a certification away. So, practice and validate your skills to become Certified Capybara Testing Professional

Querying
Understanding Scoping

Get industry recognized certification – Contact us

keyboard_arrow_up