Skip to content

Commit

Permalink
feat(playwright): add screenshots for elements
Browse files Browse the repository at this point in the history
fix: visual regression screenshots going in the wrong folder
  • Loading branch information
dnotes committed Oct 30, 2024
1 parent 974e09d commit 7f50c7d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-spies-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quickpickle/playwright": patch
---

added screenshots for elements, and fixed save location for visual regression tests
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
11 changes: 11 additions & 0 deletions packages/playwright/src/actions.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ Then('(I )take (a )screenshot named {string}', async function (world:PlaywrightW
let path = sanitizeFilepath(`${projectRoot}/${world.worldConfig.screenshotDir}/${name}${explodedTags}.png`)
await world.page.screenshot({ path })
})
Then('(I )take (a )screenshot of the {string} {word}', async function (world:PlaywrightWorld, identifier:string, role:string) {
let locator = await getLocator(world.page, identifier, role)
let path = sanitizeFilepath(`${projectRoot}/${defaultScreenshotPath(world)}`)
await locator.screenshot({ path })
})
Then('(I )take (a )screenshot of the {string} {word} named {string}', async function (world:PlaywrightWorld, identifier:string, role:string, name:string) {
let locator = await getLocator(world.page, identifier, role)
let explodedTags = world.info.explodedIdx ? `_(${world.info.tags.join(',')})` : ''
let path = sanitizeFilepath(`${projectRoot}/${world.worldConfig.screenshotDir}/${name}${explodedTags}.png`)
await locator.screenshot({ path })
})

// ================
// Browser size
Expand Down
16 changes: 13 additions & 3 deletions packages/playwright/src/outcomes.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Then } from "quickpickle";
import type { PlaywrightWorld } from "./PlaywrightWorld";
import { expect, Locator, Page } from '@playwright/test'
import './snapshotMatcher'
import { defaultScreenshotPath, getLocator, testMetatag } from "./helpers";
import { defaultScreenshotPath, getLocator, sanitizeFilepath, testMetatag } from "./helpers";

// ================
// Text on page
Expand Down Expand Up @@ -256,10 +256,20 @@ Then('the meta( )tag {string} should not/NOT contain/include/be/equal {string}',

// Visual regression testing
Then('(the )screenshot should match', async function (world:PlaywrightWorld) {
await expect(world.page).toMatchScreenshot(`${world.worldConfig.screenshotDir}/${defaultScreenshotPath(world)}`)
await expect(world.page).toMatchScreenshot(defaultScreenshotPath(world))
})
Then('(the )screenshot {string} should match', async function (world:PlaywrightWorld, name:string) {
await expect(world.page).toMatchScreenshot(`${world.worldConfig.screenshotDir}/${name}.png`)
let explodedTags = world.info.explodedIdx ? `_(${world.info.tags.join(',')})` : ''
await expect(world.page).toMatchScreenshot(`${world.worldConfig.screenshotDir}/${name}${explodedTags}.png`)
})
Then('(the )screenshot of the {string} {word} should match', async function (world:PlaywrightWorld, identifier, role) {
let locator = await getLocator(world.page, identifier, role)
await expect(locator).toMatchScreenshot(defaultScreenshotPath(world))
})
Then('(the )screenshot {string} of the {string} {word} should match', async function (world:PlaywrightWorld, name, identifier, role) {
let locator = await getLocator(world.page, identifier, role)
let explodedTags = world.info.explodedIdx ? `_(${world.info.tags.join(',')})` : ''
await expect(locator).toMatchScreenshot(`${world.worldConfig.screenshotDir}/${name}${explodedTags}.png`)
})

// Browser context
Expand Down
43 changes: 37 additions & 6 deletions packages/playwright/tests/playwright.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,46 @@ Feature: Basic tests of Playwright browser and steps
Then the screenshot should match

Example: Passing named visual regression test
When I load the file "tests/examples/simple.html"
Then the screenshot "visual-regression-simple-page" should match

Example: Passing visual regression test of an element
When I load the file "tests/examples/example.html"
Then the screenshot of the "XKCD Comic" img should match

Example: Passing named visual regression test of an element
When I load the file "tests/examples/example.html"
Then the screenshot "playwright-example" should match
Then the screenshot "visual-regression-faq-section" of the "#faq" element should match

@fails
Example: Failing visual regression test
When I load the file "tests/examples/simple.html"
Then the file "screenshots/playwright-example.png.diff.png" should not exist
And the screenshot "playwright-example" should match
When I load the file "tests/examples/example.html"
Then the file "screenshots/visual-regression-simple-page.png.diff.png" should not exist
And the file "screenshots/visual-regression-simple-page.png.actual.png" should not exist
And the screenshot "visual-regression-simple-page" should match

Scenario: Delete the visual regression failure file
Then the file "screenshots/playwright-example.png.diff.png" should not exist
And the file "screenshots/playwright-example.png.actual.png" should exist--delete it
Then the file "screenshots/visual-regression-simple-page.png.diff.png" should not exist
And the file "screenshots/visual-regression-simple-page.png.actual.png" should exist--delete it

@skip-ci
Rule: Screenshots should be placed in the proper directory

Scenario: Taking a screenshot
When I take a screenshot
Then the file "screenshots/Feature: Basic tests of Playwright browser and steps_Taking a screenshot_01.png" should exist--delete it

Scenario: Taking a named screenshot
When I take a screenshot named "test"
Then the file "screenshots/test.png" should exist--delete it

Scenario: Taking a screenshot of an element
Given I load the file "tests/examples/example.html"
When I take a screenshot of the "Image" link
Then the file "screenshots/Feature: Basic tests of Playwright browser and steps_Taking a screenshot of an element_02.png" should exist--delete it

Scenario: Taking a named screenshot of an element
Given I load the file "tests/examples/example.html"
When I take a screenshot of the "XKCD Comic" img named "test2"
Then the file "screenshots/test2.png" should exist--delete it

0 comments on commit 7f50c7d

Please sign in to comment.