【问题标题】:Unable to click pagination of grid to verify certain grid values无法单击网格的分页来验证某些网格值
【发布时间】:2018-08-20 13:32:50
【问题描述】:

我正在使用以下方法来验证表格所有页面中的某些表格数据。我已经尝试了所有的可能性,谁能分享你的想法。

下面的代码是我的页面,

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

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

我在我的规范中这样称呼它,

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

我正面临以下问题,

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

但我可以在控制台中获得与 3 完全匹配的页码。

请帮忙

【问题讨论】:

    标签: javascript protractor


    【解决方案1】:

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

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

    希望这会有所帮助。

    【讨论】:

    • 感谢 Sudharsan,但我仍然面临一些问题。我正在使用 this.checkApplication = function (text) { return element.all(by.cssContainingText("#application-auth-list-2 tbody tr td:first-child", text)); }; ,它以前工作正常,但在这种方法中,它应该返回 false,因为我给出了错误的名称,但它返回 true。我想知道它是怎么发生的
    • 谢谢,我已经改变了 if 的条件,现在它可以正常工作了
    猜你喜欢
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    相关资源
    最近更新 更多