Earlier inward previous post, We learnt how to scroll downward or up In webdriver browser Instance using javascript executor. Sometimes you lot every bit good necessitate to verify presence scroll bar on page. WebDriver create non convey whatsoever built In method using which you lot tin lavatory check the acquaint of scroll bar. But yes, You tin lavatory create It past times executing javascript. So nosotros tin lavatory purpose javascript executor In our webdriver examination to verify If horizontal or vertical scrollbar Is acquaint or non on page.
Check presence of horizontal scroll on page
To banking enterprise tally horizontal scroll on page, You tin lavatory purpose bellow given syntax In your examination script. It volition provide truthful If scroll Is acquaint else It volition provide 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 tin lavatory purpose bellow given syntax to banking enterprise tally presence of vertical scroll bar.
JavascriptExecutor javascript = (JavascriptExecutor) driver; Boolean VertscrollStatus = (Boolean) javascript.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight;");
Full webdriver examination example to banking enterprise tally horizontal as well as vertical scroll on page Is every bit bellow. To re-size window as well as acquire scrollbar on page, I convey used window().setSize() method. You tin lavatory read to a greater extent than usage item on window().setSize() method on THIS PAGE.
Run bellow given example In your eclipse as well as verify number In console. It volition banking enterprise tally as well as impress scroll bar condition In console every bit per Its availability on unlike 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; populace shape ScrollPresence { WebDriver driver; @BeforeTest populace void setup() throws Exception { driver = novel FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://google.com/"); } @Test populace void getScrollStatus() throws Exception { //Initially no scroll acquaint on page. //Check as well as impress horizontal as well as vertical scroll status. checkAndPrintScrollStatus(); Thread.sleep(2000); //resize window to acquire horizontal scroll on page. driver.manage().window().setSize(new Dimension(400,768)); //Check as well as impress horizontal as well as vertical scroll status. checkAndPrintScrollStatus(); Thread.sleep(2000); //resize window to add together vertical scroll on page. driver.manage().window().setSize(new Dimension(400,400)); //Check as well as impress horizontal as well as vertical scroll status. checkAndPrintScrollStatus(); Thread.sleep(2000); //resize window to take away horizontal scroll from page. driver.manage().window().setSize(new Dimension(1024,400)); //Check as well as impress horizontal as well as vertical scroll status. checkAndPrintScrollStatus(); } populace void checkAndPrintScrollStatus(){ JavascriptExecutor javascript = (JavascriptExecutor) driver; //Check If horizontal scroll Is acquaint or not. Boolean b1 = (Boolean) javascript.executeScript("return document.documentElement.scrollWidth>document.documentElement.clientWidth;"); //Check If vertical scroll Is acquaint or not. Boolean b2 = (Boolean) javascript.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight;"); if (b1 == truthful && b2 == true) { System.out.println("Horizontal as well as vertical Scrollbar is acquaint on page."); } else if (b1 == faux && b2 == true) { System.out.println("Horizontal Scrollbar non acquaint on page."); System.out.println("Vertical Scrollbar is acquaint on page."); }else if (b1 == truthful && b2 == false) { System.out.println("Horizontal Scrollbar Is acquaint on page."); System.out.println("Vertical Scrollbar non acquaint on page."); }else if (b1 == faux && b2 == false) { System.out.println("Horizontal as well as Vertical Scrollbar non acquaint on page."); } System.out.println("<----------x--------x--------->"); } }
This agency you lot tin lavatory verify scroll bar presence on page.
http://www.software-testing-tutorials-automation.com/