Skip to content

Commit 362439f

Browse files
authored
docs: adds screen.logToTestingPlaygroundURL method (testing-library#715)
* docs: add screen.logTestingPlaygroundURL * fix grammar
1 parent ef88502 commit 362439f

File tree

1 file changed

+20
-40
lines changed

1 file changed

+20
-40
lines changed

docs/dom-testing-library/api-queries.mdx

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ screen.debug(screen.getByText('test'))
109109
screen.debug(screen.getAllByText('multi-test'))
110110
```
111111

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+
112132
## Queries
113133

114134
> **Note**
@@ -169,7 +189,6 @@ The example below will find the input node for the following DOM structures:
169189
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
170190
<TabItem value="native">
171191

172-
173192
```js
174193
import { screen } from '@testing-library/dom'
175194

@@ -179,7 +198,6 @@ const inputNode = screen.getByLabelText('Username')
179198
</TabItem>
180199
<TabItem value="react">
181200

182-
183201
```jsx
184202
import { render, screen } from '@testing-library/react'
185203

@@ -191,15 +209,13 @@ const inputNode = screen.getByLabelText('username')
191209
</TabItem>
192210
<TabItem value="cypress">
193211

194-
195212
```js
196213
cy.findByLabelText('username').should('exist')
197214
```
198215

199216
</TabItem>
200217
</Tabs>
201218

202-
203219
It will NOT find the input node for label text broken up by elements. You can
204220
use `getByRole('textbox', { name: 'Username' })` instead which is robust against
205221
switching to `aria-label` or `aria-labelledby`.
@@ -267,7 +283,6 @@ matches the given [`TextMatch`](#textmatch).
267283
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
268284
<TabItem value="native">
269285

270-
271286
```js
272287
import { screen } from '@testing-library/dom'
273288

@@ -277,7 +292,6 @@ const inputNode = screen.getByPlaceholderText('Username')
277292
</TabItem>
278293
<TabItem value="react">
279294

280-
281295
```jsx
282296
import { render, screen } from '@testing-library/react'
283297

@@ -288,15 +302,13 @@ const inputNode = screen.getByPlaceholderText('Username')
288302
</TabItem>
289303
<TabItem value="cypress">
290304

291-
292305
```js
293306
cy.findByPlaceholderText('Username').should('exist')
294307
```
295308

296309
</TabItem>
297310
</Tabs>
298311

299-
300312
> **Note**
301313
>
302314
> A placeholder is not a good substitute for a label so you should generally use
@@ -330,7 +342,6 @@ matching the given [`TextMatch`](#textmatch).
330342
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
331343
<TabItem value="native">
332344

333-
334345
```js
335346
import { screen } from '@testing-library/dom'
336347

@@ -340,7 +351,6 @@ const aboutAnchorNode = screen.getByText(/about/i)
340351
</TabItem>
341352
<TabItem value="react">
342353

343-
344354
```jsx
345355
import { render, screen } from '@testing-library/react'
346356

@@ -351,15 +361,13 @@ const aboutAnchorNode = screen.getByText(/about/i)
351361
</TabItem>
352362
<TabItem value="cypress">
353363

354-
355364
```js
356365
cy.findByText(/about/i).should('exist')
357366
```
358367

359368
</TabItem>
360369
</Tabs>
361370

362-
363371
It also works with `input`s whose `type` attribute is either `submit` or
364372
`button`:
365373

@@ -412,7 +420,6 @@ as it's deprecated).
412420
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
413421
<TabItem value="native">
414422

415-
416423
```js
417424
import { screen } from '@testing-library/dom'
418425

@@ -422,7 +429,6 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
422429
</TabItem>
423430
<TabItem value="react">
424431

425-
426432
```jsx
427433
import { render, screen } from '@testing-library/react'
428434

@@ -433,15 +439,13 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i)
433439
</TabItem>
434440
<TabItem value="cypress">
435441

436-
437442
```js
438443
cy.findByAltText(/incredibles.*? poster/i).should('exist')
439444
```
440445

441446
</TabItem>
442447
</Tabs>
443448

444-
445449
### `ByTitle`
446450

447451
> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle,
@@ -473,7 +477,6 @@ Will also find a `title` element within an SVG.
473477
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
474478
<TabItem value="native">
475479

476-
477480
```js
478481
import { screen } from '@testing-library/dom'
479482

@@ -484,7 +487,6 @@ const closeElement = screen.getByTitle('Close')
484487
</TabItem>
485488
<TabItem value="react">
486489

487-
488490
```jsx
489491
import { render, screen } from '@testing-library/react'
490492

@@ -496,7 +498,6 @@ const closeElement = screen.getByTitle('Close')
496498
</TabItem>
497499
<TabItem value="cypress">
498500

499-
500501
```js
501502
cy.findByTitle('Delete').should('exist')
502503
cy.findByTitle('Close').should('exist')
@@ -505,7 +506,6 @@ cy.findByTitle('Close').should('exist')
505506
</TabItem>
506507
</Tabs>
507508

508-
509509
### `ByDisplayValue`
510510

511511
> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
@@ -538,7 +538,6 @@ document.getElementById('lastName').value = 'Norris'
538538
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
539539
<TabItem value="native">
540540

541-
542541
```js
543542
import { screen } from '@testing-library/dom'
544543

@@ -548,7 +547,6 @@ const lastNameInput = screen.getByDisplayValue('Norris')
548547
</TabItem>
549548
<TabItem value="react">
550549

551-
552550
```jsx
553551
import { render, screen } from '@testing-library/react'
554552

@@ -559,15 +557,13 @@ const lastNameInput = screen.getByDisplayValue('Norris')
559557
</TabItem>
560558
<TabItem value="cypress">
561559

562-
563560
```js
564561
cy.findByDisplayValue('Norris').should('exist')
565562
```
566563

567564
</TabItem>
568565
</Tabs>
569566

570-
571567
#### `textarea`
572568

573569
```html
@@ -582,7 +578,6 @@ document.getElementById('messageTextArea').value = 'Hello World'
582578
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
583579
<TabItem value="native">
584580

585-
586581
```js
587582
import { screen } from '@testing-library/dom'
588583

@@ -592,7 +587,6 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
592587
</TabItem>
593588
<TabItem value="react">
594589

595-
596590
```jsx
597591
import { render, screen } from '@testing-library/react'
598592

@@ -603,15 +597,13 @@ const messageTextArea = screen.getByDisplayValue('Hello World')
603597
</TabItem>
604598
<TabItem value="cypress">
605599

606-
607600
```js
608601
cy.findByDisplayValue('Hello World').should('exist')
609602
```
610603

611604
</TabItem>
612605
</Tabs>
613606

614-
615607
#### `select`
616608

617609
In case of `select`, this will search for a `<select>` whose selected `<option>`
@@ -630,7 +622,6 @@ matches the given [`TextMatch`](#textmatch).
630622
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
631623
<TabItem value="native">
632624

633-
634625
```js
635626
import { screen } from '@testing-library/dom'
636627

@@ -640,7 +631,6 @@ const selectElement = screen.getByDisplayValue('Alaska')
640631
</TabItem>
641632
<TabItem value="react">
642633

643-
644634
```jsx
645635
import { render, screen } from '@testing-library/react'
646636

@@ -651,15 +641,13 @@ const selectElement = screen.getByDisplayValue('Alaska')
651641
</TabItem>
652642
<TabItem value="cypress">
653643

654-
655644
```js
656645
cy.findByDisplayValue('Alaska').should('exist')
657646
```
658647

659648
</TabItem>
660649
</Tabs>
661650

662-
663651
### `ByRole`
664652

665653
> getByRole, queryByRole, getAllByRole, queryAllByRole, findByRole,
@@ -850,7 +838,6 @@ and which elements can have this state see
850838
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
851839
<TabItem value="native">
852840

853-
854841
```js
855842
import { screen } from '@testing-library/dom'
856843

@@ -860,7 +847,6 @@ const dialogContainer = screen.getByRole('dialog')
860847
</TabItem>
861848
<TabItem value="react">
862849

863-
864850
```jsx
865851
import { render, screen } from '@testing-library/react'
866852

@@ -871,15 +857,13 @@ const dialogContainer = screen.getByRole('dialog')
871857
</TabItem>
872858
<TabItem value="cypress">
873859

874-
875860
```js
876861
cy.findByRole('dialog').should('exist')
877862
```
878863

879864
</TabItem>
880865
</Tabs>
881866

882-
883867
#### `queryFallbacks`
884868

885869
By default, it's assumed that the first role of each element is supported, so
@@ -971,7 +955,6 @@ also accepts a [`TextMatch`](#textmatch)).
971955
label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }>
972956
<TabItem value="native">
973957

974-
975958
```js
976959
import { screen } from '@testing-library/dom'
977960

@@ -981,7 +964,6 @@ const element = screen.getByTestId('custom-element')
981964
</TabItem>
982965
<TabItem value="react">
983966

984-
985967
```jsx
986968
import { render, screen } from '@testing-library/react'
987969

@@ -992,15 +974,13 @@ const element = screen.getByTestId('custom-element')
992974
</TabItem>
993975
<TabItem value="cypress">
994976

995-
996977
```js
997978
cy.findByTestId('custom-element').should('exist')
998979
```
999980

1000981
</TabItem>
1001982
</Tabs>
1002983

1003-
1004984
> In the spirit of [the guiding principles](guiding-principles.mdx), it is
1005985
> recommended to use this only after the other queries don't work for your use
1006986
> case. Using data-testid attributes do not resemble how your software is used

0 commit comments

Comments
 (0)