Abhinav
Abhinav

Reputation: 1027

Protractor: not able to get all elements using protractor

I am not able to get all elements using css or repeater.

this.clickRandomEmployee = function(){      
        employees = elementController.getAllElements('css','#employee-list li');
        numberOfEmployees = employees.length;
        console.log(numberOfEmployees); 
        var numRand = this.getRandomNumber(numberOfEmployees);
        console.log(numRand);
        elementController.doubleClick(employees.get(numRand));
    }

this.getAllElements = function(locatorType,value){
        var emps;       
        console.log(locatorType);
        console.log(value);
        if(locatorType=='css'){
            emps = element.all(by.css(value));
        }
        else if(locatorType=='repeater'){
            emps = element.all(by.repeater(value));
        }   
        return emps;
    };

Above code is called from test script to find all elements but it return undefined. Please suggest!!

Upvotes: 2

Views: 690

Answers (2)

alecxe
alecxe

Reputation: 473833

getAllElements returns a promise that would resolve into an array of elements. There is no length property on a promise. Instead, use count():

employees = elementController.getAllElements('css','#employee-list li');
employees.count().then(function (numberOfEmployees) {
    var numRand = this.getRandomNumber(numberOfEmployees);
    console.log(numRand);
    elementController.doubleClick(employees.get(numRand));
});

See also:

Upvotes: 0

TesterPhi
TesterPhi

Reputation: 346

Why dont you get rid of the getAllElements function and just use the simple lines of:

employees = element.all(by.css('#employee-list li'))

and

employees = element.all(by.repeater(value))

After you have done so, you should then probably use a then statement to make sure you are returning the value of the repeater before continuing.

employees = element.all(by.css('.items li')).then(function(returnedList) {
    numberOfEmployees = returnedList.length;
...
})

Upvotes: 1

Related Questions