Skip to content

Commit af28a77

Browse files
authored
docs: migrate JS assertions over to Java/Python assertions (microsoft#10431)
1 parent af4a1c2 commit af28a77

File tree

8 files changed

+195
-271
lines changed

8 files changed

+195
-271
lines changed

docs/src/api/class-locatorassertions.md

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# class: LocatorAssertions
2-
* langs: java, python
2+
* langs: java, python, js
33

4-
The [LocatorAssertions] class provides assertion methods that can be used to make assertions about the [Locator] state in the tests. A new instance of [LocatorAssertions] is created by calling [`method: PlaywrightAssertions.assertThatLocator`]:
4+
The [LocatorAssertions] class provides assertion methods that can be used to make assertions about the [Locator] state in the tests. A new instance of [LocatorAssertions] is created by calling [`method: PlaywrightAssertions.expectLocator`]:
55

66
```java
77
...
@@ -19,7 +19,7 @@ public class TestLocator {
1919
```
2020

2121
## method: LocatorAssertions.not
22-
* langs: java
22+
* langs: java, js
2323
- returns: <[LocatorAssertions]>
2424

2525
Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text `"error"`:
@@ -225,6 +225,11 @@ Expected value.
225225

226226
Ensures the [Locator] points to a checked input.
227227

228+
```js
229+
const locator = page.locator('.subscribe');
230+
await expect(locator).toBeChecked();
231+
```
232+
228233
```java
229234
assertThat(page.locator(".subscribe")).isChecked();
230235
```
@@ -239,6 +244,11 @@ assertThat(page.locator(".subscribe")).isChecked();
239244

240245
Ensures the [Locator] points to a disabled element.
241246

247+
```js
248+
const locator = page.locator('button.submit');
249+
await expect(locator).toBeDisabled();
250+
```
251+
242252
```java
243253
assertThat(page.locator("button.submit")).isDisabled();
244254
```
@@ -252,6 +262,11 @@ assertThat(page.locator("button.submit")).isDisabled();
252262

253263
Ensures the [Locator] points to an editable element.
254264

265+
```js
266+
const locator = page.locator('input');
267+
await expect(locator).toBeEditable();
268+
```
269+
255270
```java
256271
assertThat(page.locator("input")).isEditable();
257272
```
@@ -265,6 +280,11 @@ assertThat(page.locator("input")).isEditable();
265280

266281
Ensures the [Locator] points to an empty editable element or to a DOM node that has no text.
267282

283+
```js
284+
const locator = page.locator('div.warning');
285+
await expect(locator).toBeEmpty();
286+
```
287+
268288
```java
269289
assertThat(page.locator("div.warning")).isEmpty();
270290
```
@@ -278,6 +298,11 @@ assertThat(page.locator("div.warning")).isEmpty();
278298

279299
Ensures the [Locator] points to an enabled element.
280300

301+
```js
302+
const locator = page.locator('button.submit');
303+
await expect(locator).toBeEnabled();
304+
```
305+
281306
```java
282307
assertThat(page.locator("button.submit")).isEnabled();
283308
```
@@ -291,6 +316,11 @@ assertThat(page.locator("button.submit")).isEnabled();
291316

292317
Ensures the [Locator] points to a focused DOM node.
293318

319+
```js
320+
const locator = page.locator('input');
321+
await expect(locator).toBeFocused();
322+
```
323+
294324
```java
295325
assertThat(page.locator("input")).isFocused();
296326
```
@@ -304,6 +334,11 @@ assertThat(page.locator("input")).isFocused();
304334

305335
Ensures the [Locator] points to a hidden DOM node, which is the opposite of [visible](./actionability.md#visible).
306336

337+
```js
338+
const locator = page.locator('.my-element');
339+
await expect(locator).toBeHidden();
340+
```
341+
307342
```java
308343
assertThat(page.locator(".my-element")).isHidden();
309344
```
@@ -317,6 +352,11 @@ assertThat(page.locator(".my-element")).isHidden();
317352

318353
Ensures the [Locator] points to a [visible](./actionability.md#visible) DOM node.
319354

355+
```js
356+
const locator = page.locator('.my-element');
357+
await expect(locator).toBeVisible();
358+
```
359+
320360
```java
321361
assertThat(page.locator(".my-element")).toBeVisible();
322362
```
@@ -329,18 +369,29 @@ assertThat(page.locator(".my-element")).toBeVisible();
329369

330370
Ensures the [Locator] points to an element that contains the given text. You can use regular expressions for the value as well.
331371

372+
```js
373+
const locator = page.locator('.title');
374+
await expect(locator).toContainText('substring');
375+
await expect(locator).toContainText(/\d messages/);
376+
```
377+
332378
```java
333379
assertThat(page.locator(".title")).containsText("substring");
334380
```
335381

336382
Note that if array is passed as an expected value, entire lists can be asserted:
337383

384+
```js
385+
const locator = page.locator('list > .list-item');
386+
await expect(locator).toContainText(['Text 1', 'Text 4', 'Text 5']);
387+
```
388+
338389
```java
339390
assertThat(page.locator("list > .list-item")).containsText(new String[] {"Text 1", "Text 4", "Text 5"});
340391
```
341392

342393
### param: LocatorAssertions.toContainText.expected
343-
* langs: python
394+
* langs: python, js
344395
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
345396

346397
Expected substring or RegExp or a list of those.
@@ -365,6 +416,11 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
365416

366417
Ensures the [Locator] points to an element with given attribute.
367418

419+
```js
420+
const locator = page.locator('input');
421+
await expect(locator).toHaveAttribute('type', 'text');
422+
```
423+
368424
```java
369425
assertThat(page.locator("input")).hasAttribute("type", "text");
370426
```
@@ -387,18 +443,28 @@ Expected attribute value.
387443

388444
Ensures the [Locator] points to an element with given CSS class.
389445

446+
```js
447+
const locator = page.locator('#component');
448+
await expect(locator).toHaveClass(/selected/);
449+
```
450+
390451
```java
391452
assertThat(page.locator("#component")).hasClass(Pattern.compile("selected"));
392453
```
393454

394455
Note that if array is passed as an expected value, entire lists can be asserted:
395456

457+
```js
458+
const locator = page.locator('list > .component');
459+
await expect(locator).toHaveClass(['component', 'component selected', 'component']);
460+
```
461+
396462
```java
397463
assertThat(page.locator("list > .component")).hasClass(new String[] {"component", "component selected", "component"});
398464
```
399465

400466
### param: LocatorAssertions.toHaveClass.expected
401-
* langs: python
467+
* langs: python, js
402468
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
403469

404470
Expected class or RegExp or a list of those.
@@ -418,6 +484,11 @@ Expected class or RegExp or a list of those.
418484

419485
Ensures the [Locator] resolves to an exact number of DOM nodes.
420486

487+
```js
488+
const list = page.locator('list > .component');
489+
await expect(list).toHaveCount(3);
490+
```
491+
421492
```java
422493
assertThat(page.locator("list > .component")).hasCount(3);
423494
```
@@ -435,6 +506,11 @@ Expected count.
435506

436507
Ensures the [Locator] resolves to an element with the given computed CSS style.
437508

509+
```js
510+
const locator = page.locator('button');
511+
await expect(locator).toHaveCSS('display', 'flex');
512+
```
513+
438514
```java
439515
assertThat(page.locator("button")).hasCSS("display", "flex");
440516
```
@@ -457,6 +533,11 @@ CSS property value.
457533

458534
Ensures the [Locator] points to an element with the given DOM Node ID.
459535

536+
```js
537+
const locator = page.locator('input');
538+
await expect(locator).toHaveId('lastname');
539+
```
540+
460541
```java
461542
assertThat(page.locator("input")).hasId("lastname");
462543
```
@@ -473,7 +554,13 @@ Element id.
473554
* langs:
474555
- alias-java: hasJSProperty
475556

476-
Ensures the [Locator] points to an element with given JavaScript property. Note that this property can be of a primitive type as well as a plain serializable JavaScript object.
557+
Ensures the [Locator] points to an element with given JavaScript property. Note that this property can be
558+
of a primitive type as well as a plain serializable JavaScript object.
559+
560+
```js
561+
const locator = page.locator('.component');
562+
await expect(locator).toHaveJSProperty('loaded', true);
563+
```
477564

478565
```java
479566
assertThat(page.locator("input")).hasJSProperty("type", "text");
@@ -497,19 +584,30 @@ Property value.
497584

498585
Ensures the [Locator] points to an element with the given text. You can use regular expressions for the value as well.
499586

587+
```js
588+
const locator = page.locator('.title');
589+
await expect(locator).toHaveText(/Welcome, Test User/);
590+
await expect(locator).toHaveText(/Welcome, .*/);
591+
```
592+
500593
```java
501594
assertThat(page.locator(".title")).hasText("Welcome, Test User");
502595
assertThat(page.locator(".title")).hasText(Pattern.compile("Welcome, .*"));
503596
```
504597

505598
Note that if array is passed as an expected value, entire lists can be asserted:
506599

600+
```js
601+
const locator = page.locator('list > .component');
602+
await expect(locator).toHaveText(['Text 1', 'Text 2', 'Text 3']);
603+
```
604+
507605
```java
508606
assertThat(page.locator("list > .component")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});
509607
```
510608

511609
### param: LocatorAssertions.toHaveText.expected
512-
* langs: python
610+
* langs: python, js
513611
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
514612

515613
Expected substring or RegExp or a list of those.
@@ -533,6 +631,11 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
533631

534632
Ensures the [Locator] points to an element with the given input value. You can use regular expressions for the value as well.
535633

634+
```js
635+
const locator = page.locator('input[type=number]');
636+
await expect(locator).toHaveValue(/[0-9]/);
637+
```
638+
536639
```java
537640
assertThat(page.locator("input[type=number]")).hasValue(Pattern.compile("[0-9]"));
538641
```

docs/src/api/class-pageassertions.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# class: PageAssertions
2-
* langs: java, python
2+
* langs: java, python, js
33

4-
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [LocatorAssertions] is created by calling [`method: PlaywrightAssertions.assertThatPage`]:
4+
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [LocatorAssertions] is created by calling [`method: PlaywrightAssertions.expectPage`]:
55

66
```java
77
...
@@ -20,7 +20,7 @@ public class TestPage {
2020

2121

2222
## method: PageAssertions.not
23-
* langs: java
23+
* langs: java, js
2424
- returns: <[PageAssertions]>
2525

2626
Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain `"error"`:
@@ -61,6 +61,10 @@ Expected substring or RegExp.
6161

6262
Ensures the page has the given title.
6363

64+
```js
65+
await expect(page).toHaveTitle(/.*checkout/);
66+
```
67+
6468
```java
6569
assertThat(page).hasTitle("Playwright");
6670
```
@@ -78,6 +82,10 @@ Expected title or RegExp.
7882

7983
Ensures the page is navigated to the given URL.
8084

85+
```js
86+
await expect(page).toHaveURL(/.*checkout/);
87+
```
88+
8189
```java
8290
assertThat(page).hasURL(".com");
8391
```

0 commit comments

Comments
 (0)