Skip to content

Commit

Permalink
Test frontend uploads with playwright
Browse files Browse the repository at this point in the history
- Install and configure playwright
- Includes README.md with helpful playwright commands
- Add basic upload and nav tests
  • Loading branch information
VirginiaDooley committed Sep 30, 2024
1 parent ff78a04 commit e4e2edf
Show file tree
Hide file tree
Showing 7 changed files with 684 additions and 252 deletions.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
772 changes: 521 additions & 251 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"node": ">=18"
},
"scripts": {
"build": "gulp"
"build": "gulp",
"test": "playwright test"
},
"dependencies": {
"filepond": "4.26.1",
Expand All @@ -15,5 +16,9 @@
"gulp": "^4.0.2",
"gulp-cli": "^2.3.0",
"gulp-concat": "^2.6.1"
},
"devDependencies": {
"@playwright/test": "^1.47.2",
"@types/node": "^22.7.4"
}
}
79 changes: 79 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @ts-check
const { defineConfig, devices } = require('@playwright/test');

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

/**
* @see https://playwright.dev/docs/test-configuration
*/
module.exports = defineConfig({
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: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

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

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
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,
// },
});

25 changes: 25 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Playwright tests exist in /tests. Before running any of the following commands, make sure you have installed the dependencies by running `npm install` and started the server by running `pipenv python manage.py runserver`.

npx playwright test
Runs the end-to-end tests.

npx playwright test --ui
Starts the interactive UI mode.

npx playwright test --project=chromium
Runs the tests only on Desktop Chrome.

npx playwright test example
Runs the tests in a specific file.

npx playwright test --debug
Runs the tests in debug mode.

npx playwright codegen
Auto generate tests with Codegen.

We suggest that you begin by typing:

npx playwright test

Visit https://playwright.dev/docs/intro for more information. ✨
53 changes: 53 additions & 0 deletions tests/leaflet_crud.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { test } from '@playwright/test';
import fs from 'fs';
import path from 'path';

const baseUrl = 'http://localhost:8000/';

async function navigateToUploadPage(page) {
await page.goto(baseUrl);
await page.getByRole('link', { name: 'Upload a leaflet' }).click();
await page.getByText('Take a photo of a leaflet').click();
}

async function fillPostcodeAndSubmit(page, postcode = 'W8 4NU') {
await page.getByLabel('What postcode was this').click();
await page.getByLabel('What postcode was this').fill(postcode);
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByText('Green Party').click();
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByText('Thank you so much!');
}

// Increase the timeout for all tests
test.setTimeout(60000);

test('basic upload test', async ({ page }) => {
await navigateToUploadPage(page);
await page.getByLabel('Take a photo of a leaflet').setInputFiles('electionleaflets/test_media/test_images/test_leaflet.jpeg');
await page.getByRole('button', { name: 'Continue' }).click();
await fillPostcodeAndSubmit(page);
});

test('uploading multiple leaflet images', async ({ page }) => {
await navigateToUploadPage(page);
const imageDir = path.resolve(__dirname, '../electionleaflets/test_media/test_images');
const imageFiles = fs.readdirSync(imageDir).map(file => path.join(imageDir, file));
await page.getByLabel('Take a photo of a leaflet').setInputFiles(imageFiles);
await page.getByRole('button', { name: 'Continue' }).click();
await fillPostcodeAndSubmit(page);
});

test('uploading a non-image leaflet file', async ({ page }) => {
await navigateToUploadPage(page);
await page.getByLabel('Take a photo of a leaflet').setInputFiles('electionleaflets/test_media/test_images/test_leaflet.docx');
await page.getByRole('button', { name: 'Continue' }).click();
await page.getByText('Error during upload tap to retry');
});

test('read leaflet from latest leaflets', async ({ page }) => {
await page.goto(baseUrl);
await page.getByRole('link', { name: 'Latest leaflets' }).click();
await page.locator('.ds-card-link').first().click();
await page.getByText('Leaflet details');
});

0 comments on commit e4e2edf

Please sign in to comment.