量角器:向下滚动

xv4*_*v47 30 javascript testing selenium scroll protractor

当用户向下滚动时,我的页面上有一个按钮可见.因此,量角器测试给我一个错误:

UnknownError:未知错误:元素在点(94,188)处不可点击.

我试过用:

browser.executeScript('window.scrollTo(0,document.body.scrollHeight)');
Run Code Online (Sandbox Code Playgroud)

当我在量角器elementexplorer.js中进行测试时有效,但在我的常规测试中它没有做任何事情.还有其他方法吗?

nil*_*lsK 54

你需要等待承诺得到解决.以下示例来自一个未解决的问题

browser.executeScript('window.scrollTo(0,0);').then(function () {
    page.saveButton.click();
})
Run Code Online (Sandbox Code Playgroud)

更新:这是一个老问题(2014年5月),但仍然有一些访问者.澄清:window.scrollTo(0, 0)滚动到当前页面的左上角.

如果你想滚动到页面的底部,你可以打电话

window.scrollTo(0, document.body.scrollHeight)

正如@jsuser在这个答案中提到的那样

将采用更现代的方法

browser.actions().mouseMove(element).perform();

这个答案中赞成@MartinBlaustein


Mar*_*ein 31

我发现了一种更简单的方法 如果要滚动到可以使用的元素

    browser.actions().mouseMove(element).perform();
Run Code Online (Sandbox Code Playgroud)

之后浏览器将聚焦元素.


Joe*_*mme 13

我想补充上一个答案,希望能提供更多解释.

这段代码,在对'executeScript'的调用中:

'window.scrollTo(0,0);'
Run Code Online (Sandbox Code Playgroud)
  • 将窗口向上滚动到窗口坐标0,0 ...基本上到最顶部
  • 如果您知道需要去的特定区域,只需更改坐标即可.

如果你知道你想要在窗口的最底层,这是我的目标.您可以在'y'坐标中放入一个非常大的数字,就像我在这里做的那样:

browser.executeScript('window.scrollTo(0,10000);').then(function () {
    expect(<some control>.<some state>).toBe(<some outcome>);
})
Run Code Online (Sandbox Code Playgroud)


小智 9

如果其他人像我一样遇到麻烦:

我试图滚动到页面底部,以无限滚动方案加载我的下一组数据.我尝试了window.scrollTo的不同变体以及参数[0] .click()但没有成功.

最后我意识到,要让页面滚动,我必须通过单击窗口中的任何元素将焦点带到"窗口".然后window.scrollTo(0,document.body.scrollHeight)工作喜欢魅力!

示例代码:

element(by.className('<any element on page>')).click();
browser.executeScript('window.scrollTo(0,document.body.scrollHeight)').then(function(){
//whatever you need to check for here
});
Run Code Online (Sandbox Code Playgroud)


Eam*_*han 8

我发现创建一个util帮助器并在页面对象(或测试文件本身)中使用它有帮助.这似乎对我有用:

utils.js

module.exports = {
  scrollIntoView: function(el) {
    browser.executeScript(function(el) {
      el.scrollIntoView();
    }, el.getWebElement());
  }
}
Run Code Online (Sandbox Code Playgroud)

内部页面对象...

var scrollIntoView = require('../utils').scrollIntoView;

module.exports = {
  clickBackBtn: function() {
    var el = element(by.buttonText('Go back'));
    scrollIntoView(el);
    el.click();
    return;
  }
}
Run Code Online (Sandbox Code Playgroud)

在测试中......

it('should allow the user to go back to the profile page', function() {
  PageObject.clickBackBtn();
  expect(browser.getCurrentUrl()).toContain('/user/profile');
});
Run Code Online (Sandbox Code Playgroud)


Kev*_*ton 5

如果您只想导航到长页面的顶部或底部,您可以简单地使用“HOME”键转到顶部,或使用“END”键转到底部。

例如:

browser.actions().sendKeys(protractor.Key.HOME).perform();
Run Code Online (Sandbox Code Playgroud)

或者

browser.actions().sendKeys(protractor.Key.END).perform();
Run Code Online (Sandbox Code Playgroud)