Testcafe无法确定元素是启用还是禁用

时间:2019-02-20 14:51:31

标签: automated-tests e2e-testing web-testing testcafe

我正在使用TestCafe 0.23.3。我正在尝试验证元素是否已启用。这是元素被禁用时的HTML节点:

<button class="MuiButtonBase-root-415 MuiButtonBase-disabled-416 MuiButton-root-3719 MuiButton-text-3721 MuiButton-textPrimary-3722 MuiButton-flat-3724 MuiButton-flatPrimary-3725 MuiButton-disabled-3739" tabindex="-1" type="button" disabled=""><span class="MuiButton-label-3720">Add Person</span></button>

以下是启用该元素后的HTML节点:

<button class="MuiButtonBase-root-415 MuiButton-root-7365 MuiButton-text-7367 MuiButton-textPrimary-7368 MuiButton-flat-7370 MuiButton-flatPrimary-7371" tabindex="0" type="button"><span class="MuiButton-label-7366">Add Person</span><span class="MuiTouchRipple-root-778"></span></button>

这是我的TestCafe代码,用于验证元素:

.expect(Selector('button').withText('Add Person').hasAttribute('disabled'))
.ok();

上面的TestCafe代码通过了元素的启用/禁用状态,这是不正确的,因为预期结果是要检查元素是否被禁用。我不确定这是什么问题。

1 个答案:

答案 0 :(得分:7)

正如@lostlemon解释的那样,当存在多个匹配项时会出现这种情况。

要只有一个匹配项,请使用.withExactText('Add Person')或使用正则表达式而不是字符串文字。

您也可能有不匹配的不可见元素。 因此,Expect语句应这样重写:

const button = Selector('button')
  .with({visibilityCheck: true})
  .withExactText('Add Person');
await t
  .expect(button.hasAttribute('disabled')).ok();
相关问题