Skip to content

Commit c33f950

Browse files
committed
Add playwright initial setup and tests
1 parent 22d8473 commit c33f950

File tree

9 files changed

+1013
-9
lines changed

9 files changed

+1013
-9
lines changed

.github/workflows/playwright copy.yml

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
name: Playwright Test Preparation
2+
3+
pull_request:
4+
types: [opened, synchronize, reopened, ready_for_review]
5+
6+
jobs:
7+
get-changed-files:
8+
runs-on: ubuntu-latest
9+
outputs:
10+
changed-files: ${{ steps.changed_files.outputs.changed-files }}
11+
steps:
12+
- name: Get Changed Files
13+
id: changed_files
14+
uses: actions/github-script@v6
15+
with:
16+
script: |
17+
const { context } = require('@actions/github');
18+
const fs = require('fs');
19+
const changedFiles = fs.readFileSync(context.payload.pull_request.patch_url, 'utf-8')
20+
.match(/^\+(?:\s*\w+\/)*(\S+\.ts)$/gm)
21+
.map(line => line.replace('+', '').trim());
22+
core.setOutput('changed-files', JSON.stringify(changedFiles));
23+
24+
analyze-and-trigger:
25+
runs-on: ubuntu-latest
26+
needs: get-changed-files
27+
steps:
28+
- name: Call Test Selection API
29+
id: test_selection
30+
run: |
31+
changed_files=${{ steps.changed_files.outputs.changed-files }}
32+
tests=$(curl -X POST 'https://tests-selection.azurewebsites.net/api' \
33+
-H 'Content-Type: application/json' \
34+
-d "$changed_files")
35+
echo "::set-output name=selected-tests::$tests"
36+
37+
- name: Analyze Test Timings and Determine Shard Count
38+
uses: actions/github-script@v6
39+
run: |
40+
const testsData = JSON.parse('${{ steps.test_selection.outputs.selected-tests }}');
41+
const totalTestDuration = testsData.reduce((sum, test) => sum + test.duration, 0);
42+
const targetDurationPerShard = 5 * 60; // assuming 5 minutes per shard
43+
const optimalShardCount = Math.ceil(totalTestDuration / targetDurationPerShard);
44+
const shardIndices = [];
45+
for (let i = 1; i <= optimalShardCount; i++) {
46+
shardIndices.push(i);
47+
}
48+
core.setOutput('optimal_shard_count', optimalShardCount);
49+
core.setOutput('shard_indices', JSON.stringify(shardIndices));
50+
51+
- name: Trigger Workflow 2 with Shard Count
52+
uses: benc-uk/workflow-dispatch@v1
53+
with:
54+
workflow: playwright.yml
55+
inputs: '{"shardCount": "${{ optimal_shard_count }}"}'
56+
57+
58+
name: Run Playwright Tests
59+
60+
on:
61+
workflow_dispatch:
62+
inputs:
63+
shardCount:
64+
description: 'Number of shards to use'
65+
required: true
66+
type: string
67+
shardIndices:
68+
description: 'Array of shard indices to execute'
69+
required: true
70+
type: string
71+
72+
jobs:
73+
check-dependencies:
74+
runs-on: ubuntu-latest
75+
needs: get-changed-files
76+
outputs:
77+
should-install-fresh-deps: ${{ steps.check_deps.outputs.should-install }}
78+
steps:
79+
- name: Check if package files changed
80+
id: check_deps
81+
run: |
82+
changed_files=(${CHANGED_FILES})
83+
if [[ " ${changed_files[*]} " =~ "package.json" || " ${changed_files[*]} " =~ "package-lock.json" ]]; then
84+
echo "::set-output name=should-install::true"
85+
else
86+
echo "::set-output name=should-install::false"
87+
fi
88+
89+
install-and-cache-node-deps:
90+
runs-on: ubuntu-latest
91+
needs: check-dependencies
92+
if: steps.check_deps.outputs.should-install == 'true'
93+
steps:
94+
- name: Install dependencies (Vue app)
95+
working-directory: vue-app
96+
run: npm ci
97+
98+
- name: Cache Node Dependencies
99+
uses: actions/cache@v3
100+
with:
101+
path: vue-app/node_modules
102+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
103+
restore-keys: |
104+
${{ runner.os }}-node-
105+
106+
build-and-start-vue:
107+
runs-on: ubuntu-latest
108+
needs: check-dependencies
109+
if: steps.check_deps.outputs.should-install == 'false'
110+
steps:
111+
- name: Use cached Node dependencies
112+
uses: actions/cache@v3
113+
with:
114+
path: vue-app/node_modules
115+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
116+
restore-keys: |
117+
${{ runner.os }}-node-
118+
119+
- name: Start Vue app
120+
run: npm run dev & # Runs in background
121+
122+
- name: Make sure Vue server is up and running
123+
run: |
124+
until $(curl --output /dev/null --silent --head --fail http://localhost:3000); do
125+
printf '.'
126+
sleep 3
127+
done
128+
echo "Vue server is up and running!"
129+
130+
playwright-setup:
131+
runs-on: ubuntu-latest
132+
steps:
133+
- name: Cache Playwright Dependencies
134+
uses: actions/cache@v3
135+
with:
136+
path: ~/.cache/ms-playwright
137+
key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}
138+
restore-keys: |
139+
${{ runner.os }}-playwright-
140+
141+
- name: Install Playwright (if not cached)
142+
if: steps.cache.outputs.cache-hit != 'true'
143+
run: npx playwright install --with-deps
144+
145+
run-playwright-tests:
146+
runs-on: ubuntu-latest
147+
strategy:
148+
fail-fast: false
149+
matrix:
150+
shardIndex: ${{ fromJson(github.event.inputs.outputs.shard_indices) }}
151+
shardTotal: [ ${{ github.event.inputs.shardCount }} ]
152+
timeout-minutes: 10
153+
needs: [call-test-selection-api, get-changed-files, playwright-setup, build-and-start-vue]
154+
env:
155+
CI: true
156+
steps:
157+
- uses: actions/checkout@v3
158+
- uses: actions/setup-node@v3
159+
with:
160+
node-version: 18
161+
162+
- name: Run Playwright Tests
163+
run: |
164+
tests=${{ steps.test_selection.outputs.selected-tests }}
165+
npx playwright test $tests --max-failures=2 --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}

.github/workflows/playwright.yml

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: Clear Playwright Cache
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 */7 * *' # runs weekly
6+
7+
jobs:
8+
clear-cache:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/cache@v3
12+
with:
13+
path: ~/.cache/ms-playwright
14+
restore-keys: |
15+
${{ runner.os }}-playwright-
16+
17+
18+
name: run e2e tests for vue
19+
20+
pull_request:
21+
types: [opened, synchronize, reopened, ready_for_review]
22+
23+
jobs:
24+
get-changed-files:
25+
runs-on: ubuntu-latest
26+
outputs:
27+
changed-files: ${{ steps.changed_files.outputs.changed-files }}
28+
steps:
29+
- name: Get Changed Files
30+
id: changed_files
31+
uses: actions/github-script@v6
32+
with:
33+
script: |
34+
const { context } = require('@actions/github');
35+
const fs = require('fs');
36+
const changedFiles = fs.readFileSync(context.payload.pull_request.patch_url, 'utf-8')
37+
.match(/^\+(?:\s*\w+\/)*(\S+\.ts)$/gm)
38+
.map(line => line.replace('+', '').trim());
39+
core.setOutput('changed-files', JSON.stringify(changedFiles));
40+
41+
check-dependencies:
42+
runs-on: ubuntu-latest
43+
needs: get-changed-files
44+
outputs:
45+
should-install-fresh-deps: ${{ steps.check_deps.outputs.should-install }}
46+
steps:
47+
- name: Check if package files changed
48+
id: check_deps
49+
run: |
50+
changed_files=(${CHANGED_FILES})
51+
if [[ " ${changed_files[*]} " =~ "package.json" || " ${changed_files[*]} " =~ "package-lock.json" ]]; then
52+
echo "::set-output name=should-install::true"
53+
else
54+
echo "::set-output name=should-install::false"
55+
fi
56+
57+
install-and-cache-node-deps:
58+
runs-on: ubuntu-latest
59+
needs: check-dependencies
60+
if: steps.check_deps.outputs.should-install == 'true'
61+
steps:
62+
- name: Install dependencies (Vue app)
63+
working-directory: vue-app
64+
run: npm ci
65+
66+
- name: Cache Node Dependencies
67+
uses: actions/cache@v3
68+
with:
69+
path: vue-app/node_modules
70+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
71+
restore-keys: |
72+
${{ runner.os }}-node-
73+
74+
build-and-start-vue:
75+
runs-on: ubuntu-latest
76+
needs: check-dependencies
77+
if: steps.check_deps.outputs.should-install == 'false'
78+
steps:
79+
- name: Use cached Node dependencies
80+
uses: actions/cache@v3
81+
with:
82+
path: vue-app/node_modules
83+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
84+
restore-keys: |
85+
${{ runner.os }}-node-
86+
87+
- name: Start Vue app
88+
run: npm run dev & # Runs in background
89+
90+
- name: Make sure Vue server is up and running
91+
run: |
92+
until $(curl --output /dev/null --silent --head --fail http://localhost:3000); do
93+
printf '.'
94+
sleep 3
95+
done
96+
echo "Vue server is up and running!"
97+
98+
playwright-setup:
99+
runs-on: ubuntu-latest
100+
steps:
101+
- name: Cache Playwright Dependencies
102+
uses: actions/cache@v3
103+
with:
104+
path: ~/.cache/ms-playwright
105+
key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}
106+
restore-keys: |
107+
${{ runner.os }}-playwright-
108+
109+
- name: Install Playwright (if not cached)
110+
if: steps.cache.outputs.cache-hit != 'true'
111+
run: npx playwright install --with-deps
112+
113+
call-test-selection-api:
114+
runs-on: ubuntu-latest
115+
needs: [get-changed-files]
116+
steps:
117+
- name: Call the Test Selection API
118+
id: test_selection
119+
run: |
120+
changed_files=${{ steps.changed_files.outputs.changed-files }}
121+
tests=$(curl -X POST 'https://tests-selection.azurewebsites.net/api' \
122+
-H 'Content-Type: application/json' \
123+
-d "$changed_files")
124+
echo "selected-tests::$tests"
125+
126+
run-playwright-tests:
127+
runs-on: ubuntu-latest
128+
strategy:
129+
fail-fast: false
130+
matrix:
131+
shardIndex: [1, 2, 3, 4]
132+
shardTotal: [4]
133+
timeout-minutes: 10
134+
needs: [call-test-selection-api, get-changed-files, playwright-setup, build-and-start-vue]
135+
env:
136+
CI: true
137+
steps:
138+
- uses: actions/checkout@v3
139+
- uses: actions/setup-node@v3
140+
with:
141+
node-version: 18
142+
143+
- name: Run Playwright Tests
144+
run: |
145+
tests=${{ steps.test_selection.outputs.selected-tests }}
146+
npx playwright test $tests --max-failures=2 --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [ main, master ]
5+
pull_request:
6+
branches: [ main, master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: lts/*
16+
- name: Install dependencies
17+
run: npm ci
18+
- name: Install Playwright Browsers
19+
run: npx playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: npx playwright test
22+
- uses: actions/upload-artifact@v4
23+
if: always()
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

curriculum-front/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ yarn-error.log*
1818
*.njsproj
1919
*.sln
2020
*.sw?
21+
/test-results/
22+
/playwright-report/
23+
/blob-report/
24+
/playwright/.cache/

curriculum-front/e2e/example.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
const { test, expect } = require('@playwright/test');
3+
4+
test('has title', async ({ page }) => {
5+
await page.goto('https://playwright.dev/');
6+
7+
// Expect a title "to contain" a substring.
8+
await expect(page).toHaveTitle(/Playwright/);
9+
});
10+
11+
test('get started link', async ({ page }) => {
12+
await page.goto('https://playwright.dev/');
13+
14+
// Click the get started link.
15+
await page.getByRole('link', { name: 'Get started' }).click();
16+
17+
// Expects page to have a heading with the name of Installation.
18+
await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
19+
});

0 commit comments

Comments
 (0)