【发布时间】:2015-05-21 22:45:48
【问题描述】:
这是失败的测试:
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();
});
});
其中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;
errors 应该是 在页面上找到的可见错误文本数组。
这是我们得到的错误:
Failures:
1) Checking errors should not show any errors
Message:
Expected [ ] to be empty array.
Stacktrace:
Error: Failed expectation
仅供参考,toBeEmptyArray() 匹配器来自jasmine-matchers 第三方。
我尝试用这种方式打印出scope.page.errors 的值:
scope.page.errors.then(function (errors) {
console.log(errors);
});
并打印为[]。 Array.isArray(errors) 返回true。
据我所知,scope.page.errors 是一个空数组,但期望失败。我错过了什么?
【问题讨论】:
-
如果记录 Array.isArray(errors) 会得到什么?
-
@Ryan 没想到会这样,但它会打印出
true..weird。谢谢。 -
expect(scope.page.errors.length).toBe(0);有效吗?您可能会在 webdriver 创建的范围(如页面中的 iframe)中遇到问题,这些范围具有自己的“数组”定义。请参阅stackoverflow.com/a/2265999/960524(所以这可能是toBeEmptyArray()方法中的一个错误——它可能未能通过检查的“数组”部分。) -
@P.T.谢谢你的观点,你发布的期望因
Expected undefined to be 0而失败。我实际上怀疑这与ElementArrayFinder..this particular comment 可能与我们正在经历的事情有关:You can treat an ElementArrayFinder as an array of WebElements for most purposes... -
expect(scope.page.errors).toBeArray();通过了吗?
标签: javascript testing jasmine protractor jasmine-matchers