Skip to content

Commit

Permalink
fix: E2E auth state
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Jun 20, 2024
1 parent 5fabe98 commit 7a21f6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/blocks/test/e2e/blocks/block-conditions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import { test, expect } from '@wordpress/e2e-test-utils-playwright';
import { tryLoginIn } from '../utils';

test.describe( 'Block Conditions', () => {
test.beforeEach( async({ admin, requestUtils, page }) => {
await tryLoginIn( page, 'admin', 'password' );
test.beforeEach( async({ admin, page }) => {
await tryLoginIn( page );
await admin.createNewPost();
});

test.afterEach( async({ page }) => {

/**
* Because some conditions require an user to be logged in, we need to log in the user after each test so that we do not break the next test.
* Because some conditions require a user to be logged in,
* we need to log in the user after each test so that we do not break the next test.
*/
await tryLoginIn( page, 'admin', 'password' );
await tryLoginIn( page );
});

test( 'check logged out users', async({ editor, page, admin, requestUtils }) => {
test( 'check logged out users', async({ editor, page }) => {
await editor.insertBlock({
name: 'core/image',
attributes: {
Expand All @@ -35,14 +36,15 @@ test.describe( 'Block Conditions', () => {

const postId = await editor.publishPost();

// Check the block for logged in users.
// Check the block for logged-in users.
await page.goto( `/?p=${postId}` );
await expect( page.locator( '#wp--skip-link--target img' ) ).toBeVisible();

// Check the block for logged out users.
// // Check the block for logged out users.
await page.getByRole( 'menuitem', { name: 'Howdy, admin' }).hover();
await page.waitForTimeout( 200 );
await page.getByRole( 'menuitem', { name: 'Log Out' }).click();

await page.goto( `/?p=${postId}` );
await expect( page.locator( '#wp--skip-link--target img' ) ).toBeHidden();
});
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/test/e2e/blocks/button-group.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

test.describe( 'Button Group', () => {
test.beforeEach( async({ admin }) => {
test.beforeEach( async({ admin, page }) => {
await admin.createNewPost();
});

Expand Down
15 changes: 10 additions & 5 deletions src/blocks/test/e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ export function deleteFile( filePath ) {
}
}

export async function tryLoginIn( page, username, password ) {
await page.goto( '/wp-login.php' );
await page.fill( 'input[name="log"]', username );
await page.fill( 'input[name="pwd"]', password );
await page.click( 'input[name="wp-submit"]' );
export async function tryLoginIn( page, username = 'admin', password = 'password' ) {
await page.goto('/wp-login.php');

Check failure on line 73 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space after this paren

Check failure on line 73 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space before this paren
await page.fill('input[name="log"]', username);

Check failure on line 74 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space after this paren

Check failure on line 74 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space before this paren
await page.fill('input[name="pwd"]', password);

Check failure on line 75 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space after this paren

Check failure on line 75 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space before this paren
await page.check('input[name="rememberme"]');

Check failure on line 76 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space after this paren

Check failure on line 76 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space before this paren
await page.click('input[name="wp-submit"]');

Check failure on line 77 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space after this paren

Check failure on line 77 in src/blocks/test/e2e/utils.js

View workflow job for this annotation

GitHub Actions / JS Test (18.x)

There must be a space before this paren

// Save the context since tests are isolated.
// So we need to save the auth state to keep the user logged in so that they can be used in the next test.
await page.context().storageState({ path: 'artifacts/storage-states/admin.json' });
}

0 comments on commit 7a21f6c

Please sign in to comment.