@@ -109,6 +109,26 @@ screen.debug(screen.getByText('test'))
109
109
screen .debug (screen .getAllByText (' multi-test' ))
110
110
```
111
111
112
+ ### ` screen.logTestingPlaygroundURL `
113
+
114
+ For debugging using [ testing-playground] ( https://testing-playground.com ) , screen
115
+ exposes this convenient method which logs a URL that can be opened in a browser.
116
+
117
+ ``` javascript
118
+ import { screen } from ' @testing-library/dom'
119
+
120
+ document .body .innerHTML = `
121
+ <button >test</button >
122
+ <span >multi-test</span >
123
+ <div >multi-test</div >
124
+ `
125
+
126
+ // log entire document to testing-playground
127
+ screen .logTestingPlaygroundURL ()
128
+ // log a single element
129
+ screen .logTestingPlaygroundURL (screen .getByText (' test' ))
130
+ ```
131
+
112
132
## Queries
113
133
114
134
> ** Note**
@@ -169,7 +189,6 @@ The example below will find the input node for the following DOM structures:
169
189
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
170
190
<TabItem value = " native" >
171
191
172
-
173
192
``` js
174
193
import { screen } from ' @testing-library/dom'
175
194
@@ -179,7 +198,6 @@ const inputNode = screen.getByLabelText('Username')
179
198
</TabItem >
180
199
<TabItem value = " react" >
181
200
182
-
183
201
``` jsx
184
202
import { render , screen } from ' @testing-library/react'
185
203
@@ -191,15 +209,13 @@ const inputNode = screen.getByLabelText('username')
191
209
</TabItem >
192
210
<TabItem value = " cypress" >
193
211
194
-
195
212
``` js
196
213
cy .findByLabelText (' username' ).should (' exist' )
197
214
```
198
215
199
216
</TabItem >
200
217
</Tabs >
201
218
202
-
203
219
It will NOT find the input node for label text broken up by elements. You can
204
220
use ` getByRole('textbox', { name: 'Username' }) ` instead which is robust against
205
221
switching to ` aria-label ` or ` aria-labelledby ` .
@@ -267,7 +283,6 @@ matches the given [`TextMatch`](#textmatch).
267
283
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
268
284
<TabItem value = " native" >
269
285
270
-
271
286
``` js
272
287
import { screen } from ' @testing-library/dom'
273
288
@@ -277,7 +292,6 @@ const inputNode = screen.getByPlaceholderText('Username')
277
292
</TabItem >
278
293
<TabItem value = " react" >
279
294
280
-
281
295
``` jsx
282
296
import { render , screen } from ' @testing-library/react'
283
297
@@ -288,15 +302,13 @@ const inputNode = screen.getByPlaceholderText('Username')
288
302
</TabItem >
289
303
<TabItem value = " cypress" >
290
304
291
-
292
305
``` js
293
306
cy .findByPlaceholderText (' Username' ).should (' exist' )
294
307
```
295
308
296
309
</TabItem >
297
310
</Tabs >
298
311
299
-
300
312
> ** Note**
301
313
>
302
314
> A placeholder is not a good substitute for a label so you should generally use
@@ -330,7 +342,6 @@ matching the given [`TextMatch`](#textmatch).
330
342
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
331
343
<TabItem value = " native" >
332
344
333
-
334
345
``` js
335
346
import { screen } from ' @testing-library/dom'
336
347
@@ -340,7 +351,6 @@ const aboutAnchorNode = screen.getByText(/about/i)
340
351
</TabItem >
341
352
<TabItem value = " react" >
342
353
343
-
344
354
``` jsx
345
355
import { render , screen } from ' @testing-library/react'
346
356
@@ -351,15 +361,13 @@ const aboutAnchorNode = screen.getByText(/about/i)
351
361
</TabItem >
352
362
<TabItem value = " cypress" >
353
363
354
-
355
364
``` js
356
365
cy .findByText (/ about/ i ).should (' exist' )
357
366
```
358
367
359
368
</TabItem >
360
369
</Tabs >
361
370
362
-
363
371
It also works with ` input ` s whose ` type ` attribute is either ` submit ` or
364
372
` button ` :
365
373
@@ -412,7 +420,6 @@ as it's deprecated).
412
420
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
413
421
<TabItem value = " native" >
414
422
415
-
416
423
``` js
417
424
import { screen } from ' @testing-library/dom'
418
425
@@ -422,7 +429,6 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
422
429
</TabItem >
423
430
<TabItem value = " react" >
424
431
425
-
426
432
``` jsx
427
433
import { render , screen } from ' @testing-library/react'
428
434
@@ -433,15 +439,13 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
433
439
</TabItem >
434
440
<TabItem value = " cypress" >
435
441
436
-
437
442
``` js
438
443
cy .findByAltText (/ incredibles. *? poster/ i ).should (' exist' )
439
444
```
440
445
441
446
</TabItem >
442
447
</Tabs >
443
448
444
-
445
449
### ` ByTitle `
446
450
447
451
> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle,
@@ -473,7 +477,6 @@ Will also find a `title` element within an SVG.
473
477
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
474
478
<TabItem value = " native" >
475
479
476
-
477
480
``` js
478
481
import { screen } from ' @testing-library/dom'
479
482
@@ -484,7 +487,6 @@ const closeElement = screen.getByTitle('Close')
484
487
</TabItem >
485
488
<TabItem value = " react" >
486
489
487
-
488
490
``` jsx
489
491
import { render , screen } from ' @testing-library/react'
490
492
@@ -496,7 +498,6 @@ const closeElement = screen.getByTitle('Close')
496
498
</TabItem >
497
499
<TabItem value = " cypress" >
498
500
499
-
500
501
``` js
501
502
cy .findByTitle (' Delete' ).should (' exist' )
502
503
cy .findByTitle (' Close' ).should (' exist' )
@@ -505,7 +506,6 @@ cy.findByTitle('Close').should('exist')
505
506
</TabItem >
506
507
</Tabs >
507
508
508
-
509
509
### ` ByDisplayValue `
510
510
511
511
> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
@@ -538,7 +538,6 @@ document.getElementById('lastName').value = 'Norris'
538
538
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
539
539
<TabItem value = " native" >
540
540
541
-
542
541
``` js
543
542
import { screen } from ' @testing-library/dom'
544
543
@@ -548,7 +547,6 @@ const lastNameInput = screen.getByDisplayValue('Norris')
548
547
</TabItem >
549
548
<TabItem value = " react" >
550
549
551
-
552
550
``` jsx
553
551
import { render , screen } from ' @testing-library/react'
554
552
@@ -559,15 +557,13 @@ const lastNameInput = screen.getByDisplayValue('Norris')
559
557
</TabItem >
560
558
<TabItem value = " cypress" >
561
559
562
-
563
560
``` js
564
561
cy .findByDisplayValue (' Norris' ).should (' exist' )
565
562
```
566
563
567
564
</TabItem >
568
565
</Tabs >
569
566
570
-
571
567
#### ` textarea `
572
568
573
569
``` html
@@ -582,7 +578,6 @@ document.getElementById('messageTextArea').value = 'Hello World'
582
578
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
583
579
<TabItem value = " native" >
584
580
585
-
586
581
``` js
587
582
import { screen } from ' @testing-library/dom'
588
583
@@ -592,7 +587,6 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
592
587
</TabItem >
593
588
<TabItem value = " react" >
594
589
595
-
596
590
``` jsx
597
591
import { render , screen } from ' @testing-library/react'
598
592
@@ -603,15 +597,13 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
603
597
</TabItem >
604
598
<TabItem value = " cypress" >
605
599
606
-
607
600
``` js
608
601
cy .findByDisplayValue (' Hello World' ).should (' exist' )
609
602
```
610
603
611
604
</TabItem >
612
605
</Tabs >
613
606
614
-
615
607
#### ` select `
616
608
617
609
In case of ` select ` , this will search for a ` <select> ` whose selected ` <option> `
@@ -630,7 +622,6 @@ matches the given [`TextMatch`](#textmatch).
630
622
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
631
623
<TabItem value = " native" >
632
624
633
-
634
625
``` js
635
626
import { screen } from ' @testing-library/dom'
636
627
@@ -640,7 +631,6 @@ const selectElement = screen.getByDisplayValue('Alaska')
640
631
</TabItem >
641
632
<TabItem value = " react" >
642
633
643
-
644
634
``` jsx
645
635
import { render , screen } from ' @testing-library/react'
646
636
@@ -651,15 +641,13 @@ const selectElement = screen.getByDisplayValue('Alaska')
651
641
</TabItem >
652
642
<TabItem value = " cypress" >
653
643
654
-
655
644
``` js
656
645
cy .findByDisplayValue (' Alaska' ).should (' exist' )
657
646
```
658
647
659
648
</TabItem >
660
649
</Tabs >
661
650
662
-
663
651
### ` ByRole `
664
652
665
653
> getByRole, queryByRole, getAllByRole, queryAllByRole, findByRole,
@@ -850,7 +838,6 @@ and which elements can have this state see
850
838
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
851
839
<TabItem value = " native" >
852
840
853
-
854
841
``` js
855
842
import { screen } from ' @testing-library/dom'
856
843
@@ -860,7 +847,6 @@ const dialogContainer = screen.getByRole('dialog')
860
847
</TabItem >
861
848
<TabItem value = " react" >
862
849
863
-
864
850
``` jsx
865
851
import { render , screen } from ' @testing-library/react'
866
852
@@ -871,15 +857,13 @@ const dialogContainer = screen.getByRole('dialog')
871
857
</TabItem >
872
858
<TabItem value = " cypress" >
873
859
874
-
875
860
``` js
876
861
cy .findByRole (' dialog' ).should (' exist' )
877
862
```
878
863
879
864
</TabItem >
880
865
</Tabs >
881
866
882
-
883
867
#### ` queryFallbacks `
884
868
885
869
By default, it's assumed that the first role of each element is supported, so
@@ -971,7 +955,6 @@ also accepts a [`TextMatch`](#textmatch)).
971
955
label: ' React' , value: ' react' , }, { label: ' Cypress' , value: ' cypress' , }, ] } >
972
956
<TabItem value = " native" >
973
957
974
-
975
958
``` js
976
959
import { screen } from ' @testing-library/dom'
977
960
@@ -981,7 +964,6 @@ const element = screen.getByTestId('custom-element')
981
964
</TabItem >
982
965
<TabItem value = " react" >
983
966
984
-
985
967
``` jsx
986
968
import { render , screen } from ' @testing-library/react'
987
969
@@ -992,15 +974,13 @@ const element = screen.getByTestId('custom-element')
992
974
</TabItem >
993
975
<TabItem value = " cypress" >
994
976
995
-
996
977
``` js
997
978
cy .findByTestId (' custom-element' ).should (' exist' )
998
979
```
999
980
1000
981
</TabItem >
1001
982
</Tabs >
1002
983
1003
-
1004
984
> In the spirit of [ the guiding principles] ( guiding-principles.mdx ) , it is
1005
985
> recommended to use this only after the other queries don't work for your use
1006
986
> case. Using data-testid attributes do not resemble how your software is used
0 commit comments