From 7fc2026a860de276d7602c2c16012486c1e279f7 Mon Sep 17 00:00:00 2001 From: David Hunt Date: Wed, 30 Oct 2024 06:48:04 +1300 Subject: [PATCH] fix(playwright): provide better errors for metatag tests --- .changeset/wet-ears-perform.md | 5 +++++ packages/playwright/src/helpers.ts | 10 ++++++++-- packages/playwright/src/outcomes.steps.ts | 8 ++++---- 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 .changeset/wet-ears-perform.md diff --git a/.changeset/wet-ears-perform.md b/.changeset/wet-ears-perform.md new file mode 100644 index 0000000..505dc14 --- /dev/null +++ b/.changeset/wet-ears-perform.md @@ -0,0 +1,5 @@ +--- +"@quickpickle/playwright": patch +--- + +provide better error messages for metatag tests diff --git a/packages/playwright/src/helpers.ts b/packages/playwright/src/helpers.ts index 8d4d066..d8f9efd 100644 --- a/packages/playwright/src/helpers.ts +++ b/packages/playwright/src/helpers.ts @@ -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) { diff --git a/packages/playwright/src/outcomes.steps.ts b/packages/playwright/src/outcomes.steps.ts index fd9b9db..bb9e951 100644 --- a/packages/playwright/src/outcomes.steps.ts +++ b/packages/playwright/src/outcomes.steps.ts @@ -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) @@ -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) @@ -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