Sai Vijay
Sai Vijay

Reputation: 21

asynchronous for loop with if conitions protractor

    var createTime=10:00;
    var createdPatient="vijay";
    /* In for loop after comparing the time with createdTime 'i' value should be the compare index but 'i' value becoming last index each time*/ 
    element(by.xpath("//div[@id='patients']")).all(by.xpath("div[@class='ng-scope']")).then(
function(divtags){
for(i=0;i<divtags.length;i++){
divtags[i].element(by.xpath("div[1]/div[2]")).getText().then(function(time){
if(time==createdTime){
 /*Here i am getting 'i' value every time last divtag length */        divtags[i].element(by.xpath("div[@class='patient']")).getText().then(function(patientName){
if(patientName==createdPatient){
divtags[i].click();
}})}});}});

Please tell me how to get the correct index 'i' value after comparing the time with created time in protractor for loop code.

Upvotes: 0

Views: 85

Answers (1)

alecxe
alecxe

Reputation: 473833

No, this is not how you would solve things like this in Protractor. You need a filter() to filter the desired "patient" by time and name.

Here is the code with some additional improvements:

$$("#patients > div").filter(function (patient) {
    return patient.element(by.xpath("div[1]/div[2]")).getText().then(function (time) {
        return patient.$(".patient").getText().then(function (patientName) {
            return time === createdTime && patientName == createdPatient;
        });
    });
}).first().click();

Cannot guarantee it would work as is, since I don't have a way to reproduce and test it, but I hope you get the idea and can tweak the proposed solution properly.

Upvotes: 3

Related Questions