|
2 | 2 | /** |
3 | 3 | * External dependencies |
4 | 4 | */ |
5 | | -const { chromium, expect } = require( '@playwright/test' ); |
| 5 | +const { chromium, expect } = require('@playwright/test'); |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Internal dependencies |
9 | 9 | */ |
10 | | -const fs = require( 'fs' ); |
11 | | -const { admin } = require( './config/users' ); |
12 | | - |
13 | | -module.exports = async ( config ) => { |
14 | | - const { stateDir, baseURL, userAgent } = config.projects[ 0 ].use; |
15 | | - |
16 | | - console.log( `State Dir: ${ stateDir }` ); |
17 | | - console.log( `Base URL: ${ baseURL }` ); |
18 | | - |
19 | | - // used throughout tests for authentication |
20 | | - process.env.ADMINSTATE = `${ stateDir }adminState.json`; |
21 | | - process.env.baseURL = baseURL; |
22 | | - |
23 | | - // Clear out the previous save states |
24 | | - try { |
25 | | - fs.unlinkSync( process.env.ADMINSTATE ); |
26 | | - console.log( 'Admin state file deleted successfully.' ); |
27 | | - } catch ( err ) { |
28 | | - if ( err.code === 'ENOENT' ) { |
29 | | - console.log( 'Admin state file does not exist.' ); |
30 | | - } else { |
31 | | - console.log( 'Admin state file could not be deleted: ' + err ); |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - // Pre-requisites |
36 | | - let adminLoggedIn = false; |
37 | | - |
38 | | - // Specify user agent when running against an external test site to avoid getting HTTP 406 NOT ACCEPTABLE errors. |
39 | | - const contextOptions = { baseURL, userAgent }; |
40 | | - |
41 | | - // Create browser, browserContext, and page for customer and admin users |
42 | | - const browser = await chromium.launch(); |
43 | | - const adminContext = await browser.newContext( contextOptions ); |
44 | | - const adminPage = await adminContext.newPage(); |
45 | | - |
46 | | - // Sign in as admin user and save state |
47 | | - const adminRetries = 5; |
48 | | - for ( let i = 0; i < adminRetries; i++ ) { |
49 | | - try { |
50 | | - console.log( 'Trying to log-in as admin...' ); |
51 | | - |
52 | | - // Login to admin. |
53 | | - const waitForNavigationPromise = adminPage.waitForURL( '**/wp-admin/' ); |
54 | | - await adminPage.goto( '/wp-login.php', { |
55 | | - waitUntil: 'networkidle', |
56 | | - } ); |
57 | | - const usernameLocator = await adminPage.locator( 'input[name="log"]' ); |
58 | | - await usernameLocator.fill( admin.username ); |
59 | | - const passwordLocator = await adminPage.locator( 'input[name="pwd"]' ); |
60 | | - await passwordLocator.fill( admin.password ); |
61 | | - const submitButtonLocator = await adminPage.getByRole( 'button', { name: 'Log In' } ); |
62 | | - await submitButtonLocator.click(); |
63 | | - await waitForNavigationPromise; |
64 | | - |
65 | | - // Check if logged in successfully. |
66 | | - const mainHeadingLocator = await adminPage.locator( '.wrap > h1' ); |
67 | | - await expect( await mainHeadingLocator.evaluate( ( el ) => el.textContent ) ).toBe( 'Dashboard' ); |
68 | | - |
69 | | - // Save state. |
70 | | - await adminPage.context().storageState( { path: process.env.ADMINSTATE } ); |
71 | | - |
72 | | - console.log( 'Logged-in as admin successfully.' ); |
73 | | - adminLoggedIn = true; |
74 | | - |
75 | | - break; |
76 | | - } catch ( e ) { |
77 | | - console.log( `Admin log-in failed, Retrying... ${ i }/${ adminRetries }` ); |
78 | | - console.log( e ); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - if ( ! adminLoggedIn ) { |
83 | | - console.error( |
84 | | - 'Cannot proceed e2e test, as admin login failed. Please check if the test site has been setup correctly.' |
85 | | - ); |
86 | | - process.exit( 1 ); |
87 | | - } |
88 | | - |
89 | | - await adminContext.close(); |
90 | | - await browser.close(); |
| 10 | +const fs = require('fs'); |
| 11 | +const { admin } = require('./config/users'); |
| 12 | + |
| 13 | +module.exports = async (config) => { |
| 14 | + const { stateDir, baseURL, userAgent } = config.projects[0].use; |
| 15 | + |
| 16 | + console.log(`State Dir: ${stateDir}`); |
| 17 | + console.log(`Base URL: ${baseURL}`); |
| 18 | + |
| 19 | + // used throughout tests for authentication |
| 20 | + process.env.ADMINSTATE = `${stateDir}adminState.json`; |
| 21 | + process.env.baseURL = baseURL; |
| 22 | + |
| 23 | + // Clear out the previous save states |
| 24 | + try { |
| 25 | + fs.unlinkSync(process.env.ADMINSTATE); |
| 26 | + console.log('Admin state file deleted successfully.'); |
| 27 | + } catch (err) { |
| 28 | + if (err.code === 'ENOENT') { |
| 29 | + console.log('Admin state file does not exist.'); |
| 30 | + } else { |
| 31 | + console.log('Admin state file could not be deleted: ' + err); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Pre-requisites |
| 36 | + let adminLoggedIn = false; |
| 37 | + |
| 38 | + // Specify user agent when running against an external test site to avoid getting HTTP 406 NOT ACCEPTABLE errors. |
| 39 | + const contextOptions = { baseURL, userAgent }; |
| 40 | + |
| 41 | + // Create browser, browserContext, and page for customer and admin users |
| 42 | + const browser = await chromium.launch(); |
| 43 | + const adminContext = await browser.newContext(contextOptions); |
| 44 | + const adminPage = await adminContext.newPage(); |
| 45 | + |
| 46 | + // Sign in as admin user and save state |
| 47 | + const adminRetries = 5; |
| 48 | + for (let i = 0; i < adminRetries; i++) { |
| 49 | + try { |
| 50 | + console.log('Trying to log-in as admin...'); |
| 51 | + |
| 52 | + // Login to admin. |
| 53 | + const waitForNavigationPromise = adminPage.waitForURL('**/wp-admin/'); |
| 54 | + await adminPage.goto('/wp-login.php', { |
| 55 | + waitUntil: 'networkidle', |
| 56 | + }); |
| 57 | + const usernameLocator = await adminPage.locator('input[name="log"]'); |
| 58 | + await usernameLocator.fill(admin.username); |
| 59 | + const passwordLocator = await adminPage.locator('input[name="pwd"]'); |
| 60 | + await passwordLocator.fill(admin.password); |
| 61 | + const submitButtonLocator = await adminPage.getByRole('button', { name: 'Log In' }); |
| 62 | + await submitButtonLocator.click(); |
| 63 | + await waitForNavigationPromise; |
| 64 | + |
| 65 | + // Check if logged in successfully. |
| 66 | + const mainHeadingLocator = await adminPage.locator('.wrap > h1'); |
| 67 | + await expect(await mainHeadingLocator.evaluate((el) => el.textContent)).toBe('Dashboard'); |
| 68 | + |
| 69 | + // Save state. |
| 70 | + await adminPage.context().storageState({ path: process.env.ADMINSTATE }); |
| 71 | + |
| 72 | + console.log('Logged-in as admin successfully.'); |
| 73 | + adminLoggedIn = true; |
| 74 | + |
| 75 | + break; |
| 76 | + } catch (e) { |
| 77 | + console.log(`Admin log-in failed, Retrying... ${i}/${adminRetries}`); |
| 78 | + console.log(e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!adminLoggedIn) { |
| 83 | + console.error( |
| 84 | + 'Cannot proceed e2e test, as admin login failed. Please check if the test site has been setup correctly.' |
| 85 | + ); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + await adminContext.close(); |
| 90 | + await browser.close(); |
91 | 91 | }; |
0 commit comments