Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Ensure we're logged out before trying to log in as a user
Browse files Browse the repository at this point in the history
  • Loading branch information
opr committed Aug 23, 2023
1 parent fc3bc95 commit eb315c3
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions tests/e2e/auth.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ import { customer, admin } from './test-data/data/data';

setup( 'authenticate as admin', async ( { page } ) => {
await page.goto( '/my-account' );
const isLoggedOut = await page
const isLoggedIn = await page
.getByLabel( 'Username or email address' )
.isVisible();
.isHidden();

if ( isLoggedOut ) {
if ( isLoggedIn ) {
await page
.getByLabel( 'Username or email address' )
.fill( admin.username );
await page.getByLabel( 'Password' ).fill( admin.password );
await page.getByRole( 'button', { name: 'Log in' } ).click();
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL( '/my-account/' );
.getByRole( 'paragraph' )
.filter( { hasText: /Hello .* \(not .*? Log out\)/ } )
.getByRole( 'link', { name: 'Log out' } )
.click();
}

await page.getByLabel( 'Username or email address' ).fill( admin.username );
await page.getByLabel( 'Password' ).fill( admin.password );
await page.getByRole( 'button', { name: 'Log in' } ).click();
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL( '/my-account/' );

await expect(
page
.getByRole( 'list' )
Expand All @@ -41,20 +45,25 @@ setup( 'authenticate as admin', async ( { page } ) => {

setup( 'authenticate as customer', async ( { page } ) => {
await page.goto( '/my-account' );
const isLoggedOut = await page
const isLoggedIn = await page
.getByLabel( 'Username or email address' )
.isVisible();
.isHidden();

if ( isLoggedOut ) {
if ( isLoggedIn ) {
await page
.getByLabel( 'Username or email address' )
.fill( customer.username );
await page.getByLabel( 'Password' ).fill( customer.password );
await page.getByRole( 'button', { name: 'Log in' } ).click();
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL( '/my-account/' );
.getByRole( 'paragraph' )
.filter( { hasText: /Hello .* \(not .*? Log out\)/ } )
.getByRole( 'link', { name: 'Log out' } )
.click();
}
await page
.getByLabel( 'Username or email address' )
.fill( customer.username );
await page.getByLabel( 'Password' ).fill( customer.password );
await page.getByRole( 'button', { name: 'Log in' } ).click();
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL( '/my-account/' );

await expect(
page
Expand All @@ -71,23 +80,16 @@ setup( 'authenticate as customer', async ( { page } ) => {

setup( 'authenticate as guest', async ( { page } ) => {
await page.goto( '/my-account' );
const isLoggedOut = await page
const isLoggedIn = await page
.getByLabel( 'Username or email address' )
.isVisible();
.isHidden();

if ( ! isLoggedOut ) {
if ( isLoggedIn ) {
await page
.getByRole( 'list' )
.filter( {
hasText:
'Dashboard Orders Downloads Addresses Account details Log out',
} )
.getByRole( 'paragraph' )
.filter( { hasText: /Hello .* \(not .*? Log out\)/ } )
.getByRole( 'link', { name: 'Log out' } )
.click();

// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL( '/my-account/' );
}

await expect(
Expand Down

0 comments on commit eb315c3

Please sign in to comment.