Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium, how do you check scroll position

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% of the page) and click it, this button takes user to the top of the page, now I need to check that it worked and the visible part is the top of the page. How can I do this using java?

like image 945
Yayo Avatar asked Sep 08 '14 22:09

Yayo


People also ask

How do you check scroll position using Selenium?

To check the position we shall use the Javascript executor. We have to verify the value of the window. pageYOffset in the browser. While the URL is launched, the scroll is at the top the value of window.

How do I know my scroll position?

The scrollbar position along a horizontal and vertical axis is denoted by integers. The pageXOffset property returns the number of pixels scrolled along the horizontal axis (i.e. left and right) and the pageYOffset property returns the number of pixels scrolled along the vertical axis (i.e. top and bottom).

How do you check the position of an element in Selenium?

We can use following two commands to verify the presence of an element: verifyElementPresent – returns TRUE if the specified element was FOUND in the page; FALSE if otherwise. verifyElementNotPresent – returns TRUE if the specified element was NOT FOUND anywhere in the page; FALSE if it is present.

How do you handle scrolling in Selenium?

Hence, to scroll up or down with Selenium, a JavaScriptExecutor is a must. The scrollBy() method involves two parameters, x, and y, that represent the horizontal and vertical pixel values, respectively.


2 Answers

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.pageYOffset instead of window.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".

like image 120
Louis Avatar answered Oct 05 '22 06:10

Louis


Louis' answer works, but is not fully cross-browser compatible, as Internet Explorer does not support window.scrollY. I recommend using window.pageYOffset instead - this returns the same value but is cross-browser compatible.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY

Here is the above code block with the modified code:

JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");

Also, syntax for Ruby (what I use for my current position, assuming as before that the driver instance is accessible through the variable name, 'driver'):

driver.execute_script('return window.pageYOffset;')
like image 32
John Theodore Avatar answered Oct 05 '22 06:10

John Theodore