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.

egghead
egghead
~ 35 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today