/** * 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: Deleting a note from a response removes it for good (changes the site) // Rehearsal 4f7da57 from https://freely-silky-stork.jurassic.ninja/, format 2. // Requires plugins: jetpack. Requires theme: any. Runs as: the logged-in user. test.describe( "Forms: Deleting a note from a response removes it for good (changes the site)", () => { const expect = baseExpect.configure( { timeout: 30000 } ); test.use( { actionTimeout: 30000, viewport: { width: 1280, height: 800 } } ); const rh = rehearsal( 22, "4f7da572f8d38db6a3a63c367e6cdc8f2f3781a46fef142b0e6c15a1aeb4bb74", "desktop" ); test.afterEach( ( {}, info ) => rh.report( info ) ); test( "rehearsal 4f7da57", async ( { page } ) => { test.setTimeout( 570000 ); rh.start(); const run = Date.now().toString( 36 ); const consoleErrors = rh.errors; // Script errors only, like the Rehearsal runner. Failed requests are the network's, so they go in the report. page.on( 'console', ( m ) => m.type() === 'error' && ! m.text().startsWith( 'Failed to load resource' ) && consoleErrors.push( m.text() ) ); page.on( 'pageerror', ( e ) => consoleErrors.push( e.message ) ); page.on( 'requestfailed', ( r ) => test.info().annotations.push( { type: 'request failed', description: `${ r.failure()?.errorText } ${ r.url() }` } ) ); await rh.step( 1, "goto /rehearsal-notes-form/", async () => { await page.goto( "/rehearsal-notes-form/" ); } ); await rh.step( 2, "expect form.contact-form button[type=submit] toBeVisible", async () => { await expect( page.locator( "form.contact-form button[type=submit]" ).filter( { visible: true } ).first() ).toBeVisible(); } ); await rh.step( 3, "fill form.contact-form input[type=text] = “Rehearsal " + run + "”", async () => { await page.locator( "form.contact-form input[type=text]" ).filter( { visible: true } ).first().fill( "Rehearsal " + run ); } ); await rh.step( 4, "click form.contact-form button[type=submit]", async () => { await click( page.locator( "form.contact-form button[type=submit]" ).filter( { visible: true } ).first() ); } ); await rh.step( 5, "expectText Thank you for your response.", async () => { await expect( page.locator( 'body' ) ).toContainText( "Thank you for your response." ); } ); await rh.step( 6, "goto /wp-admin/admin.php?page=jetpack-forms-responses-wp-admin&p=%2Fresponses%2Finbox", async () => { await page.goto( "/wp-admin/admin.php?page=jetpack-forms-responses-wp-admin&p=%2Fresponses%2Finbox" ); } ); await rh.step( 7, "click [role=checkbox][aria-label=\"Rehearsal " + run + "\"]", async () => { await click( page.locator( "[role=checkbox][aria-label=\"Rehearsal " + run + "\"]" ).filter( { visible: true } ).first() ); } ); await rh.step( 8, "expect .jp-forms__feedback-comments-heading toContainText \"Notes\"", async () => { await expect( page.locator( ".jp-forms__feedback-comments-heading" ).filter( { visible: true } ).first() ).toContainText( "Notes" ); } ); await rh.step( 9, "fill .jp-forms__feedback-comments-form textarea = “Rehearsal deleted note " + run + "”", async () => { await page.locator( ".jp-forms__feedback-comments-form textarea" ).filter( { visible: true } ).first().fill( "Rehearsal deleted note " + run ); } ); await rh.step( 10, "click .jp-forms__feedback-comments-form button:has-text(\"Add note\")", async () => { await click( page.locator( ".jp-forms__feedback-comments-form button:has-text(\"Add note\")" ).filter( { visible: true } ).first() ); } ); await rh.step( 11, "expect .jp-forms__feedback-comments-list toContainText \"Rehearsal deleted note " + run + "\"", async () => { await expect( page.locator( ".jp-forms__feedback-comments-list" ).filter( { visible: true } ).first() ).toContainText( "Rehearsal deleted note " + run ); } ); await rh.step( 12, "click .jp-forms__feedback-comments-comment:has-text(\"Rehearsal deleted note " + run + "\") >> button[aria-label=\"Note options\"]", async () => { await click( page.locator( ".jp-forms__feedback-comments-comment:has-text(\"Rehearsal deleted note " + run + "\")" ).locator( "button[aria-label=\"Note options\"]" ).filter( { visible: true } ).first() ); } ); await rh.step( 13, "click [role=menuitem]:has-text(\"Delete\")", async () => { await click( page.locator( "[role=menuitem]:has-text(\"Delete\")" ).filter( { visible: true } ).first() ); } ); await rh.step( 14, "expect .jp-forms__feedback-comments not toContainText \"Rehearsal deleted note " + run + "\"", async () => { await expect( page.locator( ".jp-forms__feedback-comments" ).filter( { visible: true } ).first() ).not.toContainText( "Rehearsal deleted note " + run ); } ); await rh.step( 15, "goto /wp-admin/edit-comments.php?comment_status=trash&comment_type=jetpack_form_note", async () => { await page.goto( "/wp-admin/edit-comments.php?comment_status=trash&comment_type=jetpack_form_note" ); } ); await rh.step( 16, "expect h1 toContainText \"Comments\"", async () => { await expect( page.locator( "h1" ).filter( { visible: true } ).first() ).toContainText( "Comments" ); } ); await rh.step( 17, "expect #the-comment-list not toContainText \"Rehearsal deleted note " + run + "\"", async () => { await expect( page.locator( "#the-comment-list" ).filter( { visible: true } ).first() ).not.toContainText( "Rehearsal deleted note " + run ); } ); await rh.step( 18, "expectNoConsoleErrors", async () => { expect( consoleErrors ).toEqual( [] ); } ); } ); } );