HomeSelenium

How can we set the ZOOM Levels [Page Level] in Selenium WebDriver.

How can we set the ZOOM Levels [Page Level] in Selenium WebDriver.

How can we set the ZOOM Levels, like zoom In & Zoom out [Page Level] in Selenium WebDriver.

Zoom In & Zoom Out we generally do on Mobile version but in real world if someone want to zoom in or out on desktop version how to implement the scenario we shall have a small walk through with implementation. With the Help of executeScript() method we can achieve the desired requirement.

What is executeScript() – In general the executeScript() method known as for executing a snippet of JavaScript with the context of selected frame or window. This script area will be executed as the body of identified method. TO store the return value we use the return keyword and give the variable name in the value input.

public class Demo {
	public static void main(String[] args) throws InterruptedException  {		
	   WebDriverManager.chromedriver().setup();
	   WebDriver driver = new ChromeDriver();
	   driver.manage().window().maximize();
	   driver.get("http://omayo.blogspot.com/");
	   JavascriptExecutor jse = (JavascriptExecutor)driver;
	   Thread.sleep(3000);
	   System.out.println("Zoom In with 200%");
           // Zoom In to 200%
	   jse.executeScript("document.body.style.zoom='200%'");
	   Thread.sleep(3000);
	   System.out.println("Zoom Out with 50%");
           // Zoom Out to 50%
	   jse.executeScript("document.body.style.zoom='50%'");
	   Thread.sleep(3000);
	  System.out.println("Zoom In to Original as 100%");
           // Zoom In to Original as 100%
	   jse.executeScript("document.body.style.zoom='100%'");	   
	   Thread.sleep(3000);
	   driver.close();
	}
}

The below are the screenshots of the different outputs we get while executing the above Selenium Automation code.

Output: Zoom In with 200%

Zoom Out with 50%

Zoom In to Original as 100%

Hope you like this article.

I will come up with another interesting article.

Please provide your valuable feedback in the below comments section.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *