Tuesday, 16 December 2014

How to perform a series of actions using Keys.chord?

Using Keys.chord, we can simulate a series of actions.It takes a sequence of Keys.XXXX or strings, appends each of the values to a string, and adds the chord termination key (Keys.NULL) and returns the resultant string. 

Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event

Let's see how we can tab from one element to another and perform some action in the new element.


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;

public class KeysChordDemo1 {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate()
.to("http://www.toolsqa.com/automation-practice-form/");

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.manage().window().maximize();

WebElement we = driver.findElement(By.name("firstname"));

String chords = Keys.chord(Keys.SHIFT, "Senthil")

//Tab to last name field
+ Keys.TAB

//Enter last name in caps . Use Keys.NULL to release SHIFT key
+ Keys.SHIFT + "kumaran" + Keys.NULL

//Tab to gender radio button
+ Keys.TAB

//Tab to Years of Experience,control will be in radio button 1
+ Keys.TAB

//Radio button 2 is selected
+ Keys.ARROW_RIGHT

//Radio button 3  is selected
+ Keys.ARROW_RIGHT

//Radio button 4  is selected
+ Keys.ARROW_RIGHT

//Radio button 5  is selected
+ Keys.ARROW_RIGHT

//Radio button 6  is selected
+ Keys.ARROW_RIGHT

//Radio button 7 is finally selected
+ Keys.ARROW_RIGHT

//Tab to date field and enter date
+ Keys.TAB + "27-05-2014"

//Tab to Profession checkbox, control is in first checkbox
+ Keys.TAB

//Tab to second checkbox 
+ Keys.TAB 

//And check it 
+ Keys.SPACE;

we.sendKeys(chords);


Thread.sleep(3000l);

driver.quit();

}

}


Pay It Forward..

DR

No comments:

Post a Comment