From 1ad00a9fcd6a63c7bfcfc1b302231c4beb9f8791 Mon Sep 17 00:00:00 2001 From: juhi123 Date: Sun, 29 Dec 2024 19:39:12 +0530 Subject: [PATCH] Add Playwright E2E test to create new user --- tests/e2e/specs/user/create-new-user.test.js | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/e2e/specs/user/create-new-user.test.js diff --git a/tests/e2e/specs/user/create-new-user.test.js b/tests/e2e/specs/user/create-new-user.test.js new file mode 100644 index 0000000000000..836352a53cd04 --- /dev/null +++ b/tests/e2e/specs/user/create-new-user.test.js @@ -0,0 +1,52 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.describe( 'Create User', () => { + let username, email; + + test.beforeEach( async ( { admin } ) => { + // Navigate to the Add New User page + await admin.visitAdminPage( 'user-new.php' ); + } ); + + test.afterEach( async ( { requestUtils } ) => { + await requestUtils.deleteAllUsers(); + } ); + + test( 'creates new user successfully', async ( { page, requestUtils } ) => { + // Generate random username and email + username = 'TestUser' + Date.now(); + email = `user${ Date.now() }@domain.tld`; + + // Wait until network is idle + await page.waitForLoadState( 'networkidle' ); + + // Fill in the username and email + await page.locator( '#user_login' ).fill( username ); + await page.locator( '#email' ).fill( email ); + + const passwordText = await page.locator( '#pass1' ).textContent(); + + // Click the "Add New User" button + await page.click( 'role=button[name="Add New User"i]' ); + + // Verify success message + await expect( page.locator( '#message p' ) ).toHaveText( + 'New user created. Edit user' + ); + + await requestUtils.login( username, passwordText ); + + // Verify if the expected element is visible + const isLoggedIn = await page + .locator( '.wp-heading-inline' ) + .isVisible(); + if ( isLoggedIn ) { + console.log( 'Login successful with newly created user' ); + } else { + console.error( 'Login failed' ); + } + } ); +} );