Skip to content

Commit

Permalink
fix(playwright): provide better errors for metatag tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dnotes committed Oct 29, 2024
1 parent 724e110 commit 7fc2026
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-ears-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quickpickle/playwright": patch
---

provide better error messages for metatag tests
10 changes: 8 additions & 2 deletions packages/playwright/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ export async function setValue(locator:Locator, value:string|any) {
}
}

export async function testMetatag(page:Page, name:string, expected:string, exact:boolean) {
export async function testMetatag(page:Page, name:string, expected:string, exact:boolean, expectMatching = true) {
let actual:string|null

if (name === 'title') actual = await page.title()
else actual = await (await page.locator(`meta[name="${name}"]`)).getAttribute('content')

return exact ? actual === expected : actual?.includes(expected)
let matches = exact ? actual === expected : actual?.includes(expected)

if (matches !== expectMatching) {
let word = exact ? 'exactly match' : 'contain'
let not = expectMatching ? '' : 'not '
throw new Error(`Expected ${name} metatag ${not }to ${word} '${expected}' but got '${actual}'`)
}
}

export function sanitizeFilepath(filepath:string) {
Expand Down
8 changes: 4 additions & 4 deletions packages/playwright/src/outcomes.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Then('a/an/the (value of ){string} should contain/include/be/equal {string}', as
})
Then('a/an/the (value of )(the ){string} {word} should contain/include/be/equal {string}', async function (world:PlaywrightWorld, identifier, role, expected) {
let exact = world.info.step?.match(/ should (?:be|equal) ['"]/) ? true : false
if (role === 'metatag') await expect(await testMetatag(world.page, identifier, expected, exact)).toBe(true)
if (role === 'metatag') await testMetatag(world.page, identifier, expected, exact)
else {
let locator = await getLocator(world.page, identifier, role)
if (exact) await expect(locator).toHaveValue(expected)
Expand All @@ -233,7 +233,7 @@ Then('a/an/the (value of )(the ){string} should not/NOT contain/include/be/equal
})
Then('a/an/the (value of )(the ){string} {word} should not/NOT contain/include/be/equal {string}', async function (world:PlaywrightWorld, identifier, role, expected) {
let exact = world.info.step?.match(/ should (?:not|NOT) (?:be|equal) ['"]/) ? true : false
if (role === 'metatag') await expect(await testMetatag(world.page, identifier, expected, exact)).toBe(false)
if (role === 'metatag') await testMetatag(world.page, identifier, expected, exact, false)
else {
let locator = await getLocator(world.page, identifier, role)
if (exact) await expect(locator).not.toHaveValue(expected)
Expand All @@ -247,11 +247,11 @@ Then('a/an/the (value of )(the ){string} {word} should not/NOT contain/include/b
// Metatags
Then('the meta( )tag {string} should contain/include/be/equal {string}', async function (world:PlaywrightWorld, name, expected) {
let exact = world.info.step?.match(/ should (?:be|equal) ['"]/) ? true : false
await expect(await testMetatag(world.page, name, expected, exact)).toBe(true)
await testMetatag(world.page, name, expected, exact)
})
Then('the meta( )tag {string} should not/NOT contain/include/be/equal {string}', async function (world:PlaywrightWorld, name, expected) {
let exact = world.info.step?.match(/ should (?:not|NOT) (?:be|equal) ['"]/) ? true : false
await expect(await testMetatag(world.page, name, expected, exact)).toBe(false)
await testMetatag(world.page, name, expected, exact, false)
})

// Visual regression testing
Expand Down

0 comments on commit 7fc2026

Please sign in to comment.