Using selenium with java, I need to test a \"Back to top\" button, so what I did is to scroll page down until \"Back to top\" button is shown (as it is shown when scrolled 25% o
The general principle is to check the value of window.pageYOffset in the browser. If your button scrolls completely back to the top then window.pageYOffset should have a value of 0. Assuming the driver variable holds your WebDriver instance:
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");
You can then check that value is 0. executeScript is used to run JavaScript code in the browser.
This answer initially mentioned scrollY but there's no support for it on IE. The MDN page on it, says:
For cross-browser compatibility, use
window.pageYOffsetinstead ofwindow.scrollY. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties. A fully compatible example:var supportPageOffset = window.pageXOffset !== undefined; var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft; var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
Thanks to R. Oosterholt for the "heads up".