量角器:如何在ui-grid中滚动几次

时间:2016-08-02 19:42:31

标签: javascript protractor infinite-scroll

我正在尝试编写一个滚动到表中后一行的函数。 ui-grid在行中无限滚动。

我找到了解释滚动到最后一个元素的答案,但是我的表在一次滚动之后有许多隐藏元素,我需要一个解决方案来滚动整个网格。 (Protractor: Scrolling a table and testing for infinite scroll

解决方案:How to getText from all rows & columns in an infinite scrolling ng-grid using protractor - 没有帮助我

我的网格是:



this.tableContent = function tableContent() {      

    //for loop
    var start = {};
    var distance = {};
    var lastRow = {};     
    var div = element(by.css('div.ui-grid-viewport'));
    lastRow.elm = element.all(by.css('div.ui-grid-row')).last();

    browser.executeScript("return arguments[0].scrollTop;", lastRow.elm.getWebElement()).then(function (dst) {
        return distance.value = dst;       
    }).then(function () { 


        while (start.value !== distance.value) {
            //console.log('The distance before scrolling ' + dst);


            start.value = distance.value;

            //scroll
            browser.executeScript("arguments[0].scrollIntoView();", lastRow.elm.getWebElement());
            browser.wait(protractor.ExpectedConditions.presenceOf(element.all(by.css('div.ui-grid-row')).last()), 1000);

            lastRow.elm = element.all(by.css('div.ui-grid-row')).last();


            browser.executeScript("return arguments[0].scrollTop;", div.getWebElement()).then(function (dst) {

                //console.log('the distance after scroll is ' + dst)
                return distance.value = dst;

            }).then(function () {

                console.log('the distance is ' + distance.value);
                console.log('The start.value is ' + start.value);

            });


        }


    });




根据案例,DOM中可见24行,隐藏65行。

我的算法是"滚动"顶部元素和"最后一个"之间的滚动距离元素不会被更改(并收集所有必要的文本),但WHILE主体不能正常工作,它只滚动一次:

<iframe src="https://www.weddingwire.com/morgancamille" style="border:0px #FFFFFF none;" name="weddingwirersvp" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="600px" width="600px"></iframe>

非常感谢任何帮助或想法,

提前感谢。

2 个答案:

答案 0 :(得分:1)

行, 经过长时间的研究,我得到了答案。问题是承诺(通常:))。感谢您撰写本文(While loop using bluebird promises),我的解决方案如下:

     var promiseWhile = Promise.method(function (condition, action) {
        if (!condition()) return;
        return action().then(promiseWhile.bind(null, condition, action));
    });

    lastRow.elm = element.all(by.css('div.ui-grid-row')).last();


    browser.executeScript("return arguments[0].scrollTop;", lastRow.elm.getWebElement()).then(function (dst) {
        return distance.value = dst;

    })
    .then(function () {


        promiseWhile(function () {

            // Condition for stopping
            return start.value !== distance.value;

        }, function () {
                start.value = distance.value;

                return collectData().then(function () { 

                    //scroll
                    browser.executeScript("arguments[0].scrollIntoView();", lastRow.elm.getWebElement());
                    browser.wait(protractor.ExpectedConditions.presenceOf(element.all(by.css('div.ui-grid-row')).last()), 1000);

                    lastRow.elm = element.all(by.css('div.ui-grid-row')).last();
                    browser.executeScript("return arguments[0].scrollTop;", div.getWebElement()).then(function (dst) {
                        distance.value = dst;
                    });

                });

            });
        });




    var collectData = function collectData() { 

        // HERE COLLECT YOUR DATA and don't forget return result!
     }

答案 1 :(得分:0)

例如,您正在寻找表格中的元素:

var UIGridCanvasManipulations = function () { 

this.isTheElementPresent = function(elementName) {
    var start = { value: -1 };
    var distance = {};
    var lastRow = {};
    var flag = { value: false };

    var div = element(by.css('div.ui-grid-viewport'));



    var promiseFor = Promise.method(function (condition, action, value) {
        if (!condition(value)) return value;
        return action(value).then(promiseFor.bind(null, condition, action));
    });
   //SHOULD check if the table has data (or rows) and then look for an element. For that purpose the wait function is used sinde sometimes it take a time to load a table
    var res = browser.wait(protractor.ExpectedConditions.visibilityOf(element.all(by.css('div.ui-grid-row')).last())).then(function (present) {

        if (present) {


            lastRow.elm = element.all(by.css('div.ui-grid-row')).last();

            var result = browser.executeScript("return arguments[0].scrollTop;", lastRow.elm.getWebElement()).then(function (dst) {

                return distance.value = dst;

            }).then(function () {

                promiseFor(function (count) {
                    return (flag.value != true && start.value !== distance.value);
                }, function (count) {



                    return lookForElement()
                           .then(function (res) {
                        //scroll
                        browser.executeScript("arguments[0].scrollIntoView();", lastRow.elm.getWebElement());
                        browser.wait(protractor.ExpectedConditions.presenceOf(element.all(by.css('div.ui-grid-row')).last()));

                        lastRow.elm = element.all(by.css('div.ui-grid-row')).last();


                        browser.executeScript("return arguments[0].scrollTop;", div.getWebElement()).then(function (dst) {
                            distance.value = dst;
                        });


                        return start.value = distance.value;
                    })

                    return flag.value;

                }, 0)


            }).then(function () {

                return flag.value;

            });


            return result;

        } else {
            return false;
        }

    }, function (err) {

        return false;

    });

    return res;




    lookForElement: function {
    // HERE is YOUR CODE 
   };
 };

 };module.exports = UIGridCanvasManipulations;

现在,如果您想使用它,请执行以下操作:

 var tableManipilation = require(UIGridCanvasManipulations);
 var tellMe= new tableManipilation();
 tellMe.isTheElementPresent(sElementTxt);

希望很明显:)