Actions class in Selenium



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:

  1. doubleClick(): Performs double click on the element
  2. clickAndHold(): Performs long click on the mouse without releasing it
  3. dragAndDrop(): Drags the element from one point and drops to another
  4. moveToElement(): Shifts the mouse pointer to the center of the element
  5. contextClick(): Performs right-click on the mouse

Keyboard Actions in Selenium:

  1. sendKeys(): Sends a series of keys to the element
  2. keyUp(): Performs key release
  3. 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");
    }
}

}

Tags: , , , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18