Assert an array reduces to true

无人久伴 提交于 2019-12-23 08:56:19

问题


In one of the tests, we need to assert that one of the 3 elements is present. Currently we are doing it using the protractor.promise.all() and Array.reduce():

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

protractor.promise.all([
    title.isPresent(),
    summary.isPresent(),
    description.isPresent()
]).then(function (arrExists) {
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true);
});

Is there a better way to solve it with Jasmine without resolving the promises explicitly? Would we need a custom matcher or it is possible to solve with the built-in matchers?


回答1:


Check this:

let EC = protractor.ExpectedConditions;

let title = EC.visibilityOf($("#title")),
    summary = EC.visibilityOf($("#summary")),
    description = EC.visibilityOf($("#description"));

expect(EC.or(title, summary, description)() ).toBeTruthy('Title or summary or description should be visible on the page')

Notice that i am executing function that ExpectedCondition returns - so i am getting result of that function(Promise that will be resolved to boolean) instead of function.

You can use .presenceOf() if you need it instead .visibilityOf()

http://www.protractortest.org/#/api?view=ExpectedConditions.prototype.or




回答2:


You could simply get all the elements with a single selector and assert that the count is superior to zero:

var title_summary_description = element.all(by.css("#title, #summary, #description"));
expect(title_summary_description.count()).toBeGreaterThan(0);


来源:https://stackoverflow.com/questions/37800341/assert-an-array-reduces-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!