Transcripted Summary
If you're ever using Capybara and you find that you need to find a button in order to click on it, but the method `click_button` isn't enough, or if you need to find the value entered into a textbox, find a piece of text within another section, Capybara has built-in many different Finders you can use.

Going to Team Capybara's GitHub site, you can scroll down and select "Finding" and we can see a subset of all the Finders that Capybara has.



As you can see, there are many different types of Finders:

  • find_field
  • find_link
  • find_button
  • find by xpath [find(:xpath)]

In the first example we're using the Finder find_field we're trying to find the field with the label "First Name," and checking for its value:


find_field('First Name').value

In the next one, we're trying to find the field, "by id," β€” and get the value from it:


find_field(id: 'my_field').value

For the next one, we're trying to find a link called, "Hello" β€” we're trying to find all visible links, and actually check, is this link actually visible?


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

For the next one we're trying to find the button labeled, "Send" β€” and clicking on that. The first part, we're finding the button. For the second part, we're chaining an action to it, "Click."


find_button('Send').click

Here we're trying to find the button which contains the value of "1234" β€” and clicking on that.


find_button(value: '1234').click

We can also search by xpath β€” we can search for the xpath value of the locator.


find(:xpath, ".//table/tr").click

As you can see, with the Finders there's many different arguments and options you can use.

If you want to learn more about Finders, let's go to the Ruby doc of Capybara-Node-Finders.



If you scroll down, you can see that you have the method "find." With this method find you can see arguments and options.



For options, you can wait, you can search for text, search for exact text, review text for whitespace characters, check to see if something's visible or not, search for id, class, et cetera.



For find_button for locators, you can use an id, text id, value, title, text content, or the alt tag of an image.

You can find_by_id, find_field, find_link, and many others.

Here's some examples of the find method.



You can do something like: find something and wait for five seconds, you can match the text, match the text exactly.

Let's say, we wanted to find only visible elements, which is the default way Capybara finds elements, we can explicitly say:


find(visible:true)

To find both, visible and invisible elements, we can either one of these:


find(visible:false)

 find(visible:all)

If you wanted to find invisible elements, we can say:


find(visible:hidden)

In our next section, we're going to explore Matchers.



Resources



Quiz

The quiz for this chapter can be found in section 3.2

Β© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR