@@ -169,6 +169,7 @@ The example below will find the input node for the following DOM structures:
169169label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
170170<TabItem value = " native" >
171171
172+
172173``` js
173174import { screen } from ' @testing-library/dom'
174175
@@ -178,6 +179,7 @@ const inputNode = screen.getByLabelText('Username')
178179 </TabItem >
179180 <TabItem value = " react" >
180181
182+
181183``` jsx
182184import { render , screen } from ' @testing-library/react'
183185
@@ -189,13 +191,15 @@ const inputNode = screen.getByLabelText('username')
189191 </TabItem >
190192 <TabItem value = " cypress" >
191193
194+
192195``` js
193196cy .findByLabelText (' username' ).should (' exist' )
194197```
195198
196199 </TabItem >
197200 </Tabs >
198201
202+
199203It will NOT find the input node for label text broken up by elements. You can
200204use ` getByRole('textbox', { name: 'Username' }) ` instead which is robust against
201205switching to ` aria-label ` or ` aria-labelledby ` .
@@ -263,6 +267,7 @@ matches the given [`TextMatch`](#textmatch).
263267label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
264268<TabItem value = " native" >
265269
270+
266271``` js
267272import { screen } from ' @testing-library/dom'
268273
@@ -272,6 +277,7 @@ const inputNode = screen.getByPlaceholderText('Username')
272277 </TabItem >
273278 <TabItem value = " react" >
274279
280+
275281``` jsx
276282import { render , screen } from ' @testing-library/react'
277283
@@ -282,13 +288,15 @@ const inputNode = screen.getByPlaceholderText('Username')
282288 </TabItem >
283289 <TabItem value = " cypress" >
284290
291+
285292``` js
286293cy .findByPlaceholderText (' Username' ).should (' exist' )
287294```
288295
289296 </TabItem >
290297 </Tabs >
291298
299+
292300> ** Note**
293301>
294302> A placeholder is not a good substitute for a label so you should generally use
@@ -322,6 +330,7 @@ matching the given [`TextMatch`](#textmatch).
322330label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
323331<TabItem value = " native" >
324332
333+
325334``` js
326335import { screen } from ' @testing-library/dom'
327336
@@ -331,6 +340,7 @@ const aboutAnchorNode = screen.getByText(/about/i)
331340 </TabItem >
332341 <TabItem value = " react" >
333342
343+
334344``` jsx
335345import { render , screen } from ' @testing-library/react'
336346
@@ -341,13 +351,15 @@ const aboutAnchorNode = screen.getByText(/about/i)
341351 </TabItem >
342352 <TabItem value = " cypress" >
343353
354+
344355``` js
345356cy .findByText (/ about/ i ).should (' exist' )
346357```
347358
348359 </TabItem >
349360 </Tabs >
350361
362+
351363It also works with ` input ` s whose ` type ` attribute is either ` submit ` or
352364` button ` :
353365
@@ -400,6 +412,7 @@ as it's deprecated).
400412label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
401413<TabItem value = " native" >
402414
415+
403416``` js
404417import { screen } from ' @testing-library/dom'
405418
@@ -409,6 +422,7 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
409422 </TabItem >
410423 <TabItem value = " react" >
411424
425+
412426``` jsx
413427import { render , screen } from ' @testing-library/react'
414428
@@ -419,13 +433,15 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
419433 </TabItem >
420434 <TabItem value = " cypress" >
421435
436+
422437``` js
423438cy .findByAltText (/ incredibles. *? poster/ i ).should (' exist' )
424439```
425440
426441 </TabItem >
427442 </Tabs >
428443
444+
429445### ` ByTitle `
430446
431447> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle,
@@ -457,6 +473,7 @@ Will also find a `title` element within an SVG.
457473label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
458474<TabItem value = " native" >
459475
476+
460477``` js
461478import { screen } from ' @testing-library/dom'
462479
@@ -467,6 +484,7 @@ const closeElement = screen.getByTitle('Close')
467484 </TabItem >
468485 <TabItem value = " react" >
469486
487+
470488``` jsx
471489import { render , screen } from ' @testing-library/react'
472490
@@ -478,6 +496,7 @@ const closeElement = screen.getByTitle('Close')
478496 </TabItem >
479497 <TabItem value = " cypress" >
480498
499+
481500``` js
482501cy .findByTitle (' Delete' ).should (' exist' )
483502cy .findByTitle (' Close' ).should (' exist' )
@@ -486,6 +505,7 @@ cy.findByTitle('Close').should('exist')
486505 </TabItem >
487506 </Tabs >
488507
508+
489509### ` ByDisplayValue `
490510
491511> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
@@ -518,6 +538,7 @@ document.getElementById('lastName').value = 'Norris'
518538label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
519539<TabItem value = " native" >
520540
541+
521542``` js
522543import { screen } from ' @testing-library/dom'
523544
@@ -527,6 +548,7 @@ const lastNameInput = screen.getByDisplayValue('Norris')
527548 </TabItem >
528549 <TabItem value = " react" >
529550
551+
530552``` jsx
531553import { render , screen } from ' @testing-library/react'
532554
@@ -537,13 +559,15 @@ const lastNameInput = screen.getByDisplayValue('Norris')
537559 </TabItem >
538560 <TabItem value = " cypress" >
539561
562+
540563``` js
541564cy .findByDisplayValue (' Norris' ).should (' exist' )
542565```
543566
544567 </TabItem >
545568 </Tabs >
546569
570+
547571#### ` textarea `
548572
549573``` html
@@ -558,6 +582,7 @@ document.getElementById('messageTextArea').value = 'Hello World'
558582label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
559583<TabItem value = " native" >
560584
585+
561586``` js
562587import { screen } from ' @testing-library/dom'
563588
@@ -567,6 +592,7 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
567592 </TabItem >
568593 <TabItem value = " react" >
569594
595+
570596``` jsx
571597import { render , screen } from ' @testing-library/react'
572598
@@ -577,13 +603,15 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
577603 </TabItem >
578604 <TabItem value = " cypress" >
579605
606+
580607``` js
581608cy .findByDisplayValue (' Hello World' ).should (' exist' )
582609```
583610
584611 </TabItem >
585612 </Tabs >
586613
614+
587615#### ` select `
588616
589617In case of ` select ` , this will search for a ` <select> ` whose selected ` <option> `
@@ -602,6 +630,7 @@ matches the given [`TextMatch`](#textmatch).
602630label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
603631<TabItem value = " native" >
604632
633+
605634``` js
606635import { screen } from ' @testing-library/dom'
607636
@@ -611,6 +640,7 @@ const selectElement = screen.getByDisplayValue('Alaska')
611640 </TabItem >
612641 <TabItem value = " react" >
613642
643+
614644``` jsx
615645import { render , screen } from ' @testing-library/react'
616646
@@ -621,13 +651,15 @@ const selectElement = screen.getByDisplayValue('Alaska')
621651 </TabItem >
622652 <TabItem value = " cypress" >
623653
654+
624655``` js
625656cy .findByDisplayValue (' Alaska' ).should (' exist' )
626657```
627658
628659 </TabItem >
629660 </Tabs >
630661
662+
631663### ` ByRole `
632664
633665> getByRole, queryByRole, getAllByRole, queryAllByRole, findByRole,
@@ -645,6 +677,7 @@ getByRole(
645677 selected?: boolean ,
646678 checked?: boolean ,
647679 pressed?: boolean ,
680+ expanded?: boolean ,
648681 queryFallbacks?: boolean ,
649682 level?: number ,
650683 }): HTMLElement
@@ -778,6 +811,37 @@ you can get the "👍" button by calling `getByRole('button', { pressed: true })
778811To learn more about the pressed state see
779812[ ARIA ` aria-pressed ` ] ( https://www.w3.org/TR/wai-aria-1.2/#aria-pressed ) .
780813
814+ #### ` expanded `
815+
816+ You can filter the returned elements by their expanded state by setting
817+ ` expanded: true ` or ` expanded: false ` .
818+
819+ For example in
820+
821+ ``` html
822+ <body >
823+ <nav >
824+ <ul >
825+ <li >
826+ <a aria-expanded =" false" aria-haspopup =" true" href =" ..."
827+ >Expandable Menu Item</a
828+ >
829+ <ul >
830+ <li ><a href =" #" >Submenu Item 1</a ></li >
831+ <li ><a href =" #" >Submenu Item 1</a ></li >
832+ </ul >
833+ </li >
834+ <li ><a href =" #" >Regular Menu Item</a ></li >
835+ </ul >
836+ </nav >
837+ </body >
838+ ```
839+
840+ you can get the "Expandable Menu Item" link by calling
841+ ` getByRole('link', { expanded: false }) ` . To learn more about the checked state
842+ and which elements can have this state see
843+ [ ARIA ` aria-checked ` ] ( https://www.w3.org/TR/wai-aria-1.2/#aria-expanded ) .
844+
781845``` html
782846<div role =" dialog" >...</div >
783847```
@@ -786,6 +850,7 @@ To learn more about the pressed state see
786850label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
787851<TabItem value = " native" >
788852
853+
789854``` js
790855import { screen } from ' @testing-library/dom'
791856
@@ -795,6 +860,7 @@ const dialogContainer = screen.getByRole('dialog')
795860 </TabItem >
796861 <TabItem value = " react" >
797862
863+
798864``` jsx
799865import { render , screen } from ' @testing-library/react'
800866
@@ -805,13 +871,15 @@ const dialogContainer = screen.getByRole('dialog')
805871 </TabItem >
806872 <TabItem value = " cypress" >
807873
874+
808875``` js
809876cy .findByRole (' dialog' ).should (' exist' )
810877```
811878
812879 </TabItem >
813880 </Tabs >
814881
882+
815883#### ` queryFallbacks `
816884
817885By default, it's assumed that the first role of each element is supported, so
@@ -903,6 +971,7 @@ also accepts a [`TextMatch`](#textmatch)).
903971label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
904972<TabItem value = " native" >
905973
974+
906975``` js
907976import { screen } from ' @testing-library/dom'
908977
@@ -912,6 +981,7 @@ const element = screen.getByTestId('custom-element')
912981 </TabItem >
913982 <TabItem value = " react" >
914983
984+
915985``` jsx
916986import { render , screen } from ' @testing-library/react'
917987
@@ -922,13 +992,15 @@ const element = screen.getByTestId('custom-element')
922992 </TabItem >
923993 <TabItem value = " cypress" >
924994
995+
925996``` js
926997cy .findByTestId (' custom-element' ).should (' exist' )
927998```
928999
9291000 </TabItem >
9301001 </Tabs >
9311002
1003+
9321004> In the spirit of [ the guiding principles] ( guiding-principles.mdx ) , it is
9331005> recommended to use this only after the other queries don't work for your use
9341006> case. Using data-testid attributes do not resemble how your software is used
0 commit comments