fuzzi
fuzzi

Reputation: 2277

How to check when a protractor.ElementFinder is null

I have an implementation similar to the following:

const arrayOfElements: protractor.ElementArrayFinder = $$('firstName');
returnElementMatchingCriteria.then( do something with the element..


public returnElementMatchingCriteria(criteria: string): Promise<protractor.ElementFinder> {
    arrayOfElements.each(element)
         if (element.text() === criteria) {
             return element

If there are no matching elements, then the function is evaluated as null. How can I check whether the returned value is null, before proceeding with the next line?

I have tried to use something similar to if (returnedValue === null) but a string literal cannot be applied to type ElementFinder.

Any help would be greatly appreciated.

Upvotes: 1

Views: 924

Answers (1)

alecxe
alecxe

Reputation: 473833

I think you meant to use filter() instead of each() and use count() to check the results:

var arrayOfElements = $$('firstName');
var result = arrayOfElements.filter(function (elm) {
    return elm.getText().then(function (text) { 
        return text === criteria;
    });
});
expect(result.count()).toBeGreaterThan(0);

// do something with result - e.g. result.first() to get the first matching element

In case there are no matching elements the result.count() would evaluate to 0.

Upvotes: 2

Related Questions