Assert an array reduces to true

2019-06-18 05: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?

2条回答
做自己的国王
2楼-- · 2019-06-18 05:30

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

查看更多
萌系小妹纸
3楼-- · 2019-06-18 05:36

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);
查看更多
登录 后发表回答