-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from cassus/component-tests
feat: support component tests
- Loading branch information
Showing
22 changed files
with
8,746 additions
and
13,726 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dist | ||
.features-gen | ||
.cache | ||
playwright-report | ||
**/*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,10 @@ Have a look on [this section](https://vitalets.github.io/playwright-bdd/#/faq?id | |
#### Test locally on different Playwright versions | ||
``` | ||
# install needed Playwright version | ||
npm i --no-save @playwright/test@1.33 | ||
npm i --no-save @playwright/test@1.40 @playwright/[email protected] | ||
# install corresponding chromium without clearing other versions | ||
npx cross-env PLAYWRIGHT_SKIP_BROWSER_GC=1 playwright install chromium | ||
npx cross-env PLAYWRIGHT_SKIP_BROWSER_GC=1 npx playwright install chromium | ||
# run all tests | ||
npm run test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Component tests | ||
Playwright-bdd supports [component tests](https://playwright.dev/docs/test-components) | ||
since Playwright **1.39**. | ||
|
||
Initialize components testing [per instruction](https://playwright.dev/docs/test-components#how-to-get-started): | ||
``` | ||
npm init playwright@latest -- --ct | ||
``` | ||
|
||
Add `playwright-bdd` configuration to `playwright-ct.config.ts`: | ||
```ts | ||
import { defineConfig, devices } from '@playwright/experimental-ct-react'; | ||
import { defineBddConfig } from 'playwright-bdd'; | ||
|
||
const testDir = defineBddConfig({ | ||
importTestFrom: 'fixtures.ts', | ||
paths: ['features/*.feature'], | ||
require: ['steps.tsx'], | ||
}); | ||
|
||
export default defineConfig({ | ||
testDir, | ||
// ... | ||
}); | ||
``` | ||
|
||
Create `fixtures.ts` that exports custom `test`: | ||
```ts | ||
import { mergeTests } from '@playwright/test'; | ||
import { test as ctBase } from '@playwright/experimental-ct-react'; | ||
import { test as base } from 'playwright-bdd'; | ||
|
||
export const test = mergeTests(base, ctBase); | ||
``` | ||
|
||
Define steps in `steps.tsx`: | ||
```ts | ||
import React from 'react'; | ||
import { expect } from '@playwright/test'; | ||
import { createBdd } from 'playwright-bdd'; | ||
import { test } from './fixtures'; | ||
|
||
const { Given, When, Then } = createBdd(test); | ||
|
||
Given('Mounted input component', async ({ mount }) => { | ||
await mount(<textarea data-testid="textField" />); | ||
}); | ||
|
||
When('I type {string}', async ({ page }, arg: string) => { | ||
await page.getByTestId('textField').fill(arg); | ||
}); | ||
|
||
Then('input field has {string}', async ({ page }, arg: string) => { | ||
await expect(page.getByTestId('textField')).toHaveValue(arg); | ||
}); | ||
``` | ||
|
||
Create feature file `input.feature`: | ||
```gherkin | ||
Feature: input component | ||
Scenario: Mount component and interact with it | ||
Given Mounted input component | ||
When I type "ABC" | ||
Then input field has "ABC" | ||
``` | ||
|
||
Update `test-ct` command in `package.json` to run `bddgen` before running Playwright: | ||
```json | ||
"scripts": { | ||
"test-ct": "npx bddgen -c playwright-ct.config.ts && playwright test -c playwright-ct.config.ts" | ||
}, | ||
``` | ||
|
||
Finally, run tests: | ||
``` | ||
npm run test-ct | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.