You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Angular TestHarness检测Mat Table单元格中的锚点链接

解决方案

你当前代码里用getProperty('a')的方式是错误的,getProperty用于获取元素本身的属性,不是查找子元素。正确的做法是通过单元格的宿主元素调用querySelector来定位内部的<a>标签,再断言其存在。

修改后的测试代码:

fit('should show link in weight column', async () => {
  const table = await loader.getHarness(MatTableHarness);
  const firstRow = (await table.getRows())[0];
  const cells = await firstRow.getCells({ columnName: 'weight' });
  const cellHost = await cells[0].host();
  
  // 在单元格内查找<a>标签
  const linkElement = await cellHost.querySelector('a');
  
  // 断言链接元素存在
  expect(linkElement).toBeTruthy();
});

补充说明:

  • 如果需要验证链接的具体属性(比如href值),可以在断言存在后继续获取属性:
    if (linkElement) {
      const linkHref = await linkElement.getProperty('href');
      expect(linkHref).toBe('目标链接地址');
    }
    

内容的提问来源于stack exchange,提问作者Arnab Mukherjee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:42:16