我一直在用 Protractor 编写一套测试,快完成了。不过,我很难弄清楚如何做一些相当普遍的事情。我希望能够检查文本框是否包含一个值,如果是,则以失败退出测试用例。 (如果文本框包含一个值,我知道测试用例无论如何都不可能通过)
我目前正在尝试这样的事情:
tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
//attr contains the correct value in the textbox here but how do I return it to parent scope so I can exit the test case?
console.log("attr length is " + attr.length);
expect(attr.length).toBe(0);
},function(err){
console.log("An error was caught while evaluating Date of Birth text value: " + err);
});
expect 语句如我所料失败,但测试用例继续运行,这似乎是 expect 的预期行为。所以我尝试从“then” block 中返回真/假,但我似乎无法弄清楚如何将该值返回到父范围以做出决定。换句话说,如果我将上面的内容更改为:
var trueFalse = tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
if(attr === ""){
return true;
}else{
return false;
}
},function(err){
console.log("An error was caught while evaluating Date of Birth text value: " + err);
});
//This is the 'it' block's scope
if(trueFalse == true){
return;
}
我知道我对 promises 的经验不足可能是造成这个麻烦的原因。基本上,我只是想要一种说法,“如果某某文本框包含文本,则停止测试用例失败”。
谢谢,
最佳答案
这不是 protractor
问题,而是测试框架问题(jasmine
、mocha
等)。
不过,protractor
的问题跟踪器存在问题:
关闭时引用了 jasmine
问题跟踪器中的一个 Unresolved 问题:
虽然这没有实现,但有一个解决方法(最初提到 here ):
使用npm
安装模块后,当您希望测试失败并退出时,添加:
jasmine.getEnv().bailFast();
(不要忘记加载依赖项 require('jasmine-bail-fast');
)
另见:
关于angularjs - 如果元素包含值,如何在 Protractor 中退出测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27536706/