forked from chairemobilite/transition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Playwright to the transition repo, and run a few simple tests on …
…github. FIx: chairemobilite#1065
- Loading branch information
1 parent
43f0ecd
commit 95b76f3
Showing
12 changed files
with
342 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,8 @@ MAIL_TRANSPORT_SMTP_AUTH_PWD=password | |
|
||
# From email | ||
MAIL_FROM_ADDRESS=[email protected] | ||
|
||
#Parameters used to login to a test account in the playwright tests | ||
PLAYWRIGHT_TEST_USER=testUser | ||
PLAYWRIGHT_TEST_EMAIL=[email protected] | ||
PLAYWRIGHT_TEST_PASSWORD=testPassword |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
lib/ | ||
node_modules/ | ||
**/__tests__ | ||
jestSetup.ts | ||
jestSetup.ts | ||
ui-tests/ | ||
playwright-example.config.ts | ||
playwright.config.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { defineConfig, devices } from '@playwright/test'; | ||
|
||
export default defineConfig({ | ||
testDir: './ui-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, | ||
// Each test is given 15 seconds. | ||
timeout: 15000, | ||
/* 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://localhost:8080', | ||
|
||
/* 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: 'Google Chrome', | ||
use: { ...devices['Desktop Chrome'], channel: '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, | ||
// }, | ||
}); |
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,21 @@ | ||
import { test } from '@playwright/test'; | ||
import * as testHelpers from './testHelpers'; | ||
import * as loginTestHelpers from './loginTestHelpers'; | ||
|
||
const context = { | ||
page: null as any, | ||
title: '', | ||
widgetTestCounters: {} | ||
}; | ||
|
||
// Configure the tests to run in serial mode (one after the other) | ||
test.describe.configure({ mode: 'serial' }); | ||
|
||
test.beforeAll(async ({ browser }) => { | ||
context.page = await testHelpers.initializeTestPage(browser); | ||
}); | ||
|
||
loginTestHelpers.startAndLoginAnonymously({ context, title: 'Transition' }); | ||
|
||
|
||
loginTestHelpers.logout({ context }); |
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,38 @@ | ||
/* | ||
* Copyright 2024, Polytechnique Montreal and contributors | ||
* | ||
* This file is licensed under the MIT License. | ||
* License text available at https://opensource.org/licenses/MIT | ||
*/ | ||
import * as testHelpers from './testHelpers'; | ||
|
||
/** | ||
* Test the survey's home page, login page, until the first section's page is | ||
* opened | ||
* @param {string} title - The title of the survey | ||
*/ | ||
export const startAndLoginAnonymously = ({ | ||
context, | ||
title | ||
}: { title: string} & testHelpers.CommonTestParameters) => { | ||
// Test the survey landing page | ||
testHelpers.hasTitleTest({ context, title }); | ||
testHelpers.hasUrlTest({ context, expectedUrl: '/login' }); | ||
testHelpers.hasFrenchTest({ context }); | ||
testHelpers.switchToEnglishTest({ context }); | ||
testHelpers.loginTest({ context }); | ||
|
||
// // Test the login page | ||
// testHelpers.registerWithoutEmailTest({ context }); | ||
|
||
// // Test the home section page | ||
// if (hasUser) { | ||
// testHelpers.hasUserTest({ context }); | ||
// } | ||
}; | ||
|
||
|
||
export const logout = ({ context }: testHelpers.CommonTestParameters) => { | ||
// Test the survey logout page | ||
testHelpers.logoutTest({ context }); | ||
}; |
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,125 @@ | ||
/* | ||
* Copyright 2024, Polytechnique Montreal and contributors | ||
* | ||
* This file is licensed under the MIT License. | ||
* License text available at https://opensource.org/licenses/MIT | ||
*/ | ||
import moment from 'moment'; | ||
import { test, expect, Page, Browser, Locator } from '@playwright/test'; | ||
import { env } from 'process'; | ||
|
||
|
||
// Types for the tests | ||
export type CommonTestParameters = { | ||
context: { | ||
// The main test page | ||
page: Page; | ||
// Store a counter for test names, to avoid duplicate test names. We have many objects to test and they may result in identical test names. | ||
widgetTestCounters: { [testKey: string]: number }; | ||
}; | ||
}; | ||
type Value = string; | ||
type StringOrBoolean = string | boolean; | ||
type Text = string; | ||
type Url = string; | ||
type Title = string; | ||
type Path = string; | ||
type Email = string; | ||
type PathAndValue = { path: Path; value: Value }; | ||
type PathAndValueBoolOrStr = { path: Path; value: StringOrBoolean }; | ||
type HasTitleTest = (params: { title: Title } & CommonTestParameters) => void; | ||
type HasFrenchTest = (params: CommonTestParameters) => void; | ||
type SwitchToLanguageTest = (params: CommonTestParameters) => void; | ||
type HasUrlTest = (params: { expectedUrl: Url } & CommonTestParameters) => void; | ||
type LoginTest = (params: CommonTestParameters) => void; | ||
type LogoutTest = (params: CommonTestParameters) => void; | ||
|
||
|
||
/** | ||
* Open the browser before all the tests and go to the home page | ||
* | ||
* @param {Browser} browser - The test browser object | ||
* @param {Object} options - The options for the test. | ||
* @param {{ [param: string]: string} } options.urlSearchParams - Additional | ||
* parameters to add to the URL as query string question. | ||
* @param {boolean} options.ignoreHTTPSErrors - Whether to ignore HTTPS errors. | ||
* These can happen if running the tests on a remote server with HTTPs (for | ||
* example test instances) | ||
*/ | ||
export const initializeTestPage = async ( | ||
browser: Browser, | ||
options: { urlSearchParams?: { [param: string]: string }, ignoreHTTPSErrors?: boolean } = {} | ||
): Promise<Page> => { | ||
const context = await browser.newContext({ ignoreHTTPSErrors: options.ignoreHTTPSErrors === true }); | ||
const page = await context.newPage(); | ||
|
||
const baseUrlString = test.info().project.use.baseURL; | ||
if (typeof baseUrlString === 'string' && options.urlSearchParams) { | ||
// Add the search params to the base URL | ||
const baseURL = new URL(baseUrlString); | ||
Object.keys(options.urlSearchParams).forEach((param) => { | ||
baseURL.searchParams.append(param, options.urlSearchParams![param]); | ||
}); | ||
await page.goto(baseURL.toString()); | ||
} else { | ||
// Go to home page | ||
await page.goto('/'); | ||
} | ||
|
||
return page; | ||
}; | ||
|
||
export const hasTitleTest: HasTitleTest = ({ context, title }) => { | ||
test(`Has title ${title}`, async () => { | ||
await expect(context.page).toHaveTitle(title); | ||
}); | ||
}; | ||
|
||
export const hasUrlTest: HasUrlTest = ({ context, expectedUrl }) => { | ||
test(`Current page has the url ${expectedUrl}`, async () => { | ||
await expect(context.page).toHaveURL(expectedUrl); | ||
}); | ||
}; | ||
|
||
// Test if the page has a french language | ||
export const hasFrenchTest: HasFrenchTest = ({ context }) => { | ||
test('Has French language', async () => { | ||
const englishButton = context.page.getByRole('button', { name: 'English' }); | ||
await expect(englishButton).toHaveText('English'); | ||
}); | ||
}; | ||
|
||
/** | ||
* Test if the page can switch to English language. | ||
* | ||
* @param {Object} context - The test context. | ||
* @param {Object} context.page - The page object from the test context. | ||
*/ | ||
export const switchToEnglishTest: SwitchToLanguageTest = ({ context }) => { | ||
test('Switch to English language', async () => { | ||
const englishButton = context.page.getByRole('button', { name: 'English' }); | ||
await englishButton.click(); | ||
const frenchButton = context.page.getByRole('button', { name: 'Français' }); | ||
await expect(frenchButton).toHaveText('Français'); | ||
}); | ||
}; | ||
|
||
export const loginTest: LoginTest = ({ context }) => { | ||
test(`Login to the test account`, async () => { | ||
const userNameField = context.page.locator("id=usernameOrEmail"); | ||
await userNameField.fill(process.env.PLAYWRIGHT_TEST_USER as string); | ||
const passwordField = context.page.locator("id=password"); | ||
await passwordField.fill(process.env.PLAYWRIGHT_TEST_PASSWORD as string); | ||
const loginButton = context.page.getByRole('button', { name: 'Login' }); | ||
await loginButton.click(); | ||
//await expect(context.page).toHaveURL('/dashboard'); | ||
}); | ||
}; | ||
|
||
export const logoutTest: LogoutTest = ({ context }) => { | ||
test('Logout from survey', async () => { | ||
const logoutButton = context.page.getByRole('button', { name: 'Logout' }); | ||
await logoutButton.click(); | ||
await expect(context.page).toHaveURL('/login'); | ||
}); | ||
}; |
Oops, something went wrong.