Skip to content

Commit

Permalink
Tests - Wait for the main web page to be HTTP 200
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Feb 11, 2025
1 parent dcc6734 commit 0a01b0c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
43 changes: 24 additions & 19 deletions tests/end2end/playwright/auth.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,37 @@ import { Page } from '@playwright/test';
* @param {string} user_file The path to the file where the cookies will be stored
*/
export async function auth_using_login(page: Page, login: string, password: string, user_file: string) {
// Perform authentication steps. Replace these actions with your own.
await page.goto('admin.php/auth/login?auth_url_return=%2Findex.php');
await expect(async () => {
const response = await page.goto('admin.php/auth/login?auth_url_return=%2Findex.php');
expect(response.status()).toBe(200);
}).toPass({
intervals: [1_000, 2_000, 10_000],
timeout: 60_000
});

let loginField = page.locator('#jforms_jcommunity_login_auth_login');
await expect(loginField, `The login field was not found in ${page.content()}`).toBeVisible();
await loginField.fill(login);
let loginField = page.locator('#jforms_jcommunity_login_auth_login');
await expect(loginField, `The login field was not found in the page`).toBeVisible();
await loginField.fill(login);

await page.locator('#jforms_jcommunity_login_auth_password').fill(password);
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait until the page receives the cookies.
// 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('index.php');
await page.locator('#jforms_jcommunity_login_auth_password').fill(password);
await page.getByRole('button', {name: 'Sign in'}).click();
// Wait until the page receives the cookies.
// 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('index.php');

// End of authentication steps.
await page.context().storageState({ path: user_file });
// End of authentication steps.
await page.context().storageState({path: user_file});
}

setup('authenticate as user_in_group_a', async ({ page }) => {
await auth_using_login(page, 'user_in_group_a', 'admin', getAuthStorageStatePath('user_in_group_a'));
setup('authenticate as user_in_group_a', async ({page}) => {
await auth_using_login(page, 'user_in_group_a', 'admin', getAuthStorageStatePath('user_in_group_a'));
});

setup('authenticate as admin', async ({ page }) => {
await auth_using_login(page, 'admin', 'admin', getAuthStorageStatePath('admin'));
setup('authenticate as admin', async ({page}) => {
await auth_using_login(page, 'admin', 'admin', getAuthStorageStatePath('admin'));
});

setup('authenticate as publisher', async ({ page }) => {
await auth_using_login(page, 'publisher', 'admin', getAuthStorageStatePath('publisher'));
setup('authenticate as publisher', async ({page}) => {
await auth_using_login(page, 'publisher', 'admin', getAuthStorageStatePath('publisher'));
});
37 changes: 30 additions & 7 deletions tests/end2end/playwright/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import * as path from 'path';

/**
* The file path
* @var string
* @member string
*/
const __filename = fileURLToPath(import.meta.url);

/**
* The file directory path
* @var string
* @member string
* @see https://nodejs.org/docs/latest-v15.x/api/esm.html#esm_no_filename_or_dirname
* @see https://stackoverflow.com/questions/8817423/why-is-dirname-not-defined-in-node-repl
* @example
Expand Down Expand Up @@ -98,14 +98,25 @@ export async function gotoMap(url, page, mapMustLoad = true, layersInTreeView =

// Wait for WMS GetCapabilities promise
let getCapabilitiesWMSPromise = page.waitForRequest(/SERVICE=WMS&REQUEST=GetCapabilities/);
await page.goto(url);

await expect(async () => {
const response = await page.goto(url);
expect(response.status()).toBe(200);
}).toPass({
intervals: [1_000, 2_000, 10_000],
timeout: 60_000
});

// Wait for WMS GetCapabilities
await getCapabilitiesWMSPromise;
if (mapMustLoad) {
if (waitForGetLegendGraphic) {
// Wait for WMS GetLegendGraphic promise
const getLegendGraphicPromise = page.waitForRequest(request => request.method() === 'POST' && request.postData() != null && request.postData()?.includes('GetLegendGraphic') === true);
const getLegendGraphicPromise = page.waitForRequest(
request => request.method() === 'POST' &&
request.postData() != null &&
request.postData()?.includes('GetLegendGraphic') === true
);
// Normal check about the map
// Wait for WMS GetLegendGraphic
await getLegendGraphicPromise;
Expand All @@ -132,13 +143,25 @@ export async function reloadMap(page, check = true) {

// Wait for WMS GetCapabilities promise
let getCapabilitiesWMSPromise = page.waitForRequest(/SERVICE=WMS&REQUEST=GetCapabilities/);
await page.reload();

await expect(async () => {
const response = await page.reload();
expect(response.status()).toBe(200);
}).toPass({
intervals: [1_000, 2_000, 10_000],
timeout: 60_000
});

// Wait for WMS GetCapabilities
await getCapabilitiesWMSPromise;
if (check) {
// Wait for WMS GetLegendGraphic promise
const getLegendGraphicPromise = page.waitForRequest(request => request.method() === 'POST' && request.postData() != null && request.postData()?.includes('GetLegendGraphic') === true);
const getLegendGraphicPromise = page.waitForRequest(
request =>
request.method() === 'POST' &&
request.postData() != null &&
request.postData()?.includes('GetLegendGraphic') === true
);
// Normal check about the map
// Wait for WMS GetLegendGraphic
await getLegendGraphicPromise;
Expand Down Expand Up @@ -170,7 +193,7 @@ export async function editedFeatureIds(page) {
* @returns {Promise<URLSearchParams>} List of parameters in the request
*/
export async function getEchoRequestParams(page, url) {
// Re-send the request with additionnal echo param to retrieve the OGC Request
// Re-send the request with additional echo param to retrieve the OGC Request
let echoResponse = await page.request.get(url + '&__echo__');
const originalUrl = decodeURIComponent(await echoResponse.text());
// When the request has not been logged by echo proxy
Expand Down

0 comments on commit 0a01b0c

Please sign in to comment.