首页 > 解决方案 > 如何验证带有断言的网页上不存在文本

问题描述

我是 Test Cafe Studio 的新手,我正在尝试弄清楚如何验证网页上不存在文本。如果文本存在,我希望测试失败。任何帮助,将不胜感激。

标签: testingautomationautomated-testse2e-testingtestcafe

解决方案


通常,您想要断言某个元素的文本:

const elText = await Selector('#my-element').textContent;
await t
    .expect(elText).notContains('some text');

如果你想检查整个页面,你可以这样做:

const elText = await Selector('html').textContent;
await t
    .expect(elText).notContains('some text');

但我认为最好避免它,而是以以下方式构建测试:

  • 你检查存在,而不是缺席
  • 你检查具体的东西,而不是整个页面

推荐阅读