javascript - 复选框检查断言不通过
问题描述
我的情况是,我想为复选框制作一个页面对象。使用此页面对象,我可以查看是否选中了任何所需的复选框(目前在给定示例中是“显示类别”复选框),如果没有,则检查相同的复选框,然后断言它是确实检查过。
所以在我的页面对象文件中,我有以下内容:
/* 复选框将被标记的类 - 使用下面的类页面,连同这个类功能,使用测试代码中给出的方法(最后),我首先尝试点击标签( 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 为真
解决方案
如果您使用具有特定属性的非标准元素,则应使用该hasAttribute
函数检查元素上是否存在某些属性。
我在 github 上创建了一个示例,说明了我的解决方案。如果对您没有帮助,请修改我的示例以重现该问题。
推荐阅读
- javascript - 如何过滤不同键值的 json 数据并使用该键值创建新的 json 元素,而无需硬编码数据
- python - Pandas 数据框行到列表的字典,使用每行的第一个值作为键
- java - 如何在片段中使用 ViewModel?
- c++ - 寻找洞察为什么我在这里遇到访问冲突
- ios - 将数据从 json 模型传输到数组
- qt - 如何将 QTextToSpeech(任何 Qt 类)导出到 QML?
- azure - Cosmos DB:检索 N 多条记录时使用 MaxItemCount 和 Take() 之间的区别
- r - 具有奇怪行为的 array_reshape()
- amazon-web-services - 命令“pipeline_resource_s3”不在文件中工作,但在终端中工作
- c - 如何使用循环显示带有模式的整数?