Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert an array reduces to true

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?

like image 885
alecxe Avatar asked Jun 13 '16 22:06

alecxe


People also ask

How do you check if all values in an array are true?

To check if all of the values in an array are equal to true , use the every() method to iterate over the array and compare each value to true , e.g. arr. every(value => value === true) . The every method will return true if the condition is met for all array elements.

How do you check if an array contains the same value?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.

How do you check if an array has all the same values JavaScript?

In order to check whether every value of your records/array is equal to each other or not, you can use this function. allEqual() function returns true if the all records of a collection are equal and false otherwise.

How do I see all the elements in an array?

The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements.


2 Answers

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);
like image 187
Florent B. Avatar answered Oct 20 '22 05:10

Florent B.


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

like image 20
Xotabu4 Avatar answered Oct 20 '22 04:10

Xotabu4