SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Zoom In And Zoom Out Page In Selenium Test

As you know, Selenium webdriver Is very big project which supports many different languages to create test cases and execute them In different browsers. It support keyboard and mouse events very effectively using Advanced User Interaction API. I have listed Advanced User Interaction API
tutorial links On THIS PAGE. Page Zoom In/Out Is one of the tricky action and we can do It using advanced user Interaction API.

How to perform Zoom In/Out?
We will use webdriver advanced user Interaction API to perform this operation. We can perform page zoom action using Method sendKeys(WebElement element, java.lang.CharSequence... keysToSend). Here element Is webelement which will receive keystrokes and CharSequence Is sequence of characters to send on element.

Bellow given example will expain you more how to zoom In/out/default level of page.
package Testing_Pack;

import java.util.concurrent.TimeUnit;
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.BeforeTest;
import org.testng.annotations.Test;

public class ZoomBrowser {
 WebDriver driver;
 
 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://google.com/"); 
 }
 
 @Test
 public void getScrollStatus(){
  //Call zooming functions to zoom in and out page.
  zoomIn();  
  zoomOut();
  zoomOut();
  set100();
 }
 
 public void zoomIn(){
  //To zoom In page 4 time using CTRL and + keys.
  for(int i=0; i<4; i++){   
  driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
  }
 }
 
 public void zoomOut(){
  //To zoom out page 4 time using CTRL and - keys.
  for(int i=0; i<4; i++){
   driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
  }
 }
 
 public void set100(){
  //To set browser to default zoom level 100%
  driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));
 }
}

When you run above example, 1st It will zoom In page 4 times then It will zoom out page 8 time and then It will set browser at default 100% zoom level.

5 comments:

  1. I am getting the error as


    org.openqa.selenium.WebDriverException: unknown error: cannot focus element

    ReplyDelete
  2. public void zoomIn(){
    //To zoom In page 4 time using CTRL and + keys.
    for(int i=0; i<2; i++){
    driver.findElement(By.xpath(" .//*[@id='widget-zoom-in']")).click ();

    //driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
    }
    }

    public void zoomOut(){
    //To zoom out page 4 time using CTRL and - keys.
    for(int i=0; i<2; i++){
    driver.findElement(By.xpath(".//*[@id='widget-zoom-out']")).click ();

    }
    }


    this function is use full to zoom in google maps any way thanks to given such a useful solution

    sridharnath.t

    ReplyDelete
  3. WebElement html = driver.findElement(By.tagName("html"));

    html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD)); // Zoom In
    html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); // Zoom Out

    this is working for me ..

    ReplyDelete
  4. WebElement html = driver.findElement(By.tagName("html"));

    html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); //Zoom out
    html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD)); //Zoom in

    ReplyDelete