Thursday 9 February 2017

SELENIUM: How To Zoom In And Zoom Out Page In Selenium Test

How To Zoom In And Zoom Out Page In Selenium Test:
-------------------------------------------------------------------------

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. 


Example:
package com.rameshsoft.rameshselenium;

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 ZoomInOutDemo {
          WebDriver driver;
         
          @BeforeTest
           public void setup() throws Exception {
          System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://google.com/");
           }
           
           @Test
           public void getScrollStatus(){
            //Calling zoom methods to zoom in/out page.
            zoomIn(); 
            zoomOut();
            zoomOut();
           }
           
           public void zoomIn(){
            //To zoom In page 4 time using CTRL and + keys.
            for(int i=0; i<4; i++){  
            driver.findElement(By.id("hplogo")).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.id("hplogo")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
            }
           }
           
          }
 

No comments:

Post a Comment