'Protractor - How do I return an element by the text contained within a child element?

was wondering if I could get some help on this. I have an array of <tr> elements returned when I declare this webelement reference

locationAuditsBySourceListViewTableRows : { //returns an array
    get : function(){
        return this.locationAuditsBySourceListViewTable.all(by.repeater('audit in location.displayedProfileAudits'));
    }
}, 

Within one of those <tr> elements is a span that contains the text that specifies the row I would like to select.

The current framework I am working within has some similar methods, which I tried to refactor for my purposes, but this code is failing:

locationAuditsListViewRowBySource : {
    value : function(sourceName){
        return this.locationAuditsBySourceListViewTableRows.filter(elem => { 
            //elem - placeholder as we iterate  through each element within the locationAuditsBySourceListViewTableRows array           
            return elem.element(by.css(".source-name.ng-binding")).getText().then((val) => {
                return val.toLowerCase() == sourceName.toLowerCase();
            }); 
        }).first();             
    }
},

My feeling is that the way in which I try to reference the span within the table row is incorrect (call is made within the .filter() method):

        return elem.element(by.css(".source-name.ng-binding")).getText().then((val) => {

Here is the step definition itself:

Then(/^I should see that location audit for source row: "([^"]*)" has a label for the source$/, function (sourceName, callback) {
    browser.wait(EC.visibilityOf(listingsPageObj.locationAuditsBySourceListViewContainer), timeouts.EC_TIMEOUT).then(() => {
        browser.wait(EC.visibilityOf(listingsPageObj.locationAuditsBySourceListViewTable), timeouts.EC_TIMEOUT).then(() => {
            browser.wait(() => {
                return listingsPageObj.locationAuditsBySourceListViewTableHeaders.count().then(cnt => (cnt > 0)); //which means that there are audit results displayed
            }).then(() => {
                //find a row in the list of displayed audits
                var tableRow = listingsPageObj.locationAuditsListViewRowBySource(sourceName);                                       
            });
        });
    });
});


Solution 1:[1]

The usage of elem.element(by.css(".source-name.ng-binding")).getText() is right.

But some small changes in code is better:

return val.trim().toLowerCase() == sourceName.trim().toLowerCase();

return listingsPageObj.locationAuditsBySourceListViewTableHeaders
          .count().then(cnt => {return cnt > 0;}) // I think you need return

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yong