Skip to content

Commit

Permalink
Change Discover app tests by mocking Wayback Machine API responses fo…
Browse files Browse the repository at this point in the history
…r improved reliability (#3339)
  • Loading branch information
pdcp1 authored Jan 29, 2025
1 parent 8c95bef commit 26957e8
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions site/gatsby-site/playwright/e2e/discover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ test.describe('The Discover app', () => {

test.slow();

const reportUrl = 'https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/';

// Define the mock response from the Wayback Machine API
const mockedTimestamp = '20250117132643X';

const mockResponse = {
url: reportUrl,
archived_snapshots: {
closest: {
status: '200',
available: true,
url: `http://web.archive.org/web/${mockedTimestamp}/${reportUrl}`,
timestamp: mockedTimestamp,
}
}
};

// Intercept the request to the Wayback Machine API
await page.route(`https://archive.org/wayback/available?url=${reportUrl}`, async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(mockResponse)
});
});

await page.goto(url);

await page.locator('[data-cy="web-archive-link"] [data-cy="dropdown-toggle"]').first().click();
Expand All @@ -265,7 +291,7 @@ test.describe('The Discover app', () => {

await expect(async () => {
await expect(popup).toBeTruthy();
await expect(popup).toHaveURL('https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/');
await expect(popup).toHaveURL(reportUrl);
}).toPass();

popup = null;
Expand All @@ -278,14 +304,10 @@ test.describe('The Discover app', () => {
await expect(waybackMachineLink).toBeVisible();
await waybackMachineLink.click();

const response = await (await fetch('https://archive.org/wayback/available?url=https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/')).json();

const expectedTimestamp = response.archived_snapshots.closest.timestamp;

await expect(async () => {
await expect(popup).toBeTruthy();
await expect(popup).toHaveURL(`https://web.archive.org/web/${expectedTimestamp}/https://www.cbsnews.com/news/is-starbucks-shortchanging-its-baristas/`);
}).toPass();
await expect(popup).toHaveURL(mockResponse.archived_snapshots.closest.url.replace('http:', 'https:'));
}).toPass({ timeout: 10000 });
});

test('Lets you filter by type', async ({ page }) => {
Expand Down

0 comments on commit 26957e8

Please sign in to comment.