3

如何让量角器在桌子上向下滚动?我的表进行无限滚动 - 它加载 20 条记录,当显示倒数第二行时,它会获取接下来的 20 条记录。并非所有记录都在视图中...有些在下方尚未滚动到,有些在用户滚动过去时在上方。我在想测试是

it('should fetch next set of records on scroll') {
    element.all(by.id('users')).map(function (elm) {
        return elm;
    }).then(function (users) {
        expect(users.length).toBe(20);
    });

    // Scroll the table to the bottom to trigger fetching more records

    element.all(by.id('users')).map(function (elm) {
        return elm;
    }).then(function (users) {
        expect(users.length).toBe(40);
    });
};

这是这样做的正确方法吗?

HTML表格代码:

<div ng-if="users && users.length > 0" class="table-scroll" ng-infinite-scroll="loadMoreUsers()">
    <table id="users-table" class="table table-hover text-overflow-ellipsis">
        <thead>
            <td class="col1"></td>
            <td id="users-table-name-col" class="col2">User</td>
            <td id="users-table-date-col" class="col3">Birthday</td>
        </thead>
        <tbody ng-repeat="group in users">
            <tr ng-repeat="user in group.users" ng-click="userClicked(user);">
                <td class="col1">
                    <img class="col-xs-1 profile-picture" style="padding:0" ng-src="{{user.profile_picture}}"></td>
                <td class="col2">
                    <div id="user-name"> {{ user.last_name }}, {{ user.first_name }} </div>
                </td>
                <td class="col3">
                    <div id="user-date"> {{user.date}} </div>
                </td>
            </tr>
         </tbody>
     </table>
</div>
4

1 回答 1

4

这个想法是在表格(tr标签)中找到最新的元素并通过将父元素设置scrollTop为最后一个元素来滚动到它offsetTop

Element.scrollTop属性获取或设置元素内容向上滚动的像素数。元素的 scrollTop 是元素顶部到其最顶部可见内容的距离的度量。

HTMLElement.offsetTop只读属性返回当前元素相对于 offsetParent 节点顶部的距离。

var div = element(by.css('div.table-scroll'));
var lastRow = element(by.css('table#myid tr:last-of-type'));

browser.executeScript("return arguments[0].offsetTop;", lastRow.getWebElement()).then(function (offset) {
    browser.executeScript('arguments[0].scrollTop = arguments[1];', div.getWebElement(), offset).then(function() {
        // assertions

    });
});

另见(使用类似的解决方案):

于 2015-01-06T15:00:04.843 回答