How to check if text is found in a column in Protractor

I am trying to claim that the name is displayed in a table column. I wrote an inResults function that will inResults over the text of a column to find out if a name exists. Here is what I am trying:

Page Object:

 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\ TRUE!!!!'); // this gets printed... foundit = true; } }); }); return foundit; // returns '' but should be true? }; 

Expect:

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

Both console statements print as expected ... but my wait does not work as the foundit value is still. '' I tried this in several ways and no one works. What am I missing?

+5
source share
3 answers

I would recommend you use a filter: http://angular.imtqy.com/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); 
+3
source

I developed what, in my opinion, is the best / cleanest way to solve this problem. It is less complex and does not require locator / css code in the method.

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

I added this to my protractor_example project on GitHub ...

+6
source

Use this card to do this. This will return a pending one, which will be resolved with the values ​​in the array, so if you have this:

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

It will be resolved as follows:

 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\ TRUE!!!!'); // this gets printed... foundit = true; } } return foundit; }); } 

And use this in the Spec file, for example,

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

Source: https://habr.com/ru/post/1202505/


All Articles