Skip to content

Commit a519bde

Browse files
committed
upload traces
1 parent 57c3608 commit a519bde

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

.github/scripts/unzip_traces.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Path to the JSON file
4+
json_file="./playwright-traces/test-traces.json"
5+
6+
# Function to check jq version
7+
check_jq_version() {
8+
jq_version=$(jq --version | cut -d- -f2)
9+
major_version=$(echo "$jq_version" | cut -d. -f1)
10+
minor_version=$(echo "$jq_version" | cut -d. -f2)
11+
12+
if [ "$major_version" -eq 1 ] && [ "$minor_version" -ge 6 ]; then
13+
echo "jq version: $jq_version"
14+
return 0 # jq 1.6 or higher
15+
else
16+
return 1 # Unsupported jq version
17+
fi
18+
}
19+
20+
# Check if jq is installed
21+
if ! command -v jq &> /dev/null; then
22+
echo "jq is not installed. Please install jq first."
23+
exit 1
24+
fi
25+
26+
# Check jq version
27+
if ! check_jq_version; then
28+
echo "Unsupported jq version. Please use jq 1.6 or higher."
29+
exit 1
30+
fi
31+
32+
# Read the JSON file
33+
cat "$json_file" | jq .
34+
35+
jq -r '.[] | "\(.id) \(.trace)"' "$json_file" | while read id trace_file; do
36+
if [ -f "$trace_file" ]; then
37+
# Create the directory for the trace file
38+
output_dir="./traces/$id"
39+
mkdir -p "$output_dir"
40+
41+
# Unzip the trace file into the directory
42+
unzip -o "$trace_file" -d "$output_dir" > /dev/null 2>&1 && echo "Unzipped $trace_file to $output_dir/"
43+
else
44+
echo "Trace file not found: $trace_file"
45+
fi
46+
done
47+
48+
for dir in ./traces/*; do
49+
if [ -d "$dir" ]; then
50+
artifact_name=$(basename "$dir")
51+
echo "Uploading artifact: $artifact_name"
52+
# Upload each trace/{id} as an artifact using actions/upload-artifact
53+
- name: Upload $artifact_name
54+
uses: actions/upload-artifact@v3
55+
with:
56+
name: "$artifact_name"
57+
path: "$dir"
58+
fi
59+
done

.github/workflows/playwright.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ jobs:
3434
WP_PASSWORD: password
3535
STACKABLE_SLUG: Stackable/plugin
3636
run: npm run test
37+
- name: Run script to unzip and upload traces
38+
run: |
39+
chmod +x ./.github/scripts/unzip_traces.sh
40+
./.github/scripts/unzip_traces.sh
3741
- uses: actions/upload-artifact@v4
3842
if: ${{ !cancelled() }}
3943
id: artifact-upload-step

e2e/config/reporter.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable no-console */
2+
import type {
3+
Reporter, TestCase, TestResult,
4+
} from '@playwright/test/reporter'
5+
6+
import fs from 'fs'
7+
import path from 'path'
8+
9+
class MyReporter implements Reporter {
10+
outputFolder: string;
11+
testResults: Array<{title: string, id: string, trace: any}>;
12+
13+
constructor() {
14+
this.outputFolder = 'playwright-traces'
15+
this.cleanupFolder()
16+
this.testResults = []
17+
}
18+
cleanupFolder() {
19+
const folderPath = path.resolve( this.outputFolder )
20+
21+
if ( fs.existsSync( folderPath ) ) {
22+
// Read the directory and delete files
23+
const files = fs.readdirSync( folderPath )
24+
for ( const file of files ) {
25+
const filePath = path.join( folderPath, file )
26+
if ( fs.lstatSync( filePath ).isFile() ) {
27+
fs.unlinkSync( filePath ) // Remove the file
28+
}
29+
}
30+
console.log( `All files removed from: ${ folderPath }` )
31+
} else {
32+
// If folder doesn't exist, create it
33+
fs.mkdirSync( folderPath, { recursive: true } )
34+
}
35+
}
36+
37+
onTestEnd( test: TestCase, result: TestResult ) {
38+
if ( result.attachments.length !== 0 ) {
39+
console.log( 'title:', test.title )
40+
console.log( 'attachments', result.attachments )
41+
this.testResults.push( {
42+
id: test.id,
43+
title: test.title,
44+
trace: result.attachments.find( attachment => attachment.name === 'trace' ).path,
45+
} )
46+
}
47+
}
48+
49+
async onEnd() {
50+
// Ensure the output folder exists
51+
const folderPath = path.resolve( this.outputFolder )
52+
if ( ! fs.existsSync( folderPath ) ) {
53+
fs.mkdirSync( folderPath, { recursive: true } )
54+
}
55+
56+
// Write the collected results to a JSON file
57+
const reportPath = path.join( folderPath, 'test-traces.json' )
58+
fs.writeFileSync( reportPath, JSON.stringify( this.testResults, null, 2 ) )
59+
}
60+
}
61+
export default MyReporter

e2e/playwright.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dotenv.config( { path: path.resolve( __dirname, '../.env' ) } )
1313
* See https://playwright.dev/docs/test-configuration.
1414
*/
1515
export default defineConfig( {
16-
testDir: '.',
1716
// This is run before any tests. Check the file for more information.
1817
// globalSetup: 'e2e-global-setup.ts',
1918
globalSetup: fileURLToPath(
@@ -33,7 +32,7 @@ export default defineConfig( {
3332
workers: 1,
3433
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
3534
reporter: process.env.CI
36-
? [ [ 'list' ], [ 'github', { useDetails: true, showError: true } ], [ 'html', { outputFolder: '../playwright-report' } ] ]
35+
? [ [ 'list' ], [ 'github', { useDetails: true, showError: true } ], [ 'html', { outputFolder: '../playwright-report' } ], [ './config/reporter.ts' ] ]
3736
: [ [ 'list' ], [ 'html', { outputFolder: '../playwright-report' } ] ],
3837
reportSlowTests: null,
3938
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
@@ -70,7 +69,7 @@ export default defineConfig( {
7069
storageState: process.env.WP_AUTH_STORAGE,
7170
...devices[ 'Desktop Chrome' ],
7271
},
73-
testMatch: 'tests/*.ts',
72+
testDir: './tests',
7473
},
7574
// {
7675
// name: 'chromium',

0 commit comments

Comments
 (0)