Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E2E] Get tests to consistently pass in headless mode #1036

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions e2e/pages/shared-lending-platform/InstitutionProfile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test';
import { test } from '../../fixtures/testFixture';
import { clickLinkWithRetry } from '../../utils/clickExternalLinkWithRetry';

test('Institution Profile Page', async ({
page,
Expand Down Expand Up @@ -267,22 +268,20 @@ test('Institution Profile Page', async ({

// GLEIF link
await test.step('GLEIF links', async () => {
const [externalLink] = await Promise.all([
context.waitForEvent('page'),
page
.getByRole('link', {
name: 'GLEIF',
exact: true,
})
.click(),
]);
await expect(externalLink, 'Resolves correctly').toHaveURL(
await clickLinkWithRetry({
page,
target: page.getByRole('link', {
name: 'GLEIF',
exact: true,
}),
});
await expect(page).toHaveURL(
'https://www.gleif.org/en/about-lei/get-an-lei-find-lei-issuing-organizations',
);
await expect(externalLink, 'Resolves correctly').toHaveTitle(
await expect(page).toHaveTitle(
'Get an LEI: Find LEI Issuing Organizations - LEI – GLEIF',
);
await externalLink.close();
await page.goBack();
});

// Update Financial Institution links
Expand Down
33 changes: 19 additions & 14 deletions e2e/pages/shared-lending-platform/Navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test';
import { test } from '../../fixtures/testFixture';
import { clickLinkWithRetry } from '../../utils/clickExternalLinkWithRetry';

test('Navigation', async ({ page, navigateToFilingHome }) => {
navigateToFilingHome;
Expand Down Expand Up @@ -57,10 +58,12 @@ test('Navigation', async ({ page, navigateToFilingHome }) => {
});

await test.step('Footer Navigation', async () => {
await page
.locator('.o-footer')
.getByRole('link', { name: 'About Us', exact: true })
.click();
await clickLinkWithRetry({
page,
target: page
.locator('.o-footer')
.getByRole('link', { name: 'About Us', exact: true }),
});
await expect(page.locator('h1')).toContainText('About us');
await page.goBack();

Expand Down Expand Up @@ -213,17 +216,19 @@ test('Navigation', async ({ page, navigateToFilingHome }) => {
);
await page.goBack();

await page
.locator('.o-footer')
.getByRole('link', { name: 'USA.gov' })
.click();
await clickLinkWithRetry({
page,
target: page.locator('.o-footer').getByRole('link', { name: 'USA.gov' }),
});
await expect(page).toHaveURL(/.*usa.gov/);
await page.goBack();

await page
.locator('.o-footer')
.getByRole('link', { name: 'Office of Inspector General' })
.click();
await clickLinkWithRetry({
page,
target: page
.locator('.o-footer')
.getByRole('link', { name: 'Office of Inspector General' }),
});
await expect(page).toHaveURL(/.*oig/);
await page.goBack();
});
Expand All @@ -238,10 +243,10 @@ test('Navigation', async ({ page, navigateToFilingHome }) => {
await expect(page.locator('h1')).toContainText(
'Get started filing your lending data',
);
await expect(page.locator('.navbar .nav-items')).toBeEmpty();
await expect(page.locator('.navbar .nav-items')).toHaveCount(0);

// Test CFPB Logo Link
await page.getByLabel('Home').click();
await clickLinkWithRetry({ page, target: page.getByLabel('Home') });
await expect(page).toHaveURL('https://www.consumerfinance.gov/');
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from '@playwright/test';
import { test } from '../../../fixtures/testFixture';
import { clickLinkWithRetry } from '../../../utils/clickExternalLinkWithRetry';

// this is just an example test (e2e tests should be way longer than this)
test('Unauthenticated homepage: CFPB logo link goes to consumerfinance.gov', async ({
page,
}) => {
await page.goto('/');
await page.getByLabel('Home').click();
await clickLinkWithRetry({ page, target: page.getByLabel('Home') });
await expect(page).toHaveURL('https://www.consumerfinance.gov/');
});
31 changes: 31 additions & 0 deletions e2e/utils/clickExternalLinkWithRetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Locator, Page } from '@playwright/test';

/**
* Helps address intermittent failures when loading external links
* by attempting to click the link multiple times, hoping one of
* the retries succeeds.
* @param page Page object to allow navigation operations
* @param target Target that should be clicked
*/
export const clickLinkWithRetry = async ({
page,
target,
maxRetries = 2,
}: {
page: Page;
target: Locator;
maxRetries?: number;
}) => {
const URL_LOAD_FAILED = 'chrome-error://chromewebdata/';
let counter = 0;

await target.click();

while (page.url() === URL_LOAD_FAILED && counter < maxRetries) {
counter += 1;
// eslint-disable-next-line no-await-in-loop
await page.goBack();
// eslint-disable-next-line no-await-in-loop
await target.click();
}
};