无法点击网格的分页来验证某些网格值

时间:2018-08-20 13:32:51

标签: javascript protractor

我正在使用以下方法在表格的所有页面中验证某些表格数据。我已经尝试了所有可能,任何人都可以分享您对此的想法。

下面的代码是我的页面,

this.isApplicationPresentUsingName = function (name) {

    return this.pgnumCount.count().then(function(num){
        if (num > 0) {
            console.log(num);
            for (var i = 0; i < num; i++) {
                return this.pgnumCount.filter(function(pg){
                    pg.getText().then(function(text){
                       return text.toString()===i.toString();
                   }).then(function(number){
                       number.click();
                }).then(function(){
                this.checkApplication(name).then(function (found) {
                    console.log("Text1");
                return found.length > 0;});
                });
            });
            }
        } else {
            console.log('Pagination not exists');
        }
    });
});
  

this.checkApplication = function(text){return element.all(by.cssContainingText(“#application-auth-list-2 tbody tr td:first-child”,文本)); };

     

this.pgnumCount = $$('a.ui-paginator-page');

我在以下规格中称呼它,

  

expect(appAuth.isApplicationPresentUsingName(applicationName))。toBeFalsy();

我正面临以下问题,

  

失败:无法读取未定义的属性“过滤器”

但是我可以在控制台中将页码设为3完全匹配。

请帮助

1 个答案:

答案 0 :(得分:0)

首先,我将解释如何执行您的代码。

this.isApplicationPresentUsingName = function (name) {

return this.pgnumCount.count().then(function (num) {

    //You will get the total number of pages

    if (num > 0) {
        console.log(num);
        for (var i = 0; i < num; i++) {
            //below statement will throw an error because 
            // `this` refers to current function scope.
            return this.pgnumCount.filter(function (pg) {
                // return statement is missing.
                // so the filter wont work
                pg.getText().then(function (text) {
                    // value of i will always equals to num
                    // because this code will be executed asynchronously
                    // you need to use `closure` to get correct value of i
                    return text.toString() === i.toString();
                }).then(function (number) {
                    number.click();
                }).then(function () {
                    this.checkApplication(name).then(function (found) {
                        console.log("Text1");
                        return found.length > 0;
                    });
                });
            });
        }
    } else {
        console.log('Pagination not exists');
    }
});
}

因此您的实际代码应类似于

this.isApplicationPresentUsingName = function (name) {
//save the reference of `this` to a local variable.
var self = this;

function recursiveSearch(currentPage, totalPage) {
    if (currentPage < totalPage) {
         //if current page is less total page
         // click the current page number
         self.pgnumCount.get(currentPage).click();
            // check if name is present in current page
            return self.checkApplication(name).then(function (found) {
                //if name is present in current page, return true.
                if (found) {
                    return true;
                } else {
                   //if name is not present in current page, 
                   // we need to again click next page and check if it is present
                    return recursiveSearch(index + 1, totalPage)
                }
            });
        }     else {
             //after checking for the name in all pages
             // return false because the name is not present in any of the page.
            return false;
        }
    }

    //get the total page number and recursively check if the name is present in each page.
    return this.pgnumCount.count().then(function (totalCount) {
        //start with page 0 till total number of pages    
        return recursiveSearch(0, totalCount);
    });

};

希望这会有所帮助。