/** * Rehearsals converted to Playwright: 1 rehearsal. * * Needs a logged-in session: set STORAGE_STATE to a Playwright storage state file (rehearsals with * a role read STORAGE_STATE_ first), and BASE_URL to the site to test. * * Each result is saved on the site as a run, like a run from Tools → Rehearsal, using the * STORAGE_STATE session (an administrator's). REHEARSAL_REF labels the runs; REHEARSAL_REPORT=0 turns it off. */ import { test, expect as baseExpect, request, type Locator, type TestInfo } from '@playwright/test'; const BASE_URL = process.env.BASE_URL || "https://freely-silky-stork.jurassic.ninja/"; test.use( { baseURL: BASE_URL, storageState: process.env.STORAGE_STATE, } ); // Links that open a new tab load in the same tab instead, as in the Rehearsal runner. async function click( locator: Locator, timeout?: number ) { const href = await locator.evaluate( ( el ) => ( el.closest( 'a[target="_blank"][href]' ) as HTMLAnchorElement | null )?.href ?? null, undefined, { timeout } ); await ( href ? locator.page().goto( href ) : locator.click( { timeout } ) ); } // Each step is a step in the Playwright report and a line in the run saved on the site. function rehearsal( id: number, hash: string, viewport: string ) { // The error without colours or its call log, on one line: what failed, expected and received. const brief = ( e: unknown ) => String( ( e as Error )?.message ?? e ).replace( /\x1b\[[0-9;]*m/g, '' ).split( /\nCall log:/ )[ 0 ].split( '\n' ).map( ( l ) => l.trim() ).filter( Boolean ).join( ' · ' ).slice( 0, 600 ); let log: { ok: boolean; msg: string; ms: number }[] = []; const errors: string[] = []; return { errors, start() { log = []; errors.length = 0; }, async step( n: number, label: string, body: () => Promise< unknown >, optional = false ) { const t0 = Date.now(); try { await test.step( label, body ); log.push( { ok: true, msg: label, ms: Date.now() - t0 } ); } catch ( e ) { // An optional step is skipped when it doesn't apply. log.push( { ok: optional, msg: `${ optional ? 'Skipped optional step' : 'Step' } ${ n }: ${ label }: ${ brief( e ) }`, ms: Date.now() - t0 } ); if ( ! optional ) throw e; } }, async report( info: TestInfo ) { if ( ! id || process.env.REHEARSAL_REPORT === '0' || info.status === 'skipped' ) return; const status = info.status === 'passed' ? 'pass' : 'fail'; // A timeout stops the test between log lines, so say why it ended. if ( status === 'fail' && ! log.some( ( l ) => ! l.ok ) ) log.push( { ok: false, msg: brief( info.error?.message ?? `Test ${ info.status }` ), ms: 0 } ); const note = ( type: string, description: string ) => info.annotations.push( { type, description } ); if ( ! process.env.STORAGE_STATE ) return note( 'rehearsal run not saved', 'Set STORAGE_STATE to an administrator\'s session to save runs on the site.' ); const site = await request.newContext( { baseURL: BASE_URL, storageState: process.env.STORAGE_STATE } ); try { const nonce = await ( await site.get( 'wp-admin/admin-ajax.php?action=rest-nonce' ) ).text(); const res = await site.post( '?rest_route=/rehearsal/v1/runs', { headers: { 'X-WP-Nonce': nonce }, data: { recipe_id: id, recipe_hash: hash, status, log, runner: 'playwright', viewport, ref: process.env.REHEARSAL_REF || '', errors: status === 'fail' ? errors.map( ( msg ) => ( { msg, page: '', source: '', count: 1 } ) ) : [] }, } ); const body = await res.json().catch( () => ( {} ) ); if ( res.ok() ) note( 'rehearsal run', `Saved as run #${ body.id } on ${ BASE_URL }` ); else note( 'rehearsal run not saved', body.message || `HTTP ${ res.status() }` ); } catch ( e ) { note( 'rehearsal run not saved', brief( e ) ); } finally { await site.dispose(); } }, }; } // Rehearsal: Forms: Authors cannot list notes in wp-admin Comments // Rehearsal 459bbaf from https://freely-silky-stork.jurassic.ninja/, format 1. // Requires plugins: jetpack. Requires theme: any. Runs as: author (STORAGE_STATE_AUTHOR). test.describe( "Forms: Authors cannot list notes in wp-admin Comments", () => { const expect = baseExpect.configure( { timeout: 10000 } ); test.use( { actionTimeout: 10000, storageState: process.env.STORAGE_STATE_AUTHOR || process.env.STORAGE_STATE } ); const rh = rehearsal( 27, "459bbaf99febf78c48bee76adfb97ff14ec36c1d1de1eea61bce368756ebc8d5", "desktop" ); test.afterEach( ( {}, info ) => rh.report( info ) ); test( "rehearsal 459bbaf", async ( { page } ) => { test.setTimeout( 60000 ); rh.start(); await rh.step( 1, "goto /wp-admin/edit-comments.php?comment_status=all&comment_type=jetpack_form_note", async () => { await page.goto( "/wp-admin/edit-comments.php?comment_status=all&comment_type=jetpack_form_note" ); } ); await rh.step( 2, "expect h1 toContainText \"Comments\"", async () => { await expect( page.locator( "h1" ).filter( { visible: true } ).first() ).toContainText( "Comments" ); } ); await rh.step( 3, "expect #the-comment-list not toContainText \"Rehearsal note\"", async () => { await expect( page.locator( "#the-comment-list" ).filter( { visible: true } ).first() ).not.toContainText( "Rehearsal note" ); } ); } ); } );