generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[E2E] Get tests to consistently pass in
headless
mode (#1036)
Closes #987 The failures I've experienced seem to be isolated to me + my machine, and most consistently in `headless` mode, but this workaround is implemented in a way that should not interfere with others' consistently passing tests. ## Changes - Implements retries when clicking a link that navigates to a new domain for the first time ## How to test this PR 1. Start local SBL Platform 2. `yarn playwright test --workers 4` 3. Ensure all tests pass ## Screenshots ![screencapture-localhost-9323-2024-10-30-13_31_18](https://github.com/user-attachments/assets/4ace1a05-fd75-4632-8502-4f2f9f1f6806)
- Loading branch information
Showing
4 changed files
with
63 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...lending-platform/unauthenticated-homepage/sampleTestCfpbLogoGoesToConsumerFinance.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |