在更高的层面上,我想
- 转到某个页面,然后使用某个定位器(div等),在这个定位器中拉出
a
个标签的所有href
个链接. - 然后我想要单独转到每个链接,看看它是否有效,或者是一个断开的链接,这意味着判断状态代码是否为200.
- 这里需要注意的是,第二个点应该并行运行,否则以串行方式判断每个链接将非常慢.
这是我目前拥有的代码(我已经删除了一些敏感信息,如URL等).
test.describe(`@Header Tests`, () => {
test.beforeEach(async ({ page, context }) => {
await page.goto(".....some url.....");
});
test(`@smoke Validate all header links give a status of 200`, async ({
page,
}) => {
const elements = page.locator("section#mega-nav a");
const links = await elements.evaluateAll<string[], HTMLAnchorElement>(
(itemTexts) =>
itemTexts
.map((item) => item.href)
.filter((href) => href !== "" && !href.includes("javascript:"))
);
// visit each link
for (const link of links) {
test(`Check status code for ${link}`, async () => {
// Visit the link
const response = await page.goto(link, {
waitUntil: "domcontentloaded",
});
// Check if the response status code is 200
expect(response?.status()).toBe(200);
});
}
});
});
但当我运行此命令时,我收到以下错误
Error: Playwright Test did not expect test() to be called here.
Most common reasons include:
- You are calling test() in a configuration file.
- You are calling test() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
when one of the dependencies in your package.json depends on @playwright/test.
在剧作家身上能做到这一点吗?也就是说,首先获取页面div等上的所有链接,然后并行地转到每个链接以判断它们的状态代码?