How To Zoom In And Zoom Out In Selenium Webdriver
Posted By : Kuldeep Sidana | 23-Sep-2018
As we know Selenium automates the browsers. Many times when we running scripts in selenium, there is a need to handle the coordinates of the browser and requires zoom in and zoom out. If we talk about manually, we could do that by pressing Ctrl + ADD for zoom in and Ctrl + SUBTRACT for zoom out. But in case of automation, we can easily achieve it using Selenium. In this blog, we have two methods for Zoom In and Zoom Out.
Method 1: Using Robot Class
In Java, there is having a class “Robot”. Robot class having the “keyPress” and “
keyPress() method send an event to press the control, add or subtract key, and keyRelease() method send an event to release the pressed key. VK_CONTROL and VK_ADD used to zoom in and VK_CONROL and VK_SUBTRACT for zoom out.
Method 2: Using sendKeys Method
We can also do the form zoom action using the sendKeys method.
For this, we just need to locate the web element of zoom in and zoom out with the help of the locators. And then we have to use the "Keys.CONTROL" , "Keys.ADD" and "Keys.RELEASE" with the sendKeys().
Code for the Methode 1
package com.general.kairos;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class ZoomInOut {
@Test
public void googleSearchTest() throws AWTException, InterruptedException{
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\test\\java\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.softwaretestingmaterial.com");
driver.manage().window().maximize();
Thread.sleep(5000);
System.out.println("zooming");
Robot robot = new Robot();
System.out.println("About to zoom in");
for (int i = 0; i < 3; i++) {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ADD);
robot.keyRelease(KeyEvent.VK_ADD);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
Thread.sleep(5000);
System.out.println("About to zoom out");
for (int i = 0; i < 4; i++) {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
}
}
Code for Method 2
package stm;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class zoomInZoomOut {
@Test
public void googleSearchTest() throws InterruptedException{
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\test\\java\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.softwaretestingmaterial.com");
driver.manage().window().maximize();
Thread.sleep(5000);
System.out.println("zooming");
// To zoom in 3 times
for(int i=0; i<3; i++){
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,Keys.ADD));
}
// To zoom out 3 times
for(int i=0; i<3; i++){
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,Keys.SUBTRACT));
}
//To set the browser to default zoom level ie., 100%
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));
}
}
About Author
Kuldeep Sidana
Kuldeep Sidana is an orient QA Engineer having dexterity in Core Java and vouched with Automation Testing tool (Selenium Web Driver) and Manual Testing.