- Jan 07, 2023
- admin
- 0
To test an application, one needs to perform a number of user actions on it. To perform any operations on the web application such as double-click, selecting drop-down boxes, etc. the actions class is required. This article discusses how to handle the action class in Selenium.
What is Action Class in Selenium?
Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop in Selenium , clicking on multiple elements with the control key, among others. These operations are performed using the advanced user interactions API. It mainly consists of Actions that are needed while performing these operations.
Action class is defined and invoked using the following syntax:
Actions action = new Actions(driver); action.moveToElement(element).click().perform();
Methods of Action Class
Action class is useful mainly for mouse and keyboard actions. In order to perform such actions, Selenium provides various methods.
Mouse Actions in Selenium:
- doubleClick(): Performs double click on the element
- clickAndHold(): Performs long click on the mouse without releasing it
- dragAndDrop(): Drags the element from one point and drops to another
- moveToElement(): Shifts the mouse pointer to the center of the element
- contextClick(): Performs right-click on the mouse
Keyboard Actions in Selenium:
- sendKeys(): Sends a series of keys to the element
- keyUp(): Performs key release
- keyDown(): Performs keypress without release
Now, let’s understand how to perform various mouse and keyboard actions.
Examples of Action Class in Selenium
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System.Drawing;
using System.Threading;
namespace Selenium_Demo.TestCases
{
public class clsActions
{
public IWebDriver dr;
[SetUp]
public void Setup()
{
Console.WriteLine("I am from setup method");
dr = new ChromeDriver(path of chrome driver);
}
//Perform MoveToElement Action on WebElement
[Test]
public void MovetoElementAndClick()
{
dr.Navigate().GoToUrl("https://www.browserstack.com/");
Actions action = new Actions(dr);
IWebElement btnGetstartedFree = dr.FindElement(By.XPath("//a[@id='signupModalProductButton']"));
action.MoveToElement(btnGetstartedFree).Click().Build().Perform();
string expectedURL = "https://www.browserstack.com/users/sign_up";
string actualURL = dr.Url;
Assert.AreEqual(expectedURL, actualURL, "User is not navigated to signup page");
}
[Test]
public void MovetoElementAndClick_withoutActions()
{
dr.Navigate().GoToUrl("https://www.browserstack.com/");
IWebElement btnGetstartedFree = dr.FindElement(By.XPath("//a[@id='signupModalProductButton']"));
btnGetstartedFree.Click();
string expectedURL = "https://www.browserstack.com/users/sign_up";
string actualURL = dr.Url;
Assert.AreEqual(expectedURL, actualURL, "User is not navigated to signup page");
}
//Perform RightClick action on Web Element: ContextClick
[Test]
public void RightClickonElement()
{
dr.Navigate().GoToUrl("https://www.Techtutorialz.com/");
dr.Manage().Window.Maximize();
Actions action = new Actions(dr);
IWebElement element = dr.FindElement(By.XPath("//a[text()='View Tutorial Library']"));
action.ContextClick(element).Build().Perform();
dr.Close();
}
//Perform DoubleClick Action on the Web Element
[Test]
public void DoubleClickonElement()
{
dr.Navigate().GoToUrl("https://www.Techtutorialz.com/");
dr.Manage().Window.Maximize();
Actions action = new Actions(dr);
IWebElement element = dr.FindElement(By.XPath("//a[text()='View Tutorial Library']"));
action.DoubleClick(element).Build().Perform();
}
//Perform KeyDown action on WebElement
[Test]
public void KeyDownonElement()
{
dr.Navigate().GoToUrl("https://www.Techtutorialz.com/");
dr.Manage().Window.Maximize();
Actions action = new Actions(dr);
IWebElement element = dr.FindElement(By.XPath("//a[text()='View Tutorial Library']"));
action.KeyDown(element, Keys.Enter).Build().Perform();
string actualURL = dr.Url;
Assert.IsTrue(actualURL.Contains("tutorials-library"),"Not reached to Tutorial library page");
dr.Close();
}
[Test]
public void KeyDownonElement1()
{
dr.Navigate().GoToUrl("https://www.Techtutorialz.com/");
dr.Manage().Window.Maximize();
Actions action = new Actions(dr);
IWebElement element = dr.FindElement(By.XPath("//a[text()='View Tutorial Library']"));
element.Click();
string actualURL = dr.Url;
Assert.IsTrue(actualURL.Contains("tutorials-library"), "Not reached to Tutorial library page");
dr.Close();
}
//Perform ScrollToElement action on Webelement
[Test]
public void VerifyPrivacyNote()
{
dr.Navigate().GoToUrl("http://amazon.in");
IWebElement linkPrivacy = dr.FindElement(By.XPath("//a[text()='Privacy Notice']"));
Actions action = new Actions(dr);
action.ScrollToElement(linkPrivacy).Build().Perform();
linkPrivacy.Click();
}
//Perform DragAndDrop action
[Test]
public void DragAndDropElement()
{
dr.Navigate().GoToUrl("http://demo.guru99.com/test/drag_drop.html");
//Element which needs to drag.
IWebElement From = dr.FindElement(By.XPath("//*[@id='credit2']/a"));
//Element on which need to drop.
IWebElement To = dr.FindElement(By.XPath("//*[@id='bank']/li"));
//Using Action class for drag and drop.
Actions action = new Actions(dr);
//Dragged and dropped.
action.DragAndDrop(From, To).Build().Perform();
Thread.Sleep(2000);
IWebElement debtMovement = dr.FindElement(By.XPath("//td[normalize-space(text())='Debit Movement']"));
Assert.IsTrue(debtMovement.Size != Size.Empty, "Debit movement is not displayed");
}
}
}