Trending October 2023 # Action Class In Selenium – Mouse Click &Amp; Keyboard Events # Suggested November 2023 # Top 13 Popular | Cersearch.com

Trending October 2023 # Action Class In Selenium – Mouse Click &Amp; Keyboard Events # Suggested November 2023 # Top 13 Popular

You are reading the article Action Class In Selenium – Mouse Click &Amp; Keyboard Events updated in October 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Action Class In Selenium – Mouse Click &Amp; Keyboard Events

In this tutorial, we will learn handling Keyboard and Mouse Event in Selenium Webdriver

Action Class in Selenium Handling Keyboard & Mouse Events

In this tutorial, we will learn handling Keyboard and Mouse Event in Selenium Webdriver

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.

Method Description

dragAndDrop(source, target)

Parameters:

source- element to emulate button down at.

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

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

Parameters:

source- element to emulate button down at.

xOffset- horizontal move offset.

yOffset- vertical move offset.

keyDown(modifier_key)

Parameters:

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

keyUp(modifier _key)

Parameters:

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

moveByOffset(x-offset, y-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)

Parameters:

toElement- element to move to.

release() Releases the depressed left mouse button at the current mouse location

sendKeys(onElement, charsequence)

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.

Step 1: Import the Actions and Action classes.

Step 2: Instantiate a new Actions object.

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

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.

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) { System.setProperty("webdriver.gecko.driver","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.

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.

public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement txtUsername = driver.findElement(By.id("email")); Actions builder = new Actions(driver); Action seriesOfActions = builder .moveToElement(txtUsername) .keyDown(txtUsername, Keys.SHIFT) .sendKeys(txtUsername, "hello") .keyUp(txtUsername, Keys.SHIFT) .build(); seriesOfActions.perform() ; }

Summary

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

You're reading Action Class In Selenium – Mouse Click &Amp; Keyboard Events

Update the detailed information about Action Class In Selenium – Mouse Click &Amp; Keyboard Events on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!