Skip to content

Commit

Permalink
fix: wait for click with networkidle
Browse files Browse the repository at this point in the history
  • Loading branch information
innerdvations committed Apr 28, 2024
1 parent 9274ffd commit 777252f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
3 changes: 3 additions & 0 deletions tests/e2e/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ const ADMIN_PASSWORD = 'Testing123!';
const TITLE_LOGIN = 'Strapi Admin';
const TITLE_HOME = 'Homepage | Strapi';

const URL_ROOT = '/';

module.exports = {
ADMIN_EMAIL_ADDRESS,
ADMIN_PASSWORD,
ALLOWED_CONTENT_TYPES,
CUSTOM_TRANSFER_TOKEN_ACCESS_KEY,
TITLE_LOGIN,
TITLE_HOME,
URL_ROOT,
};
51 changes: 24 additions & 27 deletions tests/e2e/utils/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Page } from '@playwright/test';
import { ADMIN_EMAIL_ADDRESS, ADMIN_PASSWORD, TITLE_HOME } from '../constants';
import { ADMIN_EMAIL_ADDRESS, ADMIN_PASSWORD, TITLE_HOME, URL_ROOT } from '../constants';

/**
* Log in to an e2e test app
Expand All @@ -9,36 +9,33 @@ import { ADMIN_EMAIL_ADDRESS, ADMIN_PASSWORD, TITLE_HOME } from '../constants';
export const login = async ({ page, rememberMe = false }: { page: Page; rememberMe?: boolean }) => {
const maxRetries = 5;
for (let i = 0; i < maxRetries; i++) {
try {
// TODO: nav to login page and check title
await page.getByLabel('Email').fill(ADMIN_EMAIL_ADDRESS);
await page
.getByLabel('Password*', {
exact: true,
})
.fill(ADMIN_PASSWORD);
// go to the root page which should be the logged in
await page.goto(URL_ROOT);

if (rememberMe) {
await page.getByLabel('Remember me').click();
}
await page.getByRole('button', { name: 'Login' }).click();
await page.waitForTimeout(2000);
if ((await page.title()) !== TITLE_HOME) {
throw new Error('Login failed to load homepage');
}
// ...unless we already logged in, and are redirected to the home page now
if (i > 0 && (await page.title()) === TITLE_HOME) {
break; // If the page loads successfully, break the loop
} catch (error) {
// If this was the last retry, give up
if (i === maxRetries - 1) {
throw error;
}
}

await page.getByLabel('Email').fill(ADMIN_EMAIL_ADDRESS);
await page
.getByLabel('Password*', {
exact: true,
})
.fill(ADMIN_PASSWORD);

if (rememberMe) {
await page.getByLabel('Remember me').click();
}

await page.reload();
await Promise.all([
page.waitForLoadState('networkidle'),
page.getByRole('button', { name: 'Login' }).click(),
]);

// if it actually did log in, proceed instead of trying again
if ((await page.title()) === TITLE_HOME) {
break;
}
if ((await page.title()) === TITLE_HOME) {
break;
}
// if we made it here, we failed to log in, so on to the next try
}
};

0 comments on commit 777252f

Please sign in to comment.