Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Cypress是否可以测试控件是否原始?_Angular_Typescript_Cypress - Fatal编程技术网

Angular Cypress是否可以测试控件是否原始?

Angular Cypress是否可以测试控件是否原始?,angular,typescript,cypress,Angular,Typescript,Cypress,我正在开发Angular 4应用程序,我们正在使用Cypress进行前端/集成测试。 在其中一个表单上,我们有一个取消按钮,用于清除子表单上的所有输入并重置表单。 我想知道Cypress是否有办法检查表单或控件是否原始,或者是否需要检查输入的内容是否都已清除?(我知道如何进行后一种操作,但宁愿使用Pristine进行检查,也不需要遍历所有控件) 以下是我目前正在做的事情: cy.get('[data-test=cancelButton]').click(); cy.get('[data-test

我正在开发Angular 4应用程序,我们正在使用Cypress进行前端/集成测试。 在其中一个表单上,我们有一个取消按钮,用于清除子表单上的所有输入并重置表单。 我想知道Cypress是否有办法检查表单或控件是否原始,或者是否需要检查输入的内容是否都已清除?(我知道如何进行后一种操作,但宁愿使用Pristine进行检查,也不需要遍历所有控件)

以下是我目前正在做的事情:

cy.get('[data-test=cancelButton]').click();
cy.get('[data-test=referenceField').should('be.empty');
cy.get('[data-test=referenceField').should('have.attr', 'placeholder', 'Numbered list number');
我想做点像

cy.get('[data-test=cancelButton]').click();
cy.get('[data-test=referenceField').should('be.pristine');

Cypress允许您创建。以下是基本语法:

Cypress.Commands.add('shouldBePristine', {
    prevSubject: true // this allows it to be chained off another command
}, (subject /*, arg1, arg2, ...*/) => {
    // subject is whatever is wrapped by the Cypress object that
    // .shouldBePristine() is called on

    // In your case, you would do something like this:
    expect(subject).to.be.empty;
    expect(subject).to.have.attr('placeholder', 'Numbered list number');

    // This command does not need to change the subject for the next function
    // in the chain, so we will just return what was passed in
    return subject;
});
然后您可以这样调用您的命令:

cy.get('[data-test=referenceField]').shouldBePristine();

您也可以,但这个过程看起来要复杂得多。

我认为编写自定义命令是一个不错的选择,但是当我尝试执行
subject.should时(…
我得到了错误
cypress\u runner.js:141523 TypeError:subject.should不是函数
。以下是我在控制台检查
主题时的内容。
[input.ng-untouched.ng-pristine.ng-invalid,prevObject:jQuery.fn.init(1),上下文:文档,选择器:“[data test=createnumberlisterference]”]
@red_dorian Doh,这是我的错。
subject
是底层的包装对象,而不是Cypress包装器,因此您需要一个Chai断言。我通常会仔细检查我的答案,但这次我忘了。很抱歉!我已经编辑了我的答案。如果它仍然不适用于您,请告诉我。值得注意的是Cypress命令无法执行可以在自定义命令中调用。我刚刚在调试我的答案时发现了这一点。这很有帮助,并且为我指明了正确的方向。检查
.empty
和占位符适用于输入字段,但不是多输入类型或具有默认值的字段的通用解决方案。
expect(主题).to.have.value(subject[0].defaultValue)
适用于后者。但这是我的问题,而不是你的答案。我将你的答案标记为解决方案,并将使用我自己的功能,并将其作为单独的帖子包含在此处。感谢你的帮助。非常感谢!很高兴能提供帮助。