简体   繁体   English

复选框选中断言不通过

[英]Check-box checked assertion does not pass

My scenario is, that i want to make a page object for check-boxes in general.我的情况是,我想为复选框创建一个页面对象。 With this page object, i can see if any desired check-box (at the moment in the given example is the 'Display Category' check-box), is checked, and if not, to check the same and afterwards assert that it is indeed checked.使用此页面对象,我可以查看是否选中了任何所需的复选框(此时在给定示例中是“显示类别”复选框),如果没有,则检查相同并随后断言它是确实检查过。

So in my page object file i have the following:所以在我的页面对象文件中,我有以下内容:

/* Class where the check-box will be marked - using the below class Page, together with this class Feature, using a method given in the the Test-code (at the end), i am trying first to click on the label (feature.label), thus checking the check-box, and then assert to see if the check-box is indeed marked as checked ( .expect(feature.checkbox.checked).ok(); ) /* 复选框将被标记的类 - 使用下面的类 Page 和这个类 Feature,使用测试代码中给出的方法(最后),我首先尝试点击标签( 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();
}

// Either way i execute, i get the following report: // 无论我执行哪种方式,我都会得到以下报告:

AssertionError: expected false to be truthy断言错误:预期假为真

If you use a non-standard element with a specific attribute, you should use the hasAttribute function to check that some attribute exists on the element.如果您使用具有特定属性的非标准元素,则应使用hasAttribute函数来检查元素上是否存在某些属性。

I created an example on github, which illustrates my solution.我在 github 上创建了一个示例,它说明了我的解决方案。 Please modify my example to reproduce the issue, if it does not help you.如果对您没有帮助,请修改我的示例以重现该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM