Search code examples
typescriptgulp-protractor

Expect Array of true values to be true


I have a function which is checking to see if all the elements which are supposed to be on a page are in fact on the page.

 /**
 * Elements are Displayed
 */
elementsDisplayed() {

    let elements = [
        this.emailTextField,
        this.pwordTextField,
        this.signInBtn,
        this.forgotLink,
    ];

    let elementsDisplayed = elements.map(function(element) {
        return element.isDisplayed();
    });

    let bundledPromise =  protractor.promise.all(elementsDisplayed)
    return bundledPromise
}

When I call this function it is returning an array of four true values. Right now I am calling this function and expecting it .toEqual([true, true, true, true])

Is there a way to either verify that each item in the array is .toBe(true)? I have tried the following and it fails. The function I have to check it is:

describe('Login Tests', () => {

it('Validate Login Page Elements', () => {
    let elementsVisible = loginPage.elementsDisplayed();
    _.forEach(elementsVisible, function(value) {
        expect(value).toBe(true);
    })
  });
});

The output is checking way more than 4 items; it is a whole bunch of expected null to be true, expected undefined to be true, expected 'pending' to be true, etc.

What am I doing wrong here?


Solution

  • So here is how we fixed it, we changed the elementsDisplayed method to look like this:

     /**
     * Elements are Displayed
     */
    elementsDisplayed() {
    
        let elements = [
            this.emailTextField,
            this.pwordTextField,
            this.signInBtn,
            this.forgotLink,
        ];
    
        let elementsDisplayed = elements.map(function(element) {
            return element.isDisplayed();
        });
    
        return protractor.promise.all(elementsDisplayed).then(result => {
            console.log(result);
            return _.every(result)
        });
    }