量角器-如何通过子元素中包含的文本返回元素?

时间:2018-10-12 21:13:12

标签: angularjs protractor automated-tests ui-automation qa

想知道我是否可以得到一些帮助。声明此网络元素引用时,我返回了<tr>个元素的数组

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

在这些<tr>元素之一内是一个范围,该范围包含指定我要选择的行的文本。

我正在使用的当前框架具有一些相似的方法,出于我的目的,我尝试对其进行重构,但是此代码失败了:

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

我的感觉是,我尝试引用表行中的跨度的方法不正确(在.filter()方法中进行了调用):

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

这是步骤定义本身:

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

有人能帮助我/指导我正确的方向吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用elem.element(by.css(".source-name.ng-binding")).getText()是正确的。

但是对代码进行一些小的更改会更好:

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

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