Actions
The Actions class is part of org.openqa.selenium.interactions package. It provides a way to perform complex user interactions such as mouse and keyboard actions. It is particularly useful for handling scenarios where simple interactions like clicks or text input are not sufficient, and more advanced interactions are needed. Actions class implements the builder pattern: Builds a CompositeAction containing all actions specified by the method calls. It will Call method perform() at the end of the method chain to actually perform the actions.
Following are some of the commonly used methods.
- sendKeys
- moveToElement
- contextClick
- doubleClick
- dragAndDrop
- keyDown
- keyUp
sendKeys
This method will sends keys to the active element or the web element provided. This method will return a Actions object, hence you can chain multiple actions method to this method as per your requirements.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/set/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement firstNameElem = driver.findElement(By.name("fname")); Actions actions = new Actions(driver); actions.sendKeys(firstNameElem, "Test Automation ").sendKeys("Studio").perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/set/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement firstNameElem = driver.FindElement(By.Name("fname")); Actions actions = new Actions(driver); actions.SendKeys(firstNameElem, "Test Automation ").SendKeys("Studio").Perform(); driver.Quit();
moveToElement
This method will moves the mouse to the middle of the element. The element is scrolled into view and its location is calculated using getClientRects.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/actions/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement idElem = driver.findElement(By.id("idMenu")); Actions actions = new Actions(driver); actions.moveToElement(idElem).click().perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/actions/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement idElem = driver.FindElement(By.Name("idMenu")); Actions actions = new Actions(driver); actions.MoveToElement(idElem).Click().Perform(); driver.Quit();
contextClick
This method will performs a context-click at middle of the given element. First it will performs a mouseMove to the location of the element, then will perform the context-click.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/actions/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement contextClickElem = driver.findElement(By.id("action3")); Actions actions = new Actions(driver); actions.contextClick(contextClickElem).perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/actions/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement contextClickElem = driver.FindElement(By.Id("action3")); Actions actions = new Actions(driver); actions.ContextClick(contextClickElem).Perform(); driver.Quit();
doubleClick
Performs a double-click at middle of the given element. First it will performs a mouseMove to the location of the element, then will perform the double-click.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/actions/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement doubleClickElem = driver.findElement(By.id("action1A")); Actions actions = new Actions(driver); actions.doubleClick(doubleClickElem).perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/actions/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement doubleClickElem = driver.FindElement(By.Id("action1A")); Actions actions = new Actions(driver); actions.DoubleClick(doubleClickElem).Perform(); driver.Quit();
dragAndDrop
This method will take two Web Element as parameter, ie source element and a destination element. This method performs click-and-hold at the location of the source element, then moves to the location of the target element, then releases the mouse.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/actions/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement sourceElem = driver.findElement(By.xpath("//ul[@id='sortable1']/li[1]")); WebElement destElem = driver.findElement(By.id("sortable2")); Actions actions = new Actions(driver); actions.dragAndDrop(sourceElem, destElem).perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/actions/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement sourceElem = driver.FindElement(By.XPath("//ul[@id='sortable1']/li[1]")); IWebElement destElem = driver.FindElement(By.Id("sortable2")); Actions actions = new Actions(driver); actions.DragAndDrop(sourceElem, destElem).Perform(); driver.Quit();
keyUp and keyDown
The keyDown method will perform a modifier key press after focusing on an element. The keyUp method will performs a modifier key release after focusing on an element.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/actions/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); WebElement contextClickelem = driver.findElement(By.id("action3")); contextClickelem.sendKeys("Welcome"); Actions actions = new Actions(driver); actions.keyDown(contextClickelem, Keys.CONTROL).keyUp(contextClickelem, "a").keyDown(contextClickelem, Keys.DELETE).perform(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/actions/"; driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); IWebElement contextClickelem = driver.FindElement(By.Id("action3")); contextClickelem.SendKeys("Welcome"); Actions actions = new Actions(driver); actions.KeyDown(contextClickelem, Keys.Control).KeyUp(contextClickelem, "a").KeyDown(contextClickelem, Keys.Delete).Perform(); driver.Quit();