2014-09-13 14 views
5

Próbuję potwierdzić, że nazwa jest wyświetlana w kolumnie tabeli. Napisałem funkcję inResults, która będzie iterować tekst w kolumnie, aby sprawdzić, czy istnieje nazwa. Oto, co usiłuję: obiektJak sprawdzić, czy tekst znajduje się w kolumnie w Kątomierz

strony:

this.names = element.all(by.repeater('row in rows').column('{{row}}')); 

this.inResults = function(nameString) { 
    var foundit = ''; 
    this.names.each(function(name) { 
     name.getText().then(function(it) { 
      console.log(it); // each name IS printed... 
      if(it == nameString) { 
       console.log('it\'s TRUE!!!!'); // this gets printed... 

       foundit = true; 
      } 
     }); 
    }); 
    return foundit; // returns '' but should be true? 
}; 

Spec spodziewać:

expect(friendPage.inResults('Jo')).toBeTruthy(); 

Obydwa oświadczenia konsoli drukować zgodnie z oczekiwaniami ... ale mój oczekiwać nie jako foundit „s wartość jest nadal ''. Próbowałem tego na wiele sposobów i żaden z nich nie działa. czego mi brakuje?

+0

Główny problem polega na tym Zachowanie asynchroniczne javascript, funkcja zwraca wartość przed wykonaniem pętli for. –

Odpowiedz

3

polecam używanie filtru: http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.filter

this.inResults = function(nameString) {  
    return this.names.filter(function(name) { 
    return name.getText().then(function(text) {   
     return text === nameString; 
    }); 
    }).then(function(filteredElements) { 
    // Only the elements that passed the filter will be here. This is an array. 
    return filteredElements.length > 0; 
    }); 
}); 

// This will be a promise that resolves to a boolean. 
expect(friendPage.inResults('Jo')).toBe(true); 
+0

Ładne rozwiązanie! Dziękuję Panu. – Brine

2

Wykorzystanie map zrobić this.This zwróci odroczone że rozwiąże z wartościami w tablicy, więc jeśli masz to:

this.mappedVals =element.all(by.repeater('row in rows').column('{{row}}')).map(function (elm) { 
    return elm.getText(); 
}); 

Będzie to rozwiązać tak:

this.inResults = function(nameString) { 
    var foundit = ''; 
    mappedVals.then(function (textArr) { 
    // textArr will be an actual JS array of the text from each node in your repeater 
    for(var i=0; i<textArr.length; i++){ 
     if(it == textArr[i]) { 
      console.log('it\'s TRUE!!!!'); // this gets printed... 
      foundit = true; 
     } 
    } 
    return foundit; 
    }); 
} 

i użyć jej w Spec złożyć jak,

friendPage.inResults('Jo').then(function(findIt){ 
    expect(findIt).toBeTruthy(); 
}); 
+0

'map()' nie jest potrzebne, 'getText()' może zostać wywołane na wyniku 'element.all()' bezpośrednio - wytworzy obietnicę rozwiązującą się w tablicę tekstów. – alecxe

6

Opracowałem, jak sądzę, lepszy/czysty sposób rozwiązania tego problemu. Jest mniej skomplikowany i nie wymaga kodu lokalizatora/css w metodzie.

friend.page.js

// locator 
this.friendName = function(text) { return element.all(by.cssContainingText('td.ng-binding', text)) }; 

// method 
this.inResults = function(name) { 
    return this.friendName(name).then(function(found) { 
     return found.length > 0; 
    }); 
}; 

friend.spec.js

expect(friendPage.inResults('Jo')).toBeTruthy(); 

dodałem to do mojego protractor_example project on GitHub ...

Powiązane problemy