mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs(test-parameterize): improve forEach example (#31331)
This commit is contained in:
parent
6ae9adfa4f
commit
951040185d
@ -9,13 +9,61 @@ You can either parameterize tests on a test level or on a project level.
|
|||||||
## Parameterized Tests
|
## Parameterized Tests
|
||||||
|
|
||||||
```js title="example.spec.ts"
|
```js title="example.spec.ts"
|
||||||
const people = ['Alice', 'Bob'];
|
[
|
||||||
for (const name of people) {
|
{ name: 'Alice', expected: 'Hello, Alice!' },
|
||||||
test(`testing with ${name}`, async () => {
|
{ name: 'Bob', expected: 'Hello, Bob!' },
|
||||||
|
{ name: 'Charlie', expected: 'Hello, Charlie!' },
|
||||||
|
].forEach(({ name, expected }) => {
|
||||||
|
// You can also do it with test.describe() or with multiple tests as long the test name is unique.
|
||||||
|
test(`testing with ${name}`, async ({ page }) => {
|
||||||
|
await page.goto(`https://example.com/greet?name=${name}`);
|
||||||
|
await expect(page.getByRole('heading')).toHaveText(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Before and after hooks
|
||||||
|
|
||||||
|
Most of the time you should put `beforeEach`, `beforeAll`, `afterEach` and `afterAll` hooks outside of `forEach`, so that hooks are executed just once:
|
||||||
|
|
||||||
|
```js title="example.spec.ts"
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
// ...
|
// ...
|
||||||
});
|
});
|
||||||
// You can also do it with test.describe() or with multiple tests as long the test name is unique.
|
|
||||||
}
|
test.afterEach(async ({ page }) => {
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
|
||||||
|
[
|
||||||
|
{ name: 'Alice', expected: 'Hello, Alice!' },
|
||||||
|
{ name: 'Bob', expected: 'Hello, Bob!' },
|
||||||
|
{ name: 'Charlie', expected: 'Hello, Charlie!' },
|
||||||
|
].forEach(({ name, expected }) => {
|
||||||
|
test(`testing with ${name}`, async ({ page }) => {
|
||||||
|
await page.goto(`https://example.com/greet?name=${name}`);
|
||||||
|
await expect(page.getByRole('heading')).toHaveText(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to have hooks for each test, you can put them inside a `describe()` - so they are executed for each iteration / each invidual test:
|
||||||
|
|
||||||
|
```js title="example.spec.ts"
|
||||||
|
[
|
||||||
|
{ name: 'Alice', expected: 'Hello, Alice!' },
|
||||||
|
{ name: 'Bob', expected: 'Hello, Bob!' },
|
||||||
|
{ name: 'Charlie', expected: 'Hello, Charlie!' },
|
||||||
|
].forEach(({ name, expected }) => {
|
||||||
|
test.describe(() => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await page.goto(`https://example.com/greet?name=${name}`);
|
||||||
|
});
|
||||||
|
test(`testing with ${expected}`, async ({ page }) => {
|
||||||
|
await expect(page.getByRole('heading')).toHaveText(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## Parameterized Projects
|
## Parameterized Projects
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user