1. 7
    Get Elements Associated With a Label Using the byLabelText Query
    49s

Get Elements Associated With a Label Using the byLabelText Query

Daniel Afonso
InstructorDaniel Afonso
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

When interacting with form fields, the user knows what an element is supposed to do, thanks to its label.

In this lesson, we will look at another one of the queries that are part of the queries accessible to everyone's family and really helpful when dealing with forms: the byLabelText query.

Instructor: [0:00] Inside the query is accessible to everyone's family, the byLabelText query is your go-to whenever you are dealing with form fields.

[0:07] The way user interacts with the form is by reading the label and deciding that indeed that is the input they want to interact with. The byLabelText query captures this behavior. Let's look at our component.

[0:18] In here, you can see, we have a label with the text username. This label is assigned to the input below, thanks to the HTML4 attribute having that input ID. If in our tests we want to have access to this input, we can use the byLabelText query. First, let's destructure the getByLabelText from render.

[0:35] Then we call getByLabelText and pass the text that was on the label, which in this case was username. Then we add R to be in a document assertion to make sure everything works. Now, if we run our tests, we should see that it has passed.