Skip to content

Commit

Permalink
Add Playwright E2E test for add new user, delete user, update role
Browse files Browse the repository at this point in the history
  • Loading branch information
juhi123 committed Dec 31, 2024
1 parent 1ad00a9 commit fa083d3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 52 deletions.
52 changes: 0 additions & 52 deletions tests/e2e/specs/user/create-new-user.test.js

This file was deleted.

117 changes: 117 additions & 0 deletions tests/e2e/specs/user/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Create User', () => {
let username, email;

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deleteAllUsers();
} );

test( 'creates new user successfully', async ( { admin, page, requestUtils } ) => {

// Navigate to the Add New User page
await admin.visitAdminPage( 'user-new.php' );

// 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( 'Logged in successfuly with newly created user' );
} else {
console.error( 'Login failed' );
}
} );

test( 'deletes new user successfully', async ( { admin, page } ) => {
// Navigate to the users page
await admin.visitAdminPage( 'users.php' );

// Filter by "Subscriber" role if required
await page.locator( 'role=link[name= /Subscriber/i]' ).click();
await page.waitForLoadState( 'networkidle' ); // Ensure all elements are loaded

// Hover over the user row and click the delete link
const userRowSelector = '.has-row-actions.column-primary';
await page.locator( userRowSelector ).first().hover();
await page.locator( 'role=link[name="Delete"i]' ).first().click();

// Confirm deletion
const confirmButtonSelector = 'role=button[name="Confirm Deletion"i]';
await page.locator( confirmButtonSelector ).click();

// Verify success message
const successMessageSelector = '#message > p';
await expect( page.locator( successMessageSelector ) ).toHaveText(
/User deleted\./i);

} );

test( 'edits user role successfully', async ( { admin, page, requestUtils } ) => {

await requestUtils.createUser({
username: 'testuser',
email: '[email protected]',
roles: 'subscriber',
password: '34hhasjdhkahsdh'
});
// Navigate to Users page
await admin.visitAdminPage( 'users.php' );

// Wait for the users table to load
await page.waitForSelector( '#the-list', { visible: true } );

// Click on the test user's link
const userLinkSelector = 'role=link[name="testuser"i]';
await page.locator( userLinkSelector ).click();

// Wait for the user profile page to load
await page.waitForSelector( '#first_name', { visible: true } );

// Edit the user's first name
await page.fill( '#first_name', 'Test' );

// Change the user's role
const roleSelector = '#role';
await page.selectOption( roleSelector, 'editor' );

// Click the "Update user" button
const updateButtonSelector = 'role=button[name="Update user"i]';
await page.locator( updateButtonSelector ).click();

// Verify the success message
const successMessageSelector = "div[id='message'] p strong";
await expect( page.locator( successMessageSelector ) ).toHaveText(
'User updated.'
);

} );


} );

0 comments on commit fa083d3

Please sign in to comment.