What are keyboard events?
Keyboard events are actions that can be performed with keyboard such as Alt F4, Shift "A", Control+Alt+Delete.There may be scenarios were we need to perform typing like a keyboard in certain fields
How Selenium handles keyboard events?
Selenium offers Action class in order to handle the keyboard events.The Action is a user facing API for emulating complex user action events.
There are different methods that are offered by Action Class. Most commonly used for keyboard events are
Notes
1-WebElement is a class in Selenium which is currently used to store the email field location
2-Action class offers keyDown method which is used to press down a particular key
3-.build() method is used to combine different keys method in a single command.For example in below case we are pressing some key then sending some value to the field and then unpress the same key, so 3 different types of action took place in order to combine them build() method is used
4-Perform method is used here to execute the action.
Keyboard events are actions that can be performed with keyboard such as Alt F4, Shift "A", Control+Alt+Delete.There may be scenarios were we need to perform typing like a keyboard in certain fields
How Selenium handles keyboard events?
Selenium offers Action class in order to handle the keyboard events.The Action is a user facing API for emulating complex user action events.
There are different methods that are offered by Action Class. Most commonly used for keyboard events are
- keyDown()- Press down a particular Key in keyboard, It can be Caps Lock or Shift button or any key present on keyboard
- KeyUp()- UnPress the a particular key in keyboard. It can be Caps Lock or Shift button or any key present on keyboard
Here is the code snippets for using the Actions
//Configure the Action
Action builder=new Action(driver)
//To press a particular key(e.g Shift key)
builder.keydown(keys.SHIFT)
The below types a capital letter text on Email id field present on the facebook page
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.*;
public class KeyboardEvents
{
public static void main(String[] args) {
WebDriver a = new FirefoxDriver();
a.manage().window().maximize();
a.get("http://www.facebook.com");
a.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions builder = new Actions(a);
WebElement e=a.findElement(By.id("email"));
Action writeCapital=builder.keyDown(Keys.SHIFT).sendKeys(e, "abc").keyUp(Keys.SHIFT).build();
writeCapital.perform();
}
}
Notes
1-WebElement is a class in Selenium which is currently used to store the email field location
2-Action class offers keyDown method which is used to press down a particular key
3-.build() method is used to combine different keys method in a single command.For example in below case we are pressing some key then sending some value to the field and then unpress the same key, so 3 different types of action took place in order to combine them build() method is used
4-Perform method is used here to execute the action.
0 comments:
Post a Comment