Skip to content

Commit f24814e

Browse files
authored
test(button): added playwright screenshot tests (#DS-3763) (#795)
Co-authored-by: artembelik <24925224+artembelik@users.noreply.github.com>
1 parent 3ebdbda commit f24814e

22 files changed

+483
-1116
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# This workflow updates the E2E screenshots when a comment with `/e2e-update-screenshots` is added to a pull request.
3+
#
4+
name: E2E update screenshots
5+
6+
on:
7+
issue_comment:
8+
types: [created, edited]
9+
10+
permissions:
11+
contents: write
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
update_screenshots:
19+
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/e2e-update-screenshots') }}
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 10
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Setup yarn
27+
uses: ./.github/workflows/actions/setup-yarn
28+
29+
- name: Setup test environment
30+
run: yarn run e2e:setup
31+
32+
- name: Update screenshots
33+
run: yarn run e2e:components --update-snapshots
34+
35+
- name: Commit and push screenshots
36+
uses: stefanzweifel/git-auto-commit-action@v5
37+
with:
38+
commit_message: 'test: auto-updated e2e screenshots'
39+
file_pattern: 'e2e/**/*.png'

.github/workflows/e2e.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: E2E tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 10
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Setup yarn
22+
uses: ./.github/workflows/actions/setup-yarn
23+
24+
- name: Setup test environment
25+
run: yarn run e2e:setup
26+
27+
- name: Run e2e tests
28+
run: yarn run e2e:components
29+
30+
- uses: actions/upload-artifact@v4
31+
if: ${{ !cancelled() }}
32+
with:
33+
name: playwright-report
34+
path: playwright-report/
35+
retention-days: 1

.gitignore

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,20 @@ dist
165165
# Stores VSCode versions used for testing VSCode extensions
166166
.vscode-test
167167

168-
# yarn v2
169-
.yarn/cache
170-
.yarn/unplugged
171-
.yarn/build-state.yml
172-
.yarn/install-state.gz
168+
# yarn
173169
.pnp.*
170+
.yarn/*
171+
!.yarn/README.md
172+
!.yarn/patches
173+
!.yarn/plugins
174+
!.yarn/releases
175+
!.yarn/sdks
176+
!.yarn/versions
174177

175178
.gitconfig
179+
180+
# Playwright
181+
/test-results/
182+
/playwright-report/
183+
/blob-report/
184+
/playwright/.cache/

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"stylelint.vscode-stylelint",
66
"dbaeumer.vscode-eslint",
77
"angular.ng-template",
8-
"editorconfig.editorconfig"
8+
"editorconfig.editorconfig",
9+
"ms-playwright.playwright"
910
]
1011
}

.vscode/mcp.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"servers": {
3+
"playwright": {
4+
"command": "npx",
5+
"args": [
6+
"@playwright/mcp@latest"
7+
]
8+
}
9+
}
10+
}

angular.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,36 @@
885885
}
886886
}
887887
},
888+
"dev-e2e": {
889+
"projectType": "application",
890+
"root": "packages/components-dev/e2e",
891+
"sourceRoot": "packages/components-dev/e2e",
892+
"architect": {
893+
"build": {
894+
"builder": "@angular-devkit/build-angular:application",
895+
"options": {
896+
"outputPath": {
897+
"base": "dist/components-dev/e2e"
898+
},
899+
"tsConfig": "packages/components-dev/e2e/tsconfig.app.json",
900+
"index": "packages/components-dev/index.html",
901+
"styles": ["packages/components-dev/main.scss"],
902+
"polyfills": ["zone.js"],
903+
"extractLicenses": false,
904+
"sourceMap": true,
905+
"optimization": false,
906+
"namedChunks": true,
907+
"browser": "packages/components-dev/e2e/main.ts"
908+
}
909+
},
910+
"serve": {
911+
"builder": "@angular-devkit/build-angular:dev-server",
912+
"options": {
913+
"buildTarget": "dev-e2e:build"
914+
}
915+
}
916+
}
917+
},
888918
"dev-empty-state": {
889919
"projectType": "application",
890920
"root": "packages/components-dev/empty-state",

docs/guides/06-testing.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,42 @@
22

33
## Unit tests
44

5-
Runs tests for package
5+
Unit tests check individual modules and components of the application in isolation. They are run using
6+
[jest](https://jestjs.io/) and [Karma](https://karma-runner.github.io/).
7+
8+
### Setup
9+
10+
```bash
11+
yarn install
12+
```
13+
14+
### Available commands
615

716
```bash
17+
yarn run unit:cdk
818
yarn run unit:components
19+
yarn run unit:components-experimental
20+
yarn run unit:angular-moment-adapter
21+
yarn run unit:angular-luxon-adapter
22+
yarn run unit:schematics
23+
yarn run unit:koobiq-docs
24+
yarn run unit:api-gen
25+
```
26+
27+
## E2E tests
28+
29+
E2E (end-to-end) tests check the application as a whole, simulating user interactions. They are run using
30+
[Playwright](https://playwright.dev/).
31+
32+
### Setup
33+
34+
```bash
35+
yarn install
36+
yarn run e2e:setup
937
```
1038

11-
Runs specific spec file
39+
### Available commands
1240

1341
```bash
14-
yarn jest packages/components/alert/alert.component.spec.ts
42+
yarn run e2e:components
1543
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, Locator, Page, test } from '@playwright/test';
2+
3+
const component = {
4+
stateAndStyle: 'dev-button-state-and-style'
5+
};
6+
7+
test.describe('KbqButtonModule', () => {
8+
test.describe(component.stateAndStyle, () => {
9+
const goToPage = (page: Page) => page.goto('');
10+
const getComponent = (page: Page) => page.locator(component.stateAndStyle);
11+
const togglePrefix = (locator: Locator) => locator.getByText('show prefix icon').click();
12+
const toggleTitle = (locator: Locator) => locator.getByText('show title').click();
13+
const toggleSuffix = (locator: Locator) => locator.getByText('show suffix icon').click();
14+
const getScreenshotTarget = (locator: Locator) => locator.locator('table');
15+
16+
test('button with title', async ({ page }) => {
17+
await goToPage(page);
18+
const locator = getComponent(page);
19+
20+
await expect(getScreenshotTarget(locator)).toHaveScreenshot();
21+
});
22+
23+
test('button with icon', async ({ page }) => {
24+
await goToPage(page);
25+
const locator = getComponent(page);
26+
27+
await togglePrefix(locator);
28+
await toggleTitle(locator);
29+
30+
await expect(getScreenshotTarget(locator)).toHaveScreenshot();
31+
});
32+
33+
test('button with title, prefix and suffix', async ({ page }) => {
34+
await goToPage(page);
35+
const locator = getComponent(page);
36+
37+
await togglePrefix(locator);
38+
await toggleSuffix(locator);
39+
40+
await expect(getScreenshotTarget(locator)).toHaveScreenshot();
41+
});
42+
});
43+
});

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@messageformat/core": "^3.0.1",
7171
"@microsoft/api-extractor": "7.40.2",
7272
"@octokit/rest": "^18.9.1",
73+
"@playwright/test": "^1.52.0",
7374
"@prettier/plugin-xml": "^3.4.1",
7475
"@rollup/plugin-commonjs": "^24.0.0",
7576
"@rollup/plugin-json": "^6.0.0",
@@ -162,6 +163,7 @@
162163
"scripts": {
163164
"ng": "ng",
164165
"prepare": "husky",
166+
"clean": "playwright clear-cache && rimraf .angular dist playwright-report test-results --verbose",
165167
"postinstall": "tsc -p tools/builders/tsconfig.json",
166168
"build:cdk": "ng build cdk",
167169
"build:angular-moment-adapter": "ng build angular-moment-adapter",
@@ -211,6 +213,7 @@
211213
"dev:divider": "ng serve dev-divider --port 3003",
212214
"dev:dl": "ng serve dev-dl --port 3003",
213215
"dev:dropdown": "ng serve dev-dropdown --port 3003",
216+
"dev:e2e": "ng serve dev-e2e",
214217
"dev:empty-state": "ng serve dev-empty-state --port 3003",
215218
"dev:experimental-form-field": "ng serve dev-experimental-form-field --port 3003",
216219
"dev:file-upload": "ng serve dev-file-upload --port 3003",
@@ -267,6 +270,9 @@
267270
"unit:schematics": "jest --roots packages/schematics",
268271
"unit:koobiq-docs": "ng test koobiq-docs",
269272
"unit:api-gen": "jest --roots tools/api-gen",
273+
"-----E2E_TESTS-----": "--------------------------------------------------------------------------------------",
274+
"e2e:setup": "playwright install chromium --with-deps",
275+
"e2e:components": "playwright test e2e/components",
270276
"-----CI-----": "---------------------------------------------------------------------------------------------",
271277
"approve-api": "ts-node --project tools/api-extractor/tsconfig.json tools/api-extractor/api-extractor.ts",
272278
"check-api": "yarn run approve-api onlyCheck",

0 commit comments

Comments
 (0)