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?
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.
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.
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.
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.
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;");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With