带有剧作家的测试元标记

发布于 2025-02-07 12:26:54 字数 538 浏览 4 评论 0原文

我想在《剧作家》中测试Web应用程序的元标记,但是我在官方文档中没有找到任何信息。有人可以通过元数据测试向我建议吗?

例如,我试图测试: < meta name =“描述” content =“ sosity”>

test("Meta data", async ({ page }) => {
         await page.goto(env.baseUrl)
         const metaDescription = page.locator('meta [name="description"]')
         await expect(metaDescription).toHaveText("something")
})

,结果是:

  • 接收的字符串是“”
  • 等待选择器'meta [name =“ description”]''

是什么问题如何以正确的方式测试它?

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:

  • The received string is ""
  • waiting for selector 'meta [name="description"]'

What is wrong and how to test it in the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2025-02-14 12:26:54

您的定位器选择器什么都不匹配。 meta [name =“ description”]目标元素meta带有属性的元素name =“ description”。要定位元元素本身,您必须使用meta [name =“ description”](请注意删除的空间)。

此外,元元素将文本保存在content属性中。这就是为什么tohaveText无法正常工作的原因。尝试tohaveatTribute

这是固定版本:

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')
});

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')
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文