Skip to content

Commit 3894585

Browse files
committed
chore: Move assertion, selector, and sync docs to playwright-basics folder for better organization.
1 parent 416ae5f commit 3894585

5 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Playwright Assertions Guide
2+
3+
This guide covers the basics and best practices for using assertions in Playwright tests.
4+
5+
---
6+
7+
## Why Assertions Matter
8+
- Assertions validate expected outcomes in your tests.
9+
- They ensure your app behaves as intended and catch regressions early.
10+
11+
---
12+
13+
## Common Assertion Types
14+
15+
### 1. Page Title
16+
```js
17+
await expect(page).toHaveTitle(/TodoMVC/);
18+
```
19+
20+
### 2. Element Visibility
21+
```js
22+
await expect(page.locator('.new-todo')).toBeVisible();
23+
```
24+
25+
### 3. Element Text
26+
```js
27+
await expect(page.locator('.todo-list li')).toHaveText('Test Item');
28+
```
29+
30+
### 4. Element Count
31+
```js
32+
await expect(page.locator('.todo-list li')).toHaveCount(2);
33+
```
34+
35+
### 5. Attribute Value
36+
```js
37+
await expect(page.locator('.new-todo')).toHaveAttribute('placeholder', 'What needs to be done?');
38+
```
39+
40+
### 6. Element Hidden
41+
```js
42+
await expect(page.locator('.footer')).toBeHidden();
43+
```
44+
45+
---
46+
47+
## Advanced Assertions
48+
- Use `.not` for negative assertions:
49+
```js
50+
await expect(page.locator('.error')).not.toBeVisible();
51+
```
52+
- Combine assertions for complex checks:
53+
```js
54+
await expect(page.locator('.todo-list li')).toHaveText(['First', 'Second']);
55+
```
56+
57+
---
58+
59+
## Best Practices
60+
- Use Playwright's built-in expect for reliability.
61+
- Prefer specific assertions (e.g., toHaveText, toHaveCount) over generic ones.
62+
- Assert before and after actions to validate state changes.
63+
- Keep assertions clear and descriptive.
64+
65+
---
66+
67+
## Example Suite
68+
See `tests/assertions/AssertionBasics.spec.js` for practical assertion examples.
69+
70+
---
71+
72+
**For more, see Playwright docs:** https://playwright.dev/docs/test-assertions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Playwright Selectors Guide
2+
3+
This guide covers the basics and best practices for using selectors in Playwright tests.
4+
5+
---
6+
7+
## Why Selectors Matter
8+
- Selectors identify elements for interaction and assertion.
9+
- Robust selectors make tests reliable and maintainable.
10+
11+
---
12+
13+
## Common Selector Types
14+
15+
### 1. CSS Selector
16+
```js
17+
await page.locator('.new-todo');
18+
```
19+
20+
### 2. Text Selector
21+
```js
22+
await page.getByText('todos');
23+
```
24+
25+
### 3. Role Selector
26+
```js
27+
await page.getByRole('textbox', { name: '' });
28+
```
29+
30+
### 4. Chained Selector
31+
```js
32+
await page.locator('.todo-list').locator('li');
33+
```
34+
35+
### 5. Nth Selector
36+
```js
37+
await page.locator('.todo-list li').nth(1);
38+
```
39+
40+
### 6. Attribute Selector
41+
```js
42+
await page.locator('input[placeholder="What needs to be done?"]');
43+
```
44+
45+
---
46+
47+
## Advanced Selector Strategies
48+
- Use data-testid or data-role attributes for stable selectors.
49+
- Combine selectors for complex queries:
50+
```js
51+
await page.locator('.parent .child[data-active="true"]');
52+
```
53+
- Use Playwright's built-in selector engines for flexibility.
54+
55+
---
56+
57+
## Best Practices
58+
- Prefer stable, unique selectors (avoid brittle ones).
59+
- Use role and text selectors for accessibility and clarity.
60+
- Chain selectors for scoped queries.
61+
- Avoid using index-based selectors unless necessary.
62+
63+
---
64+
65+
## Example Suite
66+
See `tests/selectors/SelectorBasics.spec.js` for practical selector examples.
67+
68+
---
69+
70+
**For more, see Playwright docs:** https://playwright.dev/docs/selectors
File renamed without changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { test, expect } = require('@playwright/test');
2+
3+
const DEMO_URL = 'https://demo.playwright.dev/todomvc';
4+
5+
test.describe('Assertion Basics', () => {
6+
test.beforeEach(async ({ page }) => {
7+
await page.goto(DEMO_URL);
8+
});
9+
10+
test('should assert page title', async ({ page }) => {
11+
await expect(page).toHaveTitle(/TodoMVC/);
12+
});
13+
14+
test('should assert element is visible', async ({ page }) => {
15+
await expect(page.locator('.new-todo')).toBeVisible();
16+
});
17+
18+
test('should assert element has text', async ({ page }) => {
19+
await page.fill('.new-todo', 'Test Item');
20+
await page.keyboard.press('Enter');
21+
await expect(page.locator('.todo-list li')).toHaveText('Test Item');
22+
});
23+
24+
test('should assert element count', async ({ page }) => {
25+
await page.fill('.new-todo', 'First');
26+
await page.keyboard.press('Enter');
27+
await page.fill('.new-todo', 'Second');
28+
await page.keyboard.press('Enter');
29+
await expect(page.locator('.todo-list li')).toHaveCount(2);
30+
});
31+
32+
test('should assert attribute value', async ({ page }) => {
33+
await expect(page.locator('.new-todo')).toHaveAttribute('placeholder', 'What needs to be done?');
34+
});
35+
36+
test('should assert element is hidden', async ({ page }) => {
37+
await expect(page.locator('.footer')).toBeHidden(); // Footer is hidden until todos are added
38+
});
39+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { test, expect } = require('@playwright/test');
2+
3+
const DEMO_URL = 'https://demo.playwright.dev/todomvc';
4+
5+
test.describe('Selector Basics', () => {
6+
test.beforeEach(async ({ page }) => {
7+
await page.goto(DEMO_URL);
8+
});
9+
10+
test('CSS selector', async ({ page }) => {
11+
await expect(page.locator('.new-todo')).toBeVisible();
12+
});
13+
14+
test('Text selector', async ({ page }) => {
15+
await expect(page.getByText('todos')).toBeVisible();
16+
});
17+
18+
test('Role selector', async ({ page }) => {
19+
await expect(page.getByRole('textbox', { name: '' })).toBeVisible();
20+
});
21+
22+
test('Chained selector', async ({ page }) => {
23+
await page.fill('.new-todo', 'Chained');
24+
await page.keyboard.press('Enter');
25+
await expect(page.locator('.todo-list').locator('li')).toHaveText('Chained');
26+
});
27+
28+
test('Nth selector', async ({ page }) => {
29+
await page.fill('.new-todo', 'First');
30+
await page.keyboard.press('Enter');
31+
await page.fill('.new-todo', 'Second');
32+
await page.keyboard.press('Enter');
33+
await expect(page.locator('.todo-list li').nth(1)).toHaveText('Second');
34+
});
35+
36+
test('Attribute selector', async ({ page }) => {
37+
await expect(page.locator('input[placeholder="What needs to be done?"]')).toBeVisible();
38+
});
39+
});

0 commit comments

Comments
 (0)