Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting the presence of scrollbar using Selenium (webdriver java cucumber)

I have a responsive website I need to test. If the website goes to the window size of a Tablet, I want the test to check if there is a horizontal scrollbar. According to the design they can never be present on a tablet.

Does anyone have a piece of pseudo-code to assert the presence of a horizontal scrollbar using Selenium Webdriver Java Cucumber?

like image 699
user3356141 Avatar asked May 15 '14 12:05

user3356141


People also ask

How do you check a scroll bar is present or not in Selenium?

It will return true If scroll Is present else It will return false. Same way, You can use bellow given syntax to check presence of vertical scroll bar. JavascriptExecutor javascript = (JavascriptExecutor) driver; Boolean VertscrollStatus = (Boolean) javascript.

How does Selenium handle scrolling?

The JavaScriptExecutor provides an interface that enables QAs to run JavaScript methods from Selenium scripts. 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.

What is scroll bar in Selenium?

A Scrollbar is a lets you move around screen in horizontal or vertical direction if the current page scroll does not fit the visible area of the screen. It is used to move the window up and down. Selenium Webdriver does not require scroll to perform actions as it manipulates DOM.

How do you scroll down vertically in Selenium?

Selenium can execute JavaScript commands with the help of the executeScript method. To scroll down vertically in a page we have to use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.


1 Answers

You can test this with javascriptExecutor:

Vertical scrollbar:

boolean scrollBarPresent = ((JavascriptExecutor)driver).executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight;");

Horizontal scrollbar:

boolean scrollBarPresent = ((JavascriptExecutor)driver).executeScript("return document.documentElement.scrollWidth>document.documentElement.clientWidth;");
like image 157
Richard Avatar answered Oct 18 '22 00:10

Richard