Skip to content

Commit

Permalink
PIMS-632 Playwright (#2721)
Browse files Browse the repository at this point in the history
Co-authored-by: TaylorFries <[email protected]>
Co-authored-by: taylorfries <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2024
1 parent 8c3967a commit 990b842
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 2 deletions.
7 changes: 7 additions & 0 deletions e2e/.env-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BASE_URL=
BCSC_USERNAME=
BCSC_PASSWORD=
BCEID_USERNAME=
BCEID_PASSWORD=
IDIR_USERNAME=
IDIR_PASSWORD=
6 changes: 6 additions & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
*.env
21 changes: 21 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Playwright End-to-End Testing

End-to-end (e2e) testing involves testing the entire stack of the application, including the frontend (react-app), API (express-api), and database.

These tests should focus on user paths and should mimic how the user navigates the site.

## Setup

1. Run `npm i` to install dependencies.
2. Run `npx playwright install` to install Playwright browsers.
3. Create and populate a `.env` file based on the `.env-template` file.

For the `BASE_URL`, start with `localhost:<port>` for local testing.

Change BASE_URL to target other environments as needed.

## Run Tests

- `npx playwright test`: Runs tests in headless mode.
- `npx playwright test --ui`: Starts the Playwright UI.
- `npx playwright codegen <url>`: Starts the Codegen test maker. You can use this to capture movements in a browser and construct tests.
11 changes: 11 additions & 0 deletions e2e/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import dotenv from 'dotenv';

dotenv.config()

export const BASE_URL = process.env.BASE_URL ?? 'localhost:3000';
export const BCSC_USERNAME = process.env.BCSC_USERNAME ?? '';
export const BCSC_PASSWORD = process.env.BCSC_PASSWORD ?? '';
export const BCEID_USERNAME = process.env.BCEID_USERNAME ?? '';
export const BCEID_PASSWORD = process.env.BCEID_PASSWORD ?? '';
export const IDIR_USERNAME = process.env.IDIR_USERNAME ?? '';
export const IDIR_PASSWORD = process.env.IDIR_PASSWORD ?? '';
46 changes: 46 additions & 0 deletions e2e/functions/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Functions for automating user logins.
*/

import { Page } from '@playwright/test';
import {BASE_URL, BCEID_PASSWORD, BCEID_USERNAME, BCSC_PASSWORD, BCSC_USERNAME, IDIR_PASSWORD, IDIR_USERNAME} from '../env';

// Will only work in DEV/TEST environments
export const loginBCServicesCard = async (page: Page) => {
await page.goto(BASE_URL);
await page.getByRole('button', { name: 'Login' }).click();
await page.waitForLoadState('networkidle');
await page.getByRole('link', { name: 'BC Services Card' }).click();
await page.waitForLoadState('networkidle');
await page.getByLabel('Log in with Test with').click();
await page.waitForLoadState('networkidle');
await page.getByLabel('Email or username').click();
await page.getByLabel('Email or username').fill(BCSC_USERNAME);
await page.getByLabel('Password').click();
await page.getByLabel('Password').fill(BCSC_PASSWORD);
await page.getByRole('button', { name: 'Continue' }).click();
};

export const loginBCeID = async (page: Page) => {
await page.goto(BASE_URL);
await page.getByRole('button', { name: 'Login' }).click();
await page.waitForLoadState('networkidle');
await page.getByRole('link', { name: 'Basic or Business BCeID' }).click();
await page.waitForLoadState('networkidle');
await page.locator('#user').fill(BCEID_USERNAME);
await page.getByLabel('Password').click();
await page.getByLabel('Password').fill(BCEID_PASSWORD);
await page.getByRole('button', { name: 'Continue' }).click();
};

export const loginIDIR = async (page: Page) => {
await page.goto(BASE_URL);
await page.getByRole('button', { name: 'Login' }).click();
await page.waitForLoadState('networkidle');
await page.getByRole('link', { name: 'IDIR' }).click();
await page.waitForLoadState('networkidle');
await page.locator('#user').fill(IDIR_USERNAME);
await page.getByLabel('Password').click();
await page.getByLabel('Password').fill(IDIR_PASSWORD);
await page.getByRole('button', { name: 'Continue' }).click();
};
43 changes: 43 additions & 0 deletions e2e/functions/mockRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Page } from "@playwright/test";

interface User {
CreatedById: string;
CreatedOn: string;
UpdatedById: string;
UpdatedOn: string;
Id: string;
Username: string;
FirstName: string;
MiddleName: string | null;
LastName: string;
Email: string;
Position: string;
Note: string | null;
LastLogin: string;
ApprovedById: string;
ApprovedOn: string;
KeycloakUserId: string;
AgencyId: number;
RoleId: string | undefined;
Status: string;
}

/**
* Use this at the start of a test to set your user information if needed.
* Otherwise, your user information will reflect the actual user credentials used in testing.
* @param page Page of e2e test.
* @param self A partial object with user attributes.
* @param status An optional status number. Defaults to 200.
*/
export const mockSelf = async (page: Page, self?: Partial<User>, status: number = 200) => {
// Make the actual call, but then edit it with any partial user passed in before it gets back to the page
await page.route('**/users/self', async route => {
const response = await route.fetch();
let json = await response.json();
json = {
...json,
...self
}
await route.fulfill({ response, json });
})
};
17 changes: 17 additions & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "e2e",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "1.48.0",
"@types/node": "22.7.5"
},
"dependencies": {
"dotenv": "16.4.5"
}
}
80 changes: 80 additions & 0 deletions e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
expect: {
timeout: 10000,
},
testDir: './tests',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
screenshot: 'only-on-failure',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
// Webkit not compatible with Keycloak package
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
70 changes: 70 additions & 0 deletions e2e/tests/agencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Testing navigating to Agency Table and using some filter functions.
* There are some assumptions that you will have some standard agencies populated.
*/

import { test, expect, Page } from '@playwright/test'
import { BASE_URL } from '../env';
import { loginIDIR } from '../functions/login';
import { mockSelf } from '../functions/mockRequests';

const getToAgencies = async (page: Page) => {
await page.goto(BASE_URL);
await mockSelf(page, { RoleId: "00000000-0000-0000-0000-000000000000" }); // guarantee admin status
await loginIDIR(page);
await page.getByRole('heading', { name: 'Administration' }).waitFor();
await page.getByRole('heading', { name: 'Administration' }).click();
await page.getByRole('menuitem', { name: 'Agencies' }).click();
await page.getByText('Agencies Overview');
}

test('user can navigate to agencies page as an admin', async ({ page }) => {
await getToAgencies(page);
await expect(page.getByText('Agencies Overview')).toBeVisible();
})

test('user can filter using the keyword search', async ({ page }) => {
await getToAgencies(page);

await page.getByTestId('SearchIcon').click();
await page.getByPlaceholder('Search...').click();
await page.getByPlaceholder('Search...').fill('real');
await expect(page.getByRole('gridcell', { name: 'Real Property Division' })).toBeVisible();
// Clear filter and make sure it is empty
await page.getByLabel('Clear Filter').click();
await expect(page.getByPlaceholder('Search...')).toBeEmpty();
await expect(page.getByLabel('Clear Filter')).not.toBeVisible();
})

test('user can filter using the select dropdown', async ({ page }) => {
await getToAgencies(page);

await page.getByText('All Agencies').click();
await page.getByRole('option', { name: 'Disabled', exact: true }).click();
// Not clear what to look for here to guarantee that only Disabled agencies show
await expect(page.getByTestId('FilterAltIcon')).toBeVisible(); // The icon in the column header
await expect(page.getByLabel('Clear Filter')).toBeVisible();
await page.getByLabel('Clear Filter').click();
await expect(page.getByLabel('Clear Filter')).not.toBeVisible();
})

test('user can filter using the column filter', async ({ page }) => {
await getToAgencies(page);

await page.getByText('Name', { exact: true }).hover();
await page.getByRole('button', { name: 'Menu' }).click();
await page.getByRole('menuitem', { name: 'Filter' }).click();
await page.getByPlaceholder('Filter value').click();
await page.getByPlaceholder('Filter value').fill('real');
await page.getByText('Agencies Overview (1 rows)All').click(); // To get filter off screen
await expect(page.getByRole('gridcell', { name: 'Real Property Division' })).toBeVisible();
})

test('user select a row and go to that agency', async ({ page }) => {
await getToAgencies(page);

await page.getByRole('gridcell', { name: 'Advanced Education & Skills' }).first().click();
// These two expects together should confirm user is on correct details page
await expect(page.getByText('Agency Details')).toBeVisible();
await expect(page.getByText('Advanced Education & Skills')).toBeVisible();
})
22 changes: 22 additions & 0 deletions e2e/tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Testing login options.
*/

import { test, expect } from '@playwright/test';
import { loginBCServicesCard, loginBCeID, loginIDIR } from '../functions/login';

test('log in with BC Services Card', async ({ page }) => {
await loginBCServicesCard(page);
await expect(page.getByRole('button', { name: 'Logout' })).toBeVisible();
});

test('log in with BCeID', async ({ page }) => {
await loginBCeID(page);
await page.getByRole('button', { name: 'Logout' }).waitFor();
await expect(page.getByRole('button', { name: 'Logout' })).toBeVisible();
});

test('log in with IDIR', async ({ page }) => {
await loginIDIR(page);
await expect(page.getByRole('button', { name: 'Logout' })).toBeVisible();
});
Loading

0 comments on commit 990b842

Please sign in to comment.