How To Zoom In And Zoom Out In Selenium

We all know Selenium automates browsers. When we are running selenium scripts, sometimes we may face a situation where we need to perform zoom in and zoom out. In Selenium, this can be easily achieved. In this article, I will show you two methods on how to Zoom In and Zoom Out in Selenium WebDriver.

Manually, we have to press CTRL+ADD to do Zoom In and we have to press CTRL+SUBTRACT to do zoom out. I have tested both the below methods i.e., how to zoom in and zoom out on firefox.

Method 1: Using Robot Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package stm;
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 zoomInZoomOut {
@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);
}
}
}

We can perform page zoom action using sendKeys method.

Method 2: Using SendKeys Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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”));
}
}

Must Read: Selenium Complete Tutorial

Read More:

  • Selenium Interview Questions
  • TestNG Interview Questions
  • SQL Interview Questions For Testers
  • Why Did You Choose Software Testing As A Career
  • Cross Browser Testing Complete Tutorial
  • 100+ Types of Software Testing