java keyboard.press_[Selenium+Java] Keyboard & Mouse Event using Action Class in Selenium Webdriver

Original Source: https://www.guru99.com/keyboard-mouse-events-files-webdriver.html

Handling Keyboard & Mouse Events

Handling special keyboard and mouse events are done using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events. The following are the most commonly used keyboard and mouse events provided by the Actions class.

MethodDescription

clickAndHold()

Clicks (without releasing) at the current mouse location.

contextClick()

Performs a context-click at the current mouse location.

doubleClick()

Performs a double-click at the current mouse location.

dragAndDrop(source, target)

Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

Parameters:

source- element to emulate button down at.

target- element to move to and release the mouse at.

dragAndDropBy(source, x-offset, y-offset)

Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

Parameters:

source- element to emulate button down at.

xOffset- horizontal move offset.

yOffset- vertical move offset.

keyDown(modifier_key)

Performs a modifier key press. Does not release the modifier key - subsequent interactions may assume it's kept pressed.

Parameters:

modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL)

keyUp(modifier _key)

Performs a key release.

Parameters:

modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL)

moveByOffset(x-offset, y-offset)

Moves the mouse from its current position (or 0,0) by the given offset.

Parameters:

x-offset- horizontal offset. A negative value means moving the mouse left.

y-offset- vertical offset. A negative value means moving the mouse down.

moveToElement(toElement)

Moves the mouse to the middle of the element.

Parameters:

toElement- element to move to.

release()

Releases the depressed left mouse button at the current mouse location

sendKeys(onElement, charsequence)

Sends a series of keystrokes onto the element.

Parameters:

onElement - element that will receive the keystrokes, usually a text field

charsequence - any string value representing the sequence of keystrokes to be sent

In the following example, we shall use the moveToElement() method to mouse-over on one Mercury Tours' table rows. See the example below.

80f7b94e13e55080727598d8fe5c2886.png

The cell shown above is a portion of a

element. If not hovered, its color is #FFC455 (orange). After hovering, the cell's color becomes transparent. It becomes the same color as the blue background of the whole orange table.

Step 1:Import the Actions and Action classes.

image047.png

Step 2:Instantiate a new Actions object.

6030d925e4794ab9dab537d60fec5090.png

Step 3:Instantiate an Action using the Actions object in step 2.

a5357df7ece7963ae7c929cf1763cf24.png

In this case, we are going to use the moveToElement() method because we are simply going to mouse-over the "Home" link. The build() method is always the final method used so that all the listed actions will be compiled into a single step.

Step 4:Use the perform() method when executing the Action object we designed in Step 3.

fd3941f02696bd5c335848b2a7bb76b8.png

Below is the whole WebDriver code to check the background color of the

element before and after the mouse-over.

package newproject;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;

public class PG7 {

public static void main(String[] args) {

String baseUrl = "http://demo.guru99.com/test/newtours/";

System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);

WebElement link_Home = driver.findElement(By.linkText("Home"));

WebElement td_Home = driver

.findElement(By

.xpath("//html/body/div"

+ "/table/tbody/tr/td"

+ "/table/tbody/tr/td"

+ "/table/tbody/tr/td"

+ "/table/tbody/tr"));

Actions builder = new Actions(driver);

Action mouseOverHome = builder

.moveToElement(link_Home)

.build();

String bgColor = td_Home.getCssValue("background-color");

System.out.println("Before hover: " + bgColor);

mouseOverHome.perform();

bgColor = td_Home.getCssValue("background-color");

System.out.println("After hover: " + bgColor);

driver.close();

}

}

The output below clearly states that the background color became transparent after the mouse-over.

6de002cd231e29f697b1d3d93f78837d.png

Building a Series of Multiple Actions

You can build a series of actions using the Action and Actions classes. Just remember to close the series with the build() method. Consider the sample code below.

6d81ae459325d996c728dd8fef35699b.png

c04a1191ca10960fee0dbfad8cad88b5.png

Summary

Handling special keyboard and mouse events are done using the AdvancedUserInteractions API.

Frequently used Keyword and Mouse Events are doubleClick(), keyUp, dragAndDropBy, contextClick & sendKeys.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值