Keyboard and Mouse operations are two integral part of any application. More the reach UI more the complex operation. Here is a list of methods of action classe that is supported by Selenium in order to handle Mouse and Keyboard operations.
Keyboard Operations:
Operation | Syntax | Usage | |
KeyBoard | sendkeys() | It sends the string to the locator | |
keyDown(keys modifier) | Takes the modifier key(shift,alt,control) as parameter and modifies the key | keys.SHIFT keys.ALT keys.CONTROL The exception generated is IllegalArgumentException | |
keyDown(WebElement element,keys modifierKey) | Another flavor of the keyDown which the modifier key press action is performed on a webElement. | ||
keyUp | It is used to simulate the modifier key up or key release action. This methods needs to be invoked after a keyDown method | ||
keyUp(WebElement element,keys modifierKey) | Another flavor of the keyUp which the modifier key release action is performed on a webElement. | ||
sendKeys(CharacterSequence keysToSend) | It is used when we need to send sequence of keys to a currently focused webElement. It is helpful when using modifier keys and that can not be released on the element. | ||
sendKeys(WebElement element,CharacterSequence keysToSend) | Sends a sequence of keys to a webElement. | ||
|
Mouse operations:
Operation | Syntax | Usage | |
Mouse | Click() | It clicks on the current mouse pointer | Best when used with mouse and keyboard events. Mostly a composite events |
Click(WebElement element) | It clicks on the middle of the webelement that is passed as a parameter. | ||
ClickAndHold() | Performs a click method without releasing the mouse button | ||
ClickAndHold(WebElement element) | It is used to Performs a click method without releasing the mouse button on a webelement | ||
ContextClick() | Performs a right click operation in the current location of the mouse. | ||
ContextClick(WebElement element) | Performs a right click operation in the current location of the mouse on an element. | ||
doubleClick() | Performs a double click operation in the current location of the mouse. | ||
doubleClick(WebElement element) | Performs a double click operation in the current location of the mouse on an element. | ||
dragAndDrop(WebElement fromElement,WebElement toElement) | Performs a drag and drop method from one element to other. | ||
dragAndDropBy(WebElement fromElement, xoffset,int yoffset) | performs drag and drop from element to a x,y position | Clicks on the webelement and drag to the x,y location and drops it | |
moveByOffset(int xoffset,int yoffset) | Performs a move operation of the mouse pointer based on x and y | ||
moveToElement(WebElement toElement) | Performs a move operation to the webelement, | ||
moveToElement(WebElement element,int xoffset,int yoffset) | Performs a right click operation in the current location of the mouse on an element. | x,y calculation is done from the top left corner of the webelemnt specified. | |
release() | Releases the pressed left mouse button at the current mouse position. | ||
release(WebElement element) | Releases the pressed left mouse button to an webelement | ||
build() | Generates the composite action | ||
one more example can be as follows
|
Last item in this category is to discuss the Drag and Drop. With rich web interface,drag able web elements are very common now a days.Using Action class (an interface which represents a single user interaction) we can simulate the same thing while testing.