如何断言HTML元素是playwright中的有效链接?

校服杀人魔 测试交流2164字数 464阅读模式
摘要我试图在Playwright上做一些断言。我需要在链接列表中声明这一点<a>它们都具有href属性。然而,我不知道如何与Playwright实现这一点。...

我试图在Playwright身上做一些断言。我需要在链接列表中声明这一点 <a> 它们都具有该属性 href. 然而,我不知道如何通过Playwright的作用来实现这一点。

我的代码在这里:文章源自玩技e族-https://www.playezu.com/180257.html

test.only('then a list of 44 links is displayed', async({ page }) => {
const linkList = await page.locator('div#content > ul > li > a');
for(let i = 0; i < await linkList.count(); i++) {
expect(await linkList.nth(i)).toHaveAttribute('href', '');             }
await expect(linkList).toHaveCount(44);
})

到HaveAttribute() 函数需要2到3个参数,因为它获取键属性和属性值,但我只需要检查它是否具有href attr。文章源自玩技e族-https://www.playezu.com/180257.html

我怎样才能做到这一点?文章源自玩技e族-https://www.playezu.com/180257.html

这是正在测试的网站:
https://the-internet.herokuapp.com/文章源自玩技e族-https://www.playezu.com/180257.html 文章源自玩技e族-https://www.playezu.com/180257.html

玩技站长微信
添加好友自动发送入群邀请
weinxin
rainbow-shownow
玩技官方公众号
官方微信公众号
weinxin
PLAYEZU
 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/180257.html
    转载说明: 点我前往阅读>>>
评论  2  访客  2
    • Marcos Vinícius Franco
      Marcos Vinícius Franco 9

      在尝试了一些选项之后,我意识到我可以在使用定位器时断言if。getAttribute(’href’)不返回null,因此这是一个链接。
      所以我实现了:

      const linkList = await page.locator(‘div#content > ul > li > a’);
      for(let i = 0; i < await linkList.count(); i++) {
      expect(await linkList.nth(i).getAttribute(‘href’)).not.toBeNull();
      }

      • Marcos Vinícius Franco
        Marcos Vinícius Franco 9

        在尝试了一些选项之后,我意识到我可以在使用定位器时断言if。getAttribute(’href’)不返回null,因此这是一个链接。
        所以我实现了:

        const linkList = await page.locator(‘div#content > ul > li > a’);
        for(let i = 0; i < await linkList.count(); i++) {
        expect(await linkList.nth(i).getAttribute(‘href’)).not.toBeNull();
        }

      匿名

      发表评论

      匿名网友
      确定