[英]Failed expectation: “Expected [ ] to be empty array.”
Here is the failing test: 这是失败的测试:
describe("Checking errors", function () {
var scope = {};
beforeEach(function () {
browser.get("/#endpoint");
browser.waitForAngular();
scope.page = new MyPage();
});
it("should not show any errors", function () {
expect(scope.page.errors).toBeEmptyArray();
});
});
where MyPage
is a Page Object : 其中
MyPage
是页面对象 :
var MyPage = function () {
this.errors = element.all(by.css("div.error-block b.error"))
.filter(function (elm) {
return elm.isDisplayed().then(function (value) {
return value;
});
})
.map(function (elm) {
return elm.getText();
});
};
module.exports = MyPage;
where errors
supposed to be an array of visible error texts found on a page. errors
应该是在页面上找到的可见错误文本数组 。
Here is the error we are getting: 这是我们得到的错误:
Failures:
1) Checking errors should not show any errors
Message:
Expected [ ] to be empty array.
Stacktrace:
Error: Failed expectation
FYI, toBeEmptyArray()
matcher is coming from jasmine-matchers
third-party. 仅供参考,
toBeEmptyArray()
matcher来自jasmine-matchers
toBeEmptyArray()
第三方。
I've tried to print out the value of scope.page.errors
this way: 我试图用这种方式打印出
scope.page.errors
的值:
scope.page.errors.then(function (errors) {
console.log(errors);
});
And it is printed out as []
. 它以
[]
打印出来。 Array.isArray(errors)
returns true
. Array.isArray(errors)
返回true
。
From what I see, scope.page.errors
is an empty array, but the expectation fails. 从我看到,
scope.page.errors
是一个空数组,但期望失败。 What I a missing? 我错过了什么?
the answer is four lines down in the protractor src . 答案是量角器src中的四行。
ElementArrayFinder extends Promise
, while jasmine-matchers checks first checks that errors is an actual array exactly how Array.isArray is done , which will return false; ElementArrayFinder extends Promise
,而jasmine-matchers检查首先检查错误是否是Array.isArray完成的实际数组,这将返回false;
This is also consistent with expect(scope.page.errors.length).toBe(0)
being undefined because promises do not have lengths. 这也与
expect(scope.page.errors.length).toBe(0)
未定义一致,因为promises没有长度。
Just run errors.then
on your promise, and test that the argument is [] You've also shown that can be done when you ran scope.page.errors.then
只是运行
errors.then
你的承诺,并测试参数是[]你还表明,当你运行scope.page.errors.then
时可以完成
Inside test script your code line "scope.page = new MyPage();" is creating new empty MyPage object.Code which you have written in application script is creating page object locally and its not bounded with any angular scope.When there is requirement of testing such objects you need to replicate code for page object creation in beforeEach(); block of test script.And test it.
describe("Checking errors", function () {
var scope = {};
var MyPage ;
beforeEach(function () {
browser.get("/#endpoint");
browser.waitForAngular();
MyPage = function () {
this.errors = element.all(by.css("div.error-block b.error"))
.filter(function (elm) {
return elm.isDisplayed().then(function (value) {
return value;
});
})
.map(function (elm) {
return elm.getText();
});
};
});
it("should not show any errors", function () {
expect(MyPage.errors).toBeEmptyArray();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.