I want to test meta tags of the web application in Playwright, but I haven't found any info in the official documentation. Can somebody advise me with meta data testing?
For example, I have tried to test:
<meta name="description" content="something">
test("Meta data", async ({ page }) => {
await page.goto(env.baseUrl)
const metaDescription = page.locator('meta [name="description"]')
await expect(metaDescription).toHaveText("something")
})
and the result is:
What is wrong and how to test it in the right way?
Your locator selector is matching nothing. meta [name="description"]
targets elements inside a meta
element with the attribute name="description"
. To target the meta element itself you have to use meta[name="description"]
(note the removed space).
Additionally, the meta element holds the text in the content
attribute. That's why toHaveText
can't work. Try toHaveAttribute
instead.
Here's the fixed version:
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }) => {
await page.goto('https://your-site.com');
const metaDescription = page.locator('meta[name="description"]');
await expect(metaDescription).toHaveAttribute('content', 'something')
});