Earlier in previous post, We learnt how to scroll down or up In webdriver browser Instance using javascript executor. Sometimes you also need to verify presence scroll bar on page. WebDriver do not have any built In method using which you can check the present of scroll bar. But yes, You can do It by executing javascript. So we can use javascript executor In our webdriver test to verify If horizontal or vertical scrollbar Is present or not on page.
Check presence of horizontal scroll on page
To check horizontal scroll on page, You can use bellow given syntax In your test script. It will return true If scroll Is present else It will return false.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
Boolean horzscrollStatus = (Boolean) javascript.executeScript("return document.documentElement.scrollWidth>document.documentElement.clientWidth;");
Check presence of vertical scroll on page
Same way, You can use bellow given syntax to check presence of vertical scroll bar.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
Boolean VertscrollStatus = (Boolean) javascript.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight;");
Full webdriver test example to check horizontal and vertical scroll on page Is as bellow. To re-size window and get scrollbar on page, I have used window().setSize() method. You can read more usage detail on window().setSize() method on THIS PAGE.
Run bellow given example In your eclipse and verify result In console. It will check and print scroll bar status In console as per Its availability on different size of browser window.
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScrollPresence {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com/");
}
@Test
public void getScrollStatus() throws Exception {
//Initially no scroll present on page.
//Check and print horizontal and vertical scroll status.
checkAndPrintScrollStatus();
Thread.sleep(2000);
//resize window to get horizontal scroll on page.
driver.manage().window().setSize(new Dimension(400,768));
//Check and print horizontal and vertical scroll status.
checkAndPrintScrollStatus();
Thread.sleep(2000);
//resize window to add vertical scroll on page.
driver.manage().window().setSize(new Dimension(400,400));
//Check and print horizontal and vertical scroll status.
checkAndPrintScrollStatus();
Thread.sleep(2000);
//resize window to remove horizontal scroll from page.
driver.manage().window().setSize(new Dimension(1024,400));
//Check and print horizontal and vertical scroll status.
checkAndPrintScrollStatus();
}
public void checkAndPrintScrollStatus(){
JavascriptExecutor javascript = (JavascriptExecutor) driver;
//Check If horizontal scroll Is present or not.
Boolean b1 = (Boolean) javascript.executeScript("return document.documentElement.scrollWidth>document.documentElement.clientWidth;");
//Check If vertical scroll Is present or not.
Boolean b2 = (Boolean) javascript.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight;");
if (b1 == true && b2 == true) {
System.out.println("Horizontal and vertical Scrollbar is present on page.");
} else if (b1 == false && b2 == true) {
System.out.println("Horizontal Scrollbar not present on page.");
System.out.println("Vertical Scrollbar is present on page.");
}else if (b1 == true && b2 == false) {
System.out.println("Horizontal Scrollbar Is present on page.");
System.out.println("Vertical Scrollbar not present on page.");
}else if (b1 == false && b2 == false) {
System.out.println("Horizontal and Vertical Scrollbar not present on page.");
}
System.out.println("<----------x--------x--------->");
}
}
This way you can verify scroll bar presence on page.
Ah so essentially looks like we need to do this client-side.
ReplyDelete