【问题标题】:Testcafe unable to determine if an element is enabled or disabledTestcafe 无法确定元素是启用还是禁用
【发布时间】:2019-07-14 07:19:42
【问题描述】:

我正在使用 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 代码通过了元素的启用/禁用状态,这是不正确的,因为预期的结果是检查元素是否被禁用。我不确定这里有什么问题。

【问题讨论】:

  • 页面上还有其他禁用的按钮吗?如果我对文档的理解正确,则选择器将同时匹配“按钮”和“带有添加人员的按钮”,然后确定这些匹配项中是否有任何匹配项具有 disabled 属性。请参阅第一个 withText 示例后的注释:devexpress.github.io/testcafe/documentation/test-api/… 此外,此 github 问题:github.com/DevExpress/testcafe/issues/1071
  • 我从withText 的文档中了解到,它会查找包含文本的元素(此处为Add Person)。它也可以传递文本Add Person People,但它不会仅查找与button 匹配的元素。我也尝试过withExactText,但两种情况下的测试都再次通过了。我仔细检查了页面中没有具有相同文本Add Person的元素。

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


【解决方案1】:

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

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

也有可能你有不可见的元素也匹配。 所以expect语句应该改写成这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2019-11-14
    • 2013-01-03
    • 1970-01-01
    • 2018-12-31
    • 2011-08-02
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多