Skip to content

Commit

Permalink
Setup visual and a11y tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanteixeira committed Jul 25, 2022
1 parent 26e2a50 commit 3dd71f0
Show file tree
Hide file tree
Showing 11 changed files with 1,462 additions and 12 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,25 @@ jobs:
name: failure-screenshots-trace
path: test-results
retention-days: 10

visual_tests:
name: Visual Regression tests
runs-on: ubuntu-latest
env:
HAPPO_API_KEY: ${{ secrets.HAPPO_API_KEY }}
HAPPO_API_SECRET: ${{ secrets.HAPPO_API_SECRET }}
steps:
- name: Cancel previous runs
uses: styfle/cancel-workflow-action@0.9.0
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Run tests
run: yarn test:visual
env:
FORCE_COLOR: 2
26 changes: 26 additions & 0 deletions .happo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { RemoteBrowserTarget } = require('happo.io');

module.exports = {
apiKey: process.env.HAPPO_API_KEY,
apiSecret: process.env.HAPPO_API_SECRET,
compareThreshold: 0.005,
project: 'demo-playwright-test',
targets: {
chrome: new RemoteBrowserTarget('chrome', {
viewport: '1440x900'
}),
firefox: new RemoteBrowserTarget('firefox', {
viewport: '1440x900'
}),
safari: new RemoteBrowserTarget('safari', {
viewport: '1440x900',
scrollStitch: true
}),
iosSafari: new RemoteBrowserTarget('ios-safari', {
viewport: '320x568'
}),
ipadSafari: new RemoteBrowserTarget('ipad-safari', {
viewport: '1080x810'
})
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ Test reports can be generated with [Allure reports](https://github.com/allure-fr
## CI

The project uses [GitHub Actions](https://docs.github.com/en/actions) and tests are run automatically on PRs and on merge to `main` branch.

---

## Contributing

We have a [Kanban board](https://github.com/stefanteixeira/demo-playwright-test/projects/1) with next tasks to work on. If you are interested in contributing to the project, please reach out to @stefanteixeira to become a collaborator and get a task assigned to you.
13 changes: 13 additions & 0 deletions lib/a11y.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { injectAxe, checkA11y } = require('axe-playwright')

const testA11y = async (page, context = undefined) => {
await injectAxe(page)

await checkA11y(page, context, {
detailedReport: true
})
}

module.exports = {
testA11y
}
7 changes: 7 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const createUser = async page => {
await page.click('data-testid=cadastrar')
}

const openHomepage = async page => {
await page.goto('/cadastrarusuarios')
await createUser(page)
await page.waitForNavigation()
}

const getAuthToken = async request => {
const body = getUserBody()
const loginBody = {
Expand All @@ -77,5 +83,6 @@ module.exports = {
getCartBody,
getCartId,
createUser,
openHomepage,
getAuthToken
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
"@playwright/test": "^1.22.1",
"allure-commandline": "^2.17.2",
"allure-playwright": "^2.0.0-beta.16",
"axe-playwright": "^1.1.11",
"eslint": "^8.15.0",
"eslint-plugin-playwright": "^0.9.0",
"expect-playwright": "^0.8.0",
"happo-e2e": "^1.2.0",
"happo-playwright": "^1.1.0",
"happo.io": "^7.2.1",
"playwright": "^1.22.1",
"wait-on": "^6.0.1"
},
Expand All @@ -26,6 +30,8 @@
"test:api:ci:wait": "wait-on -i 1000 -t 30000 -l http://localhost:3000",
"test:api:ci": "yarn test:api:ci:wait && yarn test:api --workers 2",
"test:e2e": "yarn playwright test --project e2e --config playwright.config.js",
"test:e2e:ci": "yarn test:e2e --workers 2"
"test:e2e:ci": "yarn test:e2e --workers 2",
"test:visual": "HAPPO_DOWNLOAD_ALL=true yarn happo-e2e -- -- yarn playwright test --project visual --config playwright.config.js",
"test:visual:ci": "yarn test:visual --workers 2"
}
}
9 changes: 8 additions & 1 deletion playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ module.exports = {
headless: true
}
}
}
},
{
name: 'visual',
testMatch: '**/*.visual.test.js',
use: {
baseURL: 'https://front.serverest.dev'
}
},
]
}
6 changes: 6 additions & 0 deletions tests/e2e/create-user.e2e.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { test, expect } = require('@playwright/test')

const { createUser } = require('../../lib/helpers')
const { testA11y } = require('../../lib/a11y')

test.describe.parallel('Create user', () => {
test.beforeEach(async ({ page }) => {
Expand All @@ -20,4 +21,9 @@ test.describe.parallel('Create user', () => {
await page.waitForNavigation()
await expect(page).toHaveURL('/admin/home')
})

// eslint-disable-next-line playwright/no-skipped-test
test.skip('has no a11y issues in the form', async ({ page }) => {
await testA11y(page, '.form')
})
})
27 changes: 27 additions & 0 deletions tests/visual/login.visual.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { test } = require('@playwright/test')
const happoPlaywright = require('happo-playwright')

test.describe('Login page', () => {
let context, page

test.beforeAll(async ({ browser }) => {
context = await browser.newContext()
page = await context.newPage()
await happoPlaywright.init(context)
})

test.afterAll(async () => {
await happoPlaywright.finish()
})

test('has correct form layout', async () => {
await page.goto('/login')

const form = page.locator('form')

await happoPlaywright.screenshot(page, form, {
component: 'Login Form',
variant: 'Default'
})
})
})
29 changes: 29 additions & 0 deletions tests/visual/navbar.visual.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { test } = require('@playwright/test')
const happoPlaywright = require('happo-playwright')

const { openHomepage } = require('../../lib/helpers')

test.describe('Navbar', () => {
let context, page

test.beforeAll(async ({ browser }) => {
context = await browser.newContext()
page = await context.newPage()
await happoPlaywright.init(context)

await openHomepage(page)
})

test.afterAll(async () => {
await happoPlaywright.finish()
})

test('has correct layout', async () => {
const form = page.locator('.navbar')

await happoPlaywright.screenshot(page, form, {
component: 'Navbar',
variant: 'Default'
})
})
})

0 comments on commit 3dd71f0

Please sign in to comment.