Course contentsShow
← React
Lesson 2057 of 2,39646. Testing React with RTLFree lesson

getByPlaceholderText and getByDisplayValue

Less preferred queries for form inputs when labels aren't available.

Less Preferred Queries for Form Inputs Without Labels

You'll learn two fallback queries for form fields when proper labels aren't available—and why they're not your first choice.

What You'll Learn

When a form input lacks an accessible label (which is not ideal), getByPlaceholderText and getByDisplayValue can find inputs by their placeholder attribute or current value, respectively.

Why These Are Less Preferred

Remember from previous lessons that getByLabelText and getByRole are your priority queries because they match how assistive technologies work. These two queries target implementation details rather than accessibility semantics:

  • getByPlaceholderText: Finds inputs by their placeholder attribute
  • getByDisplayValue: Finds inputs by their current displayed value

Both are escape hatches for poorly-labeled forms, not your go-to approach.

When You Might Use Them

getByPlaceholderText works when an input has no label but has a placeholder:

// An input with <input placeholder="Enter your email" />
const emailInput = screen.getByPlaceholderText('Enter your email');

getByDisplayValue finds inputs that already contain a specific value:

// An input currently showing "John Doe" as its value
const nameInput = screen.getByDisplayValue('John Doe');

The Analogy

Think of these as asking someone "What's the faded gray hint text in that box?" or "Which box currently says 'John'?" instead of "Where's the email field?" Technically it works, but it's not how users naturally identify fields.

Why Avoid Them

Placeholders disappear when users type. Display values change constantly. Neither reflects how screen readers announce fields to users. If you find yourself reaching for these queries, it's often a sign your component needs better accessibility labeling.

Key Takeaway: getByPlaceholderText and getByDisplayValue are fallback queries for unlabeled inputs—use them sparingly, and consider it a signal to improve your component's accessibility instead.