-
Notifications
You must be signed in to change notification settings - Fork 10
feat(js): add playwright+mocha test to capture test context #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| { | ||
| "name": "capture-context", | ||
| "private": true, | ||
| "scripts": { | ||
| "test": "mocha --timeout 5000 'tests/login.js'" | ||
| }, | ||
| "devDependencies": { | ||
| "@axe-core/watcher": "^4.0.0", | ||
| "chai": "^4", | ||
| "mocha": "^10.6.0", | ||
| "playwright": "^1.45.1" | ||
| } | ||
| } |
This file contains hidden or 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,101 @@ | ||
| const { assert } = require('chai') | ||
| const playwright = require('playwright') | ||
| const { | ||
| wrapPlaywrightPage, | ||
| PlaywrightController, | ||
| playwrightConfig | ||
| } = require('@axe-core/watcher/playwright') | ||
|
|
||
| /* Get your configuration from environment variables. */ | ||
| const { | ||
| API_KEY, | ||
| PROJECT_ID, | ||
| SERVER_URL = 'https://axe.deque.com' | ||
| } = process.env | ||
|
|
||
| describe('My Login Application', () => { | ||
| let browserContext | ||
| let page | ||
| let controller | ||
|
|
||
| before(async () => { | ||
| browserContext = await playwright.chromium.launchPersistentContext( | ||
| '', | ||
| playwrightConfig({ | ||
| axe: { | ||
| apiKey: API_KEY, | ||
| projectId: PROJECT_ID, | ||
| serverURL: SERVER_URL | ||
| }, | ||
| //@see: https://playwright.dev/docs/chrome-extensions#headless-mode | ||
| headless: false, | ||
| args: ['--headless=new'] | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| after(async () => { | ||
| await browserContext.close() | ||
|
Garbee marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| // Note: function expression (not arrow) so `this.currentTest` is bound | ||
| // to Mocha's Context. Arrow functions would leave it undefined. | ||
| beforeEach(async function () { | ||
| // Create a fresh page instance for each test. | ||
| page = await browserContext.newPage() | ||
|
|
||
| // Initialize the PlaywrightController by passing in the Playwright page. | ||
| controller = new PlaywrightController(page) | ||
|
|
||
| // Use the new wrapped Playwright page instance. | ||
| page = wrapPlaywrightPage(page, controller) | ||
|
|
||
| // Tell axe Watcher which test each scan in this test belongs to. | ||
| controller.setTestContext({ | ||
| test_file_path: this.currentTest?.file ?? null, | ||
| test_location: this.currentTest?.titlePath() ?? null | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| /* Flush axe-watcher results after each test. */ | ||
| await controller.flush() | ||
|
|
||
| // Clear so no stray scan after flush picks up the previous test's context. | ||
| controller.setTestContext(null) | ||
|
|
||
| await page.close() | ||
|
Garbee marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| describe('Login', () => { | ||
| describe('with valid credentials', () => { | ||
| it('should login', async () => { | ||
| await page.goto('https://the-internet.herokuapp.com/login') | ||
|
|
||
| await page.locator('#username').fill('tomsmith') | ||
| await page.locator('#password').fill('SuperSecretPassword!') | ||
|
|
||
| await page.locator('button[type="submit"]').click() | ||
|
|
||
| const element = await page.waitForSelector('#flash') | ||
|
|
||
| assert.isNotNull(element) | ||
| }) | ||
|
|
||
| it('should be able to logout', async () => { | ||
| await page.goto('https://the-internet.herokuapp.com/login') | ||
|
|
||
| await page.locator('#username').fill('tomsmith') | ||
| await page.locator('#password').fill('SuperSecretPassword!') | ||
| await page.locator('button[type="submit"]').click() | ||
|
|
||
| await page.waitForSelector('#flash') | ||
|
|
||
| // Logout and verify we're back on the login page | ||
| await page.locator('a[href="/logout"]').click() | ||
|
|
||
| await page.waitForSelector('#flash') | ||
|
Garbee marked this conversation as resolved.
|
||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.