@@ -169,6 +169,7 @@ The example below will find the input node for the following DOM structures:
169
169
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
170
170
<TabItem value = " native" >
171
171
172
+
172
173
``` js
173
174
import { screen } from ' @testing-library/dom'
174
175
@@ -178,6 +179,7 @@ const inputNode = screen.getByLabelText('Username')
178
179
</TabItem >
179
180
<TabItem value = " react" >
180
181
182
+
181
183
``` jsx
182
184
import { render , screen } from ' @testing-library/react'
183
185
@@ -189,13 +191,15 @@ const inputNode = screen.getByLabelText('username')
189
191
</TabItem >
190
192
<TabItem value = " cypress" >
191
193
194
+
192
195
``` js
193
196
cy .findByLabelText (' username' ).should (' exist' )
194
197
```
195
198
196
199
</TabItem >
197
200
</Tabs >
198
201
202
+
199
203
It will NOT find the input node for label text broken up by elements. You can
200
204
use ` getByRole('textbox', { name: 'Username' }) ` instead which is robust against
201
205
switching to ` aria-label ` or ` aria-labelledby ` .
@@ -263,6 +267,7 @@ matches the given [`TextMatch`](#textmatch).
263
267
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
264
268
<TabItem value = " native" >
265
269
270
+
266
271
``` js
267
272
import { screen } from ' @testing-library/dom'
268
273
@@ -272,6 +277,7 @@ const inputNode = screen.getByPlaceholderText('Username')
272
277
</TabItem >
273
278
<TabItem value = " react" >
274
279
280
+
275
281
``` jsx
276
282
import { render , screen } from ' @testing-library/react'
277
283
@@ -282,13 +288,15 @@ const inputNode = screen.getByPlaceholderText('Username')
282
288
</TabItem >
283
289
<TabItem value = " cypress" >
284
290
291
+
285
292
``` js
286
293
cy .findByPlaceholderText (' Username' ).should (' exist' )
287
294
```
288
295
289
296
</TabItem >
290
297
</Tabs >
291
298
299
+
292
300
> ** Note**
293
301
>
294
302
> A placeholder is not a good substitute for a label so you should generally use
@@ -322,6 +330,7 @@ matching the given [`TextMatch`](#textmatch).
322
330
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
323
331
<TabItem value = " native" >
324
332
333
+
325
334
``` js
326
335
import { screen } from ' @testing-library/dom'
327
336
@@ -331,6 +340,7 @@ const aboutAnchorNode = screen.getByText(/about/i)
331
340
</TabItem >
332
341
<TabItem value = " react" >
333
342
343
+
334
344
``` jsx
335
345
import { render , screen } from ' @testing-library/react'
336
346
@@ -341,13 +351,15 @@ const aboutAnchorNode = screen.getByText(/about/i)
341
351
</TabItem >
342
352
<TabItem value = " cypress" >
343
353
354
+
344
355
``` js
345
356
cy .findByText (/ about/ i ).should (' exist' )
346
357
```
347
358
348
359
</TabItem >
349
360
</Tabs >
350
361
362
+
351
363
It also works with ` input ` s whose ` type ` attribute is either ` submit ` or
352
364
` button ` :
353
365
@@ -400,6 +412,7 @@ as it's deprecated).
400
412
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
401
413
<TabItem value = " native" >
402
414
415
+
403
416
``` js
404
417
import { screen } from ' @testing-library/dom'
405
418
@@ -409,6 +422,7 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
409
422
</TabItem >
410
423
<TabItem value = " react" >
411
424
425
+
412
426
``` jsx
413
427
import { render , screen } from ' @testing-library/react'
414
428
@@ -419,13 +433,15 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
419
433
</TabItem >
420
434
<TabItem value = " cypress" >
421
435
436
+
422
437
``` js
423
438
cy .findByAltText (/ incredibles. *? poster/ i ).should (' exist' )
424
439
```
425
440
426
441
</TabItem >
427
442
</Tabs >
428
443
444
+
429
445
### ` ByTitle `
430
446
431
447
> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle,
@@ -457,6 +473,7 @@ Will also find a `title` element within an SVG.
457
473
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
458
474
<TabItem value = " native" >
459
475
476
+
460
477
``` js
461
478
import { screen } from ' @testing-library/dom'
462
479
@@ -467,6 +484,7 @@ const closeElement = screen.getByTitle('Close')
467
484
</TabItem >
468
485
<TabItem value = " react" >
469
486
487
+
470
488
``` jsx
471
489
import { render , screen } from ' @testing-library/react'
472
490
@@ -478,6 +496,7 @@ const closeElement = screen.getByTitle('Close')
478
496
</TabItem >
479
497
<TabItem value = " cypress" >
480
498
499
+
481
500
``` js
482
501
cy .findByTitle (' Delete' ).should (' exist' )
483
502
cy .findByTitle (' Close' ).should (' exist' )
@@ -486,6 +505,7 @@ cy.findByTitle('Close').should('exist')
486
505
</TabItem >
487
506
</Tabs >
488
507
508
+
489
509
### ` ByDisplayValue `
490
510
491
511
> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
@@ -518,6 +538,7 @@ document.getElementById('lastName').value = 'Norris'
518
538
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
519
539
<TabItem value = " native" >
520
540
541
+
521
542
``` js
522
543
import { screen } from ' @testing-library/dom'
523
544
@@ -527,6 +548,7 @@ const lastNameInput = screen.getByDisplayValue('Norris')
527
548
</TabItem >
528
549
<TabItem value = " react" >
529
550
551
+
530
552
``` jsx
531
553
import { render , screen } from ' @testing-library/react'
532
554
@@ -537,13 +559,15 @@ const lastNameInput = screen.getByDisplayValue('Norris')
537
559
</TabItem >
538
560
<TabItem value = " cypress" >
539
561
562
+
540
563
``` js
541
564
cy .findByDisplayValue (' Norris' ).should (' exist' )
542
565
```
543
566
544
567
</TabItem >
545
568
</Tabs >
546
569
570
+
547
571
#### ` textarea `
548
572
549
573
``` html
@@ -558,6 +582,7 @@ document.getElementById('messageTextArea').value = 'Hello World'
558
582
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
559
583
<TabItem value = " native" >
560
584
585
+
561
586
``` js
562
587
import { screen } from ' @testing-library/dom'
563
588
@@ -567,6 +592,7 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
567
592
</TabItem >
568
593
<TabItem value = " react" >
569
594
595
+
570
596
``` jsx
571
597
import { render , screen } from ' @testing-library/react'
572
598
@@ -577,13 +603,15 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
577
603
</TabItem >
578
604
<TabItem value = " cypress" >
579
605
606
+
580
607
``` js
581
608
cy .findByDisplayValue (' Hello World' ).should (' exist' )
582
609
```
583
610
584
611
</TabItem >
585
612
</Tabs >
586
613
614
+
587
615
#### ` select `
588
616
589
617
In case of ` select ` , this will search for a ` <select> ` whose selected ` <option> `
@@ -602,6 +630,7 @@ matches the given [`TextMatch`](#textmatch).
602
630
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
603
631
<TabItem value = " native" >
604
632
633
+
605
634
``` js
606
635
import { screen } from ' @testing-library/dom'
607
636
@@ -611,6 +640,7 @@ const selectElement = screen.getByDisplayValue('Alaska')
611
640
</TabItem >
612
641
<TabItem value = " react" >
613
642
643
+
614
644
``` jsx
615
645
import { render , screen } from ' @testing-library/react'
616
646
@@ -621,13 +651,15 @@ const selectElement = screen.getByDisplayValue('Alaska')
621
651
</TabItem >
622
652
<TabItem value = " cypress" >
623
653
654
+
624
655
``` js
625
656
cy .findByDisplayValue (' Alaska' ).should (' exist' )
626
657
```
627
658
628
659
</TabItem >
629
660
</Tabs >
630
661
662
+
631
663
### ` ByRole `
632
664
633
665
> getByRole, queryByRole, getAllByRole, queryAllByRole, findByRole,
@@ -645,6 +677,7 @@ getByRole(
645
677
selected?: boolean ,
646
678
checked?: boolean ,
647
679
pressed?: boolean ,
680
+ expanded?: boolean ,
648
681
queryFallbacks?: boolean ,
649
682
level?: number ,
650
683
}): HTMLElement
@@ -778,6 +811,37 @@ you can get the "👍" button by calling `getByRole('button', { pressed: true })
778
811
To learn more about the pressed state see
779
812
[ ARIA ` aria-pressed ` ] ( https://www.w3.org/TR/wai-aria-1.2/#aria-pressed ) .
780
813
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
+
781
845
``` html
782
846
<div role =" dialog" >...</div >
783
847
```
@@ -786,6 +850,7 @@ To learn more about the pressed state see
786
850
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
787
851
<TabItem value = " native" >
788
852
853
+
789
854
``` js
790
855
import { screen } from ' @testing-library/dom'
791
856
@@ -795,6 +860,7 @@ const dialogContainer = screen.getByRole('dialog')
795
860
</TabItem >
796
861
<TabItem value = " react" >
797
862
863
+
798
864
``` jsx
799
865
import { render , screen } from ' @testing-library/react'
800
866
@@ -805,13 +871,15 @@ const dialogContainer = screen.getByRole('dialog')
805
871
</TabItem >
806
872
<TabItem value = " cypress" >
807
873
874
+
808
875
``` js
809
876
cy .findByRole (' dialog' ).should (' exist' )
810
877
```
811
878
812
879
</TabItem >
813
880
</Tabs >
814
881
882
+
815
883
#### ` queryFallbacks `
816
884
817
885
By default, it's assumed that the first role of each element is supported, so
@@ -903,6 +971,7 @@ also accepts a [`TextMatch`](#textmatch)).
903
971
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
904
972
<TabItem value = " native" >
905
973
974
+
906
975
``` js
907
976
import { screen } from ' @testing-library/dom'
908
977
@@ -912,6 +981,7 @@ const element = screen.getByTestId('custom-element')
912
981
</TabItem >
913
982
<TabItem value = " react" >
914
983
984
+
915
985
``` jsx
916
986
import { render , screen } from ' @testing-library/react'
917
987
@@ -922,13 +992,15 @@ const element = screen.getByTestId('custom-element')
922
992
</TabItem >
923
993
<TabItem value = " cypress" >
924
994
995
+
925
996
``` js
926
997
cy .findByTestId (' custom-element' ).should (' exist' )
927
998
```
928
999
929
1000
</TabItem >
930
1001
</Tabs >
931
1002
1003
+
932
1004
> In the spirit of [ the guiding principles] ( guiding-principles.mdx ) , it is
933
1005
> recommended to use this only after the other queries don't work for your use
934
1006
> case. Using data-testid attributes do not resemble how your software is used
0 commit comments