Tuesday 26 September 2017

Chord and Keys in Selenium Webdriver to press multiple keys

Chord and Keys in Selenium WebDriver to press multiple keys




keys class in selenium webdriver, how to use keyboard keys in selenium webdriver, keys.control selenium, how to press Control a+c+v key using selenium, java key example





A good option to simulate multiple keys press can be achieved through Chord and Keys in Selenium Webdriver

Senario Taken: Type some text in the text box(Search box in this example) and perform Copy and Paste using Chord and Keys




package com.p1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class chord {
   
    public WebDriver driver;
   
     @Test
     public void testKeys_Chord() {
       
       driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);   
     
      //Object for the search box
      WebElement sb=driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[1]/input"));   
     
      //Type "The mouse-over using JavascriptExecutor and Actions in Selenium Webdriver" in search box
      sb.sendKeys("The mouse-over using JavascriptExecutor and Actions in Selenium Webdriver "); 
    
      //Perform Control A and C actions  
      sb.sendKeys(Keys.chord(Keys.CONTROL,"a"), "");
      sb.sendKeys(Keys.chord(Keys.CONTROL,"c"), "");
     
      //Clear the Text 'The mouse-over using JavascriptExecutor and Actions in Selenium Webdriver' typed in the Search box
      sb.clear();
     
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Perform paste action using CTRL+V in search box
        sb.sendKeys(Keys.chord(Keys.CONTROL, "v"), "");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
        driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[2]/input")).click();

     }

      @BeforeClass
      public void beforeClass()
         {
        System.setProperty("webdriver.gecko.driver", "C://Selenium//geckodriver-v0.18.0-win64//geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
        driver.get("http://allthingsaboutuft.blogspot.in/");
        driver.manage().window().maximize();
         }


      @AfterClass
      public void afterClass() throws Exception
         {
          driver.quit();
         }

}

No comments:

Post a Comment