首页 > 解决方案 > 复选框检查断言不通过

问题描述

我的情况是,我想为复选框制作一个页面对象。使用此页面对象,我可以查看是否选中了任何所需的复选框(目前在给定示例中是“显示类别”复选框),如果没有,则检查相同的复选框,然后断言它是确实检查过。

所以在我的页面对象文件中,我有以下内容:

/* 复选框将被标记的类 - 使用下面的类页面,连同这个类功能,使用测试代码中给出的方法(最后),我首先尝试点击标签( feature.label),从而检查复选框,然后断言以查看复选框是否确实标记为已选中 (.expect(feature.checkbox.checked).ok();)

// this will translate to:
// click.Selector('coral-checkbox-label').withText('Display Category);

// expect(Selector('coral-checkbox-label').withText('Display
// Category).parent(0).parent(0).checked).ok; // or

// expect(Selector('coral-checkbox-label').withText('Display
// Category).parent(0).parent(0).child(0).checked).ok

// I use either the parent or the child, because:
// 1. Parent is used because, when marking the check-box, and inspecting, the 'checked' element is // shown on the parent (coral-checkbox)
// 2. In a similar issue, the solution is to do the assertion if the check-box is checked, on the
// element which is of type . In my code, this translates to the above mentioned child(0).
https://stackoverflow.com/questions/54153903/testcafe-test-script-checkbox-checked-always-return-false-even-when-checked-how

    Helper.js // filename
_const label = Selector('coral-checkbox-label');

export class Feature {
constructor (text) {
this.label = label.withText(text);
this.checkbox = label.parent(0).parent(0) // .child(0)
// the reason .child(0) is commented will be mentioned in the following
}
}

// List of all desired check-boxes taken by label
export class Page {
constructor () {
this.featureList = [
new Feature('Display Category')
];
}
}_

 // Test code where check-boxes should be checked and asserted
import Page from Helper.js
let page = new Page();
for (const feature of page.featureList) {
console.log(feature.checkbox);
await t
.click(feature.label)
.wait(5000)
.expect(feature.checkbox.checked).ok();
}

// 无论我执行哪种方式,我都会得到以下报告:

AssertionError:预期 false 为真

标签: javascripttestingcheckboxautomated-teststestcafe

解决方案


如果您使用具有特定属性的非标准元素,则应使用该hasAttribute函数检查元素上是否存在某些属性。

我在 github 上创建了一个示例,说明了我的解决方案。如果对您没有帮助,请修改我的示例以重现该问题。


推荐阅读