| Server IP : 3.93.235.159 / Your IP : 216.73.216.140 Web Server : Apache System : Linux ip-172-26-0-169 5.10.0-46-cloud-amd64 #1 SMP Debian 5.10.262-1 (2026-07-31) x86_64 User : bitnami ( 1000) PHP Version : 8.2.9 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /bitnami/wordpress/wp-content/plugins/hello-plus/tests/playwright/ |
Upload File : |
import { APIRequest, APIRequestContext, Page, chromium, APIResponse } from '@playwright/test';
export async function login( apiRequest: APIRequest, user: string, password: string, baseUrl: string ) {
// Important: make sure we authenticate in a clean environment by unsetting storage state.
const context = await apiRequest.newContext( { storageState: undefined } );
await context.post( `${ baseUrl }/wp-login.php`, {
form: {
log: user,
pwd: password,
'wp-submit': 'Log In',
redirect_to: `${ baseUrl }/wp-admin/`,
testcookie: '1',
},
} );
return context;
}
export async function fetchNonce( context: APIRequestContext, baseUrl: string ) {
const response = await context.get( `${ baseUrl }/wp-admin/post-new.php` );
await validateResponse( response, 'Failed to fetch page' );
let pageText = await response.text();
if ( pageText.includes( 'WordPress has been updated!' ) ) {
pageText = await updateDatabase( context, baseUrl );
}
const nonceMatch = pageText.match( /var wpApiSettings = .*;/ );
if ( ! nonceMatch ) {
throw new Error( `Nonce not found on the page:\n"${ pageText }"` );
}
return nonceMatch[ 0 ].replace( /^.*"nonce":"([^"]*)".*$/, '$1' );
}
async function updateDatabase( context: APIRequestContext, baseUrl: string ): Promise<string> {
const browser = await chromium.launch();
const browserContext = await browser.newContext();
const page: Page = await browserContext.newPage();
await page.goto( `${ baseUrl }/wp-admin/post-new.php` );
await page.getByText( 'Update WordPress Database' ).click();
await page.getByText( 'Continue' ).click();
const retryResponse = await context.get( `${ baseUrl }/wp-admin/post-new.php` );
const pageText = await retryResponse.text();
await browser.close();
return pageText;
}
async function validateResponse( response: APIResponse, errorMessage: string ) {
if ( ! response.ok() ) {
throw new Error( `
${ errorMessage }: ${ response.status }.
${ await response.text() }
${ response.url() }
` );
}
}