I have an button on my page that is visible when the user scrolls down. Because of this, protractor tests give me an error:
UnknownError: unknown erro
The answer of this question was marked correct at top replied.But after upgrade the newest chrome v54.The following code maybe best solution.
browser.actions().mouseMove(element).perform();
I'd like to add to the previous answer, and hopefully provide a little more explanation.
This code, in the call to 'executeScript':
'window.scrollTo(0,0);'
If you know you want to be at the very bottom of the window, which was my goal. You can put a very large number in the 'y' coordinate, like I did here:
browser.executeScript('window.scrollTo(0,10000);').then(function () {
expect(<some control>.<some state>).toBe(<some outcome>);
})
The easiest way is scroll till the expected element and click it. Please use the below tested and verified code in the current protractor version .
it('scroll to element', function() {
browser.driver.get('https://www.seleniumeasy.com/');
var btnSubscribe= element(by.id('mc-embedded-subscribe'));
browser.executeScript("arguments[0].scrollIntoView();", btnSubscribe);
browser.sleep(7500);
btnSubscribe.click();
});
Other way, you can try this:
this.setScrollPage = function (element) {
function execScroll() {
return browser.executeScript('arguments[0].scrollIntoView()',
element.getWebElement())
}
browser.wait(execScroll, 5000);
};
In case anyone else is having troubles like I was:
I was trying to scroll to the bottom of the page to load my next set of data in an infinite scroll scenario. I tried different variations of window.scrollTo as well as arguments[0].click() with no success.
Finally I realized, to get the page to scroll, I had to bring focus to the "window" by clicking on any element within the window. Then window.scrollTo(0, document.body.scrollHeight) worked liked a charm!
example code:
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
});
If you are looking just to navigate to the top or bottom of a long page you could simply use the 'HOME' key to go too the top, or 'END' key to go to the bottom.
e.g:
browser.actions().sendKeys(protractor.Key.HOME).perform();
or
browser.actions().sendKeys(protractor.Key.END).perform();