Skip to content

Commit

Permalink
Add login and empty environment Playwright fixtures (#2864)
Browse files Browse the repository at this point in the history
* Add missing wait request

* Add missing wait

* Make intercept alias obligatory

* Increase local timeout

* Prevent test from writing to log history

* Update tests

* Refactor conditional tests to use fixtures

* Undo timeout change
  • Loading branch information
cesarvarela authored Jun 18, 2024
1 parent 8cf3e5e commit 5acd06a
Show file tree
Hide file tree
Showing 5 changed files with 591 additions and 520 deletions.
3 changes: 3 additions & 0 deletions site/gatsby-site/playwright/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type ConfigType = {
E2E_ADMIN_PASSWORD: string;
E2E_ADMIN_USERNAME: string;
IS_EMPTY_ENVIRONMENT: string;
[key: string]: string;
};

Expand Down
67 changes: 46 additions & 21 deletions site/gatsby-site/playwright/e2e/cite.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { conditionalIntercept, waitForRequest, query, login, maybeIt, mockDate } from '../utils';
import { conditionalIntercept, waitForRequest, query, mockDate, test } from '../utils';
import flaggedReport from '../fixtures/reports/flagged.json';
import unflaggedReport from '../fixtures/reports/unflagged.json';
import upsertDuplicateClassification from '../fixtures/classifications/upsertDuplicateClassification.json';
Expand All @@ -10,7 +10,7 @@ import incident50 from '../fixtures/incidents/fullIncident50.json';
import { transformIncidentData, deleteIncidentTypenames } from '../../src/utils/cite';
import { transformReportData, deleteReportTypenames } from '../../src/utils/reports';
import { gql } from '@apollo/client';
import { test, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import config from '../config';

test.describe('Cite pages', () => {
Expand Down Expand Up @@ -53,8 +53,8 @@ test.describe('Cite pages', () => {
await page.goto(url);
});

maybeIt('Should show an edit link to users with the appropriate role', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should show an edit link to users with the appropriate role', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

const id = 'r3';

Expand Down Expand Up @@ -327,8 +327,8 @@ test.describe('Cite pages', () => {
await expect(page.locator('[data-cy="header-previous-incident-link"]')).toHaveAttribute('href', `/cite/${lastIncidentId - 1}`);
});

maybeIt('Should show the edit incident form', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should show the edit incident form', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

await page.goto(url);

Expand Down Expand Up @@ -405,8 +405,8 @@ test.describe('Cite pages', () => {
await expect(page.locator('[data-cy="edit-similar-incidents"]')).not.toBeVisible();
});

maybeIt('Should display edit link when logged in as editor', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should display edit link when logged in as editor', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

await page.goto('/cite/9');

Expand Down Expand Up @@ -476,8 +476,8 @@ test.describe('Cite pages', () => {
expect(input).toEqual(expectedIncident);
});

maybeIt('Should flag an incident as not related (authenticated)', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should flag an incident as not related (authenticated)', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

await conditionalIntercept(
page,
Expand Down Expand Up @@ -583,8 +583,8 @@ test.describe('Cite pages', () => {
await expect(page.locator('head meta[property="twitter:image"]')).toHaveAttribute('content');
});

maybeIt('Should subscribe to incident updates (user authenticated)', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should subscribe to incident updates (user authenticated)', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

await page.goto('/cite/51');

Expand All @@ -599,10 +599,13 @@ test.describe('Cite pages', () => {
},
},
},
'upsertSubscription'
);

await page.locator('button:has-text("Notify Me of Updates")').click();

await waitForRequest('upsertSubscription');

await expect(page.locator('[data-cy="toast"]')).toBeVisible();

await expect(page.locator('[data-cy="toast"]')).toHaveText(
Expand Down Expand Up @@ -659,8 +662,8 @@ test.describe('Cite pages', () => {
await expect(page).toHaveURL(new RegExp('/cite/10'));
});

maybeIt('Should link similar incidents', async ({ page }) => {
await login(page, config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);
test('Should link similar incidents', async ({ page, login }) => {
await login(config.E2E_ADMIN_USERNAME, config.E2E_ADMIN_PASSWORD);

await conditionalIntercept(
page,
Expand Down Expand Up @@ -704,29 +707,51 @@ test.describe('Cite pages', () => {
'updateDissimilarIncidents'
);

await conditionalIntercept(
page,
'**/graphql',
(req) =>
req.postDataJSON().operationName === 'logIncidentHistory',
{
"data": {
"logIncidentHistory": {
"incident_id": 50,
"__typename": "LogIncidentHistoryPayload"
}
}
},
'logIncidentHistory'
);

await page.goto('/incidents/edit/?incident_id=50');

await page.locator('[data-cy="similar-id-input"]').fill('123');

await page.locator('[data-cy="related-byId"] [data-cy="result"]:nth-child(1) button:has-text("Yes")').click();
await page.locator('[data-cy="related-byId"] [data-cy="result"]:nth-child(1)').getByText("Yes").click();

await page.locator('[data-cy="similar-id-input"]').fill('456');

await page.locator('[data-cy="related-byId"] [data-cy="result"]:nth-child(1) button:has-text("No")').click();
await page.locator('[data-cy="related-byId"] [data-cy="result"]:nth-child(1)').getByText('No', { exact: true }).click();

await page.locator('button[type="submit"]').click();

const updateSimilarIncidentsRequest = await waitForRequest('updateSimilarIncidents');
await waitForRequest('updateIncident');

await waitForRequest('logIncidentHistory');

const updateDissimilarIncidentsRequest = await waitForRequest('updateDissimilarIncidents');

expect(updateDissimilarIncidentsRequest.postDataJSON().variables.query).toEqual({ incident_id_in: [456] });
expect(updateDissimilarIncidentsRequest.postDataJSON().variables.set).toEqual({
editor_dissimilar_incidents: [50],
});

const updateSimilarIncidentsRequest = await waitForRequest('updateSimilarIncidents');

expect(updateSimilarIncidentsRequest.postDataJSON().variables.query).toEqual({ incident_id_in: [123] });
expect(updateSimilarIncidentsRequest.postDataJSON().variables.set).toEqual({
editor_similar_incidents: [50],
});

expect(updateDissimilarIncidentsRequest.postDataJSON().variables.query).toEqual({ incident_id_in: [456] });
expect(updateDissimilarIncidentsRequest.postDataJSON().variables.set).toEqual({
editor_dissimilar_incidents: [50],
});
});
});
41 changes: 21 additions & 20 deletions site/gatsby-site/playwright/e2e/discover.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, Page, Download } from '@playwright/test';
import { conditionalIntercept, conditionalIt, mockDate, waitForRequest } from '../utils';
import { expect, Page, Download } from '@playwright/test';
import { conditionalIntercept, mockDate, waitForRequest, test } from '../utils';
import flaggedReport from '../fixtures/reports/flagged.json';
import unflaggedReport from '../fixtures/reports/unflagged.json';
import { getUnixTime } from 'date-fns';
Expand All @@ -15,7 +15,7 @@ test.describe('The Discover app', () => {
await page.goto(url);
});

conditionalIt(process.env.isEmptyEnvironment === 'true', 'Should display empty state when no incidents are available', async ({ page }) => {
test('Should display empty state when no incidents are available', async ({ page, runOnlyOnEmptyEnvironment }) => {
await page.goto(url);

await expect(async () => {
Expand All @@ -27,7 +27,7 @@ test.describe('The Discover app', () => {
await expect(page.locator('text=Your search returned no results.')).toBeVisible();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Should default to incident reports and show at least 30', async ({ page }) => {
test('Should default to incident reports and show at least 30', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await expect(async () => {
Expand All @@ -43,7 +43,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Performs a search and filters results', async ({ page }) => {
test('Performs a search and filters results', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.fill('[data-cy="search-box"] input[placeholder="Type Here"]', 'starbucks');
Expand All @@ -59,7 +59,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Filters by incident Id using top filters', async ({ page }) => {
test('Filters by incident Id using top filters', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.click('[data-cy=expand-filters]');
Expand All @@ -80,7 +80,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Filters by Language using top filters', async ({ page }) => {
test('Filters by Language using top filters', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.click('[data-cy=expand-filters]');
Expand All @@ -101,7 +101,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Shows expected filters', async ({ page }) => {
test('Shows expected filters', async ({ page, skipOnEmptyEnvironment }) => {
await page.setViewportSize({ width: 1920, height: 1080 });
await page.goto(url);

Expand All @@ -127,7 +127,7 @@ test.describe('The Discover app', () => {
await expect(page.locator('button:has-text("Submitter")')).toBeVisible();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Filters by Tags using top filters', async ({ page }) => {
test('Filters by Tags using top filters', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.click('[data-cy=expand-filters]');
Expand All @@ -148,7 +148,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(process.env.isEmptyEnvironment !== 'true', 'Filters by incident Id using card button', async ({ page }) => {
test('Filters by incident Id using card button', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.click('[data-cy=expand-filters]');
Expand All @@ -167,13 +167,14 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(!process.env.isEmptyEnvironment, 'Should flag an incident', async ({ page }) => {
test('Should flag an incident', async ({ page, skipOnEmptyEnvironment }) => {

await conditionalIntercept(
page,
'**/graphql',
(req) => req.postDataJSON().operationName === 'FindReport',
unflaggedReport
unflaggedReport,
'findReport',
);


Expand Down Expand Up @@ -245,7 +246,7 @@ test.describe('The Discover app', () => {
await expect(modal).not.toBeVisible();
});

conditionalIt(!process.env.isEmptyEnvironment, 'Opens an archive link', async ({ page }) => {
test('Opens an archive link', async ({ page, skipOnEmptyEnvironment }) => {

await page.goto(url);

Expand Down Expand Up @@ -315,7 +316,7 @@ test.describe('The Discover app', () => {
await expect(page.locator('button:has-text("Clear Filter")')).toBeDisabled();
});

conditionalIt(!process.env.isEmptyEnvironment, 'Should sort by incident date', async ({ page }) => {
test('Should sort by incident date', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.locator('[data-cy="discover-sort"]').click();
Expand All @@ -336,7 +337,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(!process.env.isEmptyEnvironment, 'Should sort by published date', async ({ page }) => {
test('Should sort by published date', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.locator('[data-cy="discover-sort"]').click();
Expand Down Expand Up @@ -373,7 +374,7 @@ test.describe('The Discover app', () => {
await expect(page).toHaveURL(/\?is_incident_report=true$/);
});

conditionalIt(!process.env.isEmptyEnvironment, 'Should export results to a CSV file', async ({ page }) => {
test('Should export results to a CSV file', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.locator('[data-cy="search-box"] input[placeholder="Type Here"]').type('starbucks');
Expand Down Expand Up @@ -419,7 +420,7 @@ test.describe('The Discover app', () => {
await expect(page.locator('[data-cy="discover-sort"]')).toHaveText('Newest Incident Date');
});

conditionalIt(!process.env.isEmptyEnvironment, 'Should default to the featured incidents', async ({ page }) => {
test('Should default to the featured incidents', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

for (const item of config.header.search.featured) {
Expand All @@ -428,7 +429,7 @@ test.describe('The Discover app', () => {
}
});

conditionalIt(!process.env.isEmptyEnvironment, 'Performs a search and filters results by source', async ({ page }) => {
test('Performs a search and filters results by source', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.locator('[data-cy="search-box"] input[placeholder="Type Here"]').fill('google');
Expand All @@ -454,7 +455,7 @@ test.describe('The Discover app', () => {
}).toPass();
});

conditionalIt(!process.env.isEmptyEnvironment, 'Loads filters based on URL', async ({ page }) => {
test('Loads filters based on URL', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url + '?is_incident_report=true&submitters=Anonymous&page=3&classifications=CSETv0%3AIntent%3AAccident');

await expect(page.locator('button:has-text("Submitters") span.badge')).toContainText('1');
Expand Down Expand Up @@ -485,7 +486,7 @@ test.describe('The Discover app', () => {
await expect(page.locator('[data-cy="display-mode-details"]')).toHaveClass(/selected/);
});

conditionalIt(!process.env.isEmptyEnvironment, 'Search using the classifications filter', async ({ page }) => {
test('Search using the classifications filter', async ({ page, skipOnEmptyEnvironment }) => {
await page.goto(url);

await page.locator('[data-cy=expand-filters]').click();
Expand Down
Loading

0 comments on commit 5acd06a

Please sign in to comment.