2014-09-13 10 views
5

Sto tentando di affermare che un nome viene visualizzato in una colonna di una tabella. Ho scritto una funzione inResults in grado di scorrere il testo di una colonna per vedere se esiste un nome. Ecco quello che sto cercando: oggettoCome verificare se il testo si trova nella colonna in Goniometro

Pagina:

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 aspettarsi:

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

Entrambe le affermazioni Print Console come previsto ... ma la mia aspettarsi fallisce come foundit 's valore è ancora ''. Ho provato questo un numero di modi e nessuno sta funzionando. Cosa mi manca?

+0

problema principale è Il comportamento asincrono di javascript, la funzione restituisce il valore prima che esegua il ciclo for. –

risposta

3

ti consiglierei di utilizzare il filtro: 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

Bella soluzione! Grazie Signore. – Brine

2

Utilizza mappa per fare this.This restituirà un differito che risolverà con i valori in un array, quindi se avete questo:

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

Si risolverà in questo modo:

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; 
    }); 
} 

e l'uso che in Spec file come,

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

'map()' non è necessario, 'getText()' può essere chiamato sul risultato di 'element.all()' direttamente - produrrebbe una promessa risolvendosi in una serie di testi. – alecxe

6

Ho ideato quello che penso sia un modo migliore/più pulito per risolvere questo problema. È meno complesso e non richiede il codice locator/css nel metodo.

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(); 

ho aggiunto questo al mio protractor_example project on GitHub ...

Problemi correlati