How To Zoom In And Zoom Out Browser In Selenium WebDriver
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
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
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
Thanks Raj for your inputs. I will try at my end and will get back to you for any issues.
Thank you so much.
Hey hi Raj,
Thank you for the solution and its working fine for me.
Thank you so much!!
Hi Raj,
Would like to share an observation for Method 2 code. I tried using SendKeys() method, but no change is seen on the screen. i have also put thread.sleep() to see the changes but nothing is seen.
I agree with you Saili Inamdar. Good observation. Sometimes in some applications it wont work. We could use any of the two methods.
Nice to hear that it worked for you.
Thank you!! 🙂
Robot class worked thanks 🙂