Select Page
Automation Testing

An Introductory Action Class Guide for Beginners

Looking to automate keyboard and mouse actions for your automation testing? Find out how to make it happen by reading our Action Class Guide.

An Introductory Action Class Guide for Beginners
Listen to this blog

If you are going to test an application using Selenium WebDriver, you most definitely will face scenarios where you will be needed to trigger keyboard and mouse interactions. This is where our Action Class Guide comes into the picture. Basically, Action Class is a built-in feature provided by Selenium for simulating various events and acts of a keyboard and mouse. With the help of Action classes, you will be able to trigger mouse events like Double Click, Right Click, and many more events. The same goes for keyboards as well, you can trigger the functions of CTRL key, CTRL + different keys, and other such combinations. As one of the best QA companies, we have been able to use Action Class to its zenith by using it in various combinations as per the project needs. But before exploring such Action class implementations, let’s take a look at some basics.

Action Class Guide for MouseActions

So we wanted to start our Action Class Guide by listing some of the most frequently used mouse events available in the Action class.

click() – Clicks on the particular WebElement (Normal Left Click)

contextClick() – Right clicks on the particular WebElement

doubleClick() – Performs a double click on the WebElement

dragAndDrop (WebElement source, WebElement target) – Clicks and holds the web element to drag it to the targeted web element where it is released.

dragAndDropBy(WebElement source, int xOffset, int yOffset) – Clicks and Drag the element to the given location using offset values

moveToElement(WebElement) – Moves the mouse to the web element and holds it on the location (In simple words, the mouse hovers over an element)

moveByOffset(int xOffSet, int yOffSet) – Moves the mouse from the current position to the given left (xOffset value) and down (yOffset Value).

clickAndHold(WebElement element) – Clicks and holds an element without release.

release() – Releases a held mouse click.

Action Class Guide Keyboard Actions

Same as above, we have listed some frequently used keyboard events available in the Action class,

keyDown(WebElement, java.lang.CharSequence key) – To press the key without releasing it on the WebElement

keyUp(WebElement, java.lang.CharSequence key) – To release the key stroke on the webElement

sendkeys(value) – To enter values on WebElements like textboxes

So by including these methods, you can smoothly run your script and execute the code without any issues….

Absolutely not, we’re just kidding. We still have to gather all the action methods and execute them under the Action class.

build() – It is a method where all the actions are chained together for the action which is going to be performed.

So the above method can be used to make the actions that are to be executed ready.

perform() – It is a method used to compile and also execute the action class.
A perform method can be called alone without a build method to execute the action class methods if only one action is performed.

Action Class Guide for Performing actions

Now that we have gone through the basics, let’s find out how to implement the Action Classes in Code.

Step1:

Import the Interaction package that contains the Action Class. You can use the below line for importing,

“importorg.openqa.selenium.interactions.Actions; ”

Step2:

Create the object of the Action Class and use the Web Driver reference as the argument
Actions action = new Actions (driver)

Step3:

Once the above two steps have been completed, you can start writing your script using the Action classes and the different methods available.

Let’s proceed further and take a look at the implementation and uses of the actions available for both the mouse & keyboard.

1. SendKeys(WebElement element, value)

As stated above, this action class is mainly used to send a char sequence into the textbox. But it is also worth noting that we can use it to send the keystrokes of different key combinations likeCTRL+T, Enter, and so on.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class SendKeys {
    public static void main(String[] args) {
        WebDriver driver;        System.setProperty("webdriver.chrome.driver","D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.flipkart.com/");
        driver.manage().window().maximize();
        Actions action = new Actions(driver);
        WebElement eleSearchBox = driver.findElement(By.cssSelector("input[placeholder='Search for products, brands and more']"));
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        action.sendKeys(eleSearchBox, "Iphone").build().perform();
        action.sendKeys(Keys.ENTER).build().perform();
        driver.close();
    }
}

By using the SendKeys method, an element is searched by the keystroke instead of clicking on the Search Button. (i.e.) We can clearly see in the code that the “Keys.Enter” is inside the Keys class that has various keystrokes available for the keys.

2. MoveToElement(WebElement element)

You might be in a position to test if an element changes color, shows some additional information, or performs the intended action when the mouse hovers over it. So let’s take a look at the code and find out how you can make it happen.

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class MouseHover {
    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://www.leafground.com/");
        driver.manage().window().maximize();
        Actions action = new Actions(driver);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,170)", "");
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//img[@alt='mouseover']"))).click();
        WebElement eleTabCourses = driver.findElement(By.xpath("//a[normalize-space()='TestLeaf Courses']"));
        action.moveToElement(eleTabCourses).build().perform();
        driver.close();
    }
}

We have written the above code in a way that the code first waits for the image to become clickable. Once it loads, the image gets clicked, and the mouse hovers over the element for a second.

3. DragAndDrop(source, target)

So there are basically two types of drag and drop that we will be seeing in this Action Class Guide. This is the type of action class using which we can assign a target area where the element can be dragged and dropped. Now let’s see the code to execute the DragAndDrop action,

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DragAndDrop {
        public static void main(String[] args) {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.get("http://www.leafground.com/");
            driver.manage().window().maximize();
            Actions action = new Actions(driver);
            driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//h5[normalize-space()='Droppable']")).click();
            WebElement eleSource = driver.findElement(By.xpath("//div[@id='draggable']"));
            WebElement eleTarget = driver.findElement(By.xpath("//div[@id='droppable']"));
            action.dragAndDrop(eleSource,eleTarget).build().perform(););
            driver.close();
        }
}

For dragging an element to the dropped place, first, the locators are captured for the source and target. Following this, they are passed inside the action method using dragAndDrop.

4. DragAndDropBy(WebElement source,int xOffset, int yOffSet )

So we have already seen how to drag a drop an element within a targeted area, but what if we would like to drag and drop an element by a defined value? Let’s take a look at the code and find out how.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DragAndDropOffset {
        public static void main(String[] args) throws InterruptedException {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.get("http://www.leafground.com/");
            driver.manage().window().maximize();
            Actions action = new Actions(driver);
            driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//img[@alt='Draggable']")).click();
            WebElement eleDrag= driver.findElement(By.xpath("//div[@id='draggable']"));
            action.dragAndDropBy(eleDrag,200,130).build().perform();
            Thread.sleep(2000);
            driver.close();
        }
    }

In the above code, we have used the DragAndDropBy method in a way that it clicks and moves the element to the offset position as specified and releases it once the target location is reached.

5. Click(WebElement element)

There is no way to test anything without being able to use the left click button. So let’s find out the code to execute this very basic and necessary functionality.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class LeftClick {
    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        Actions actions = new Actions(driver);
        WebElement eleInput = driver.findElement(By.name("q"));
        actions.sendKeys(eleInput, "www.codoid.com").build().perform();
        WebElement BtnSearch = driver.findElement(By.xpath("//div[@class='FPdoLc lJ9FBc']//input[@name='btnK']"));
        actions.click(BtnSearch).build().perform();
        driver.close();
    }
}

6. ContextClick(WebElement element)

Though the right-click is not used as commonly as the left click, it is still a very basic functionality every tester must know. So let’s take a look at the code to find out how to implement it.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class RightClick {
    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();
        Actions action = new Actions(driver);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        WebElement eleRightClick = driver.findElement(By.xpath("//span[@class='context-menu-one btn btn-neutral']"));
        action.contextClick(eleRightClick).perform();
        driver.close();
    }
}

It is worth mentioning here that we have not used ‘build’ anywhere in the above code. Instead, we have used ‘perform’ to execute the functionality.

7. DoubleClick(WebElement element)

Just like the previous functionalities we have seen in the Action Class Guide, double-click is another basic functionality that is vital to any form of testing. So let’s jump straight to the code.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DoubleClick {
    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();
        Actions action = new Actions(driver);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        WebElement eleDoubleClick = driver.findElement(By.xpath("//button[normalize-space()='Double-Click Me To See Alert']"));
        action.doubleClick(eleDoubleClick).perform();
        driver.quit();
    }

8. KeyDown(WebElement element, Modifier Key) & KeyUp (WebElement element, Modifier Key)

CTRL, SHIFT, and ALT are few examples of modifier keys that we all use on a day-to-day basis. For example, we hold down Shift if we want to type something in caps. So when we use the KeyDown action class, it holds a particular key down until we release it using the KeyUp action class. With that said, let’s see an example code in which we have used these functionalities,

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class KeyDownAndKeyUp {
    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        Actions actions = new Actions(driver);
        WebElement eleInput = driver.findElement(By.name("q"));
        actions.click(eleInput).build().perform();
        actions.keyDown(eleInput, Keys.SHIFT);
        actions.sendKeys("eiffel tower");
        actions.keyUp(eleInput,Keys.SHIFT);
        actions.sendKeys(Keys.ENTER);
        actions.build().perform();
        driver.close();
    }
}

So if you have taken a look at the code, it is evident that once we have used the KeyDown method, the Shift key was pressed down. So the text ‘eiffel tower’ that was fed in the next line would have gotten capitalized. Now that the KeyDown has solved its purpose in this scenario, we have used to KeyUp method to release it.

9. MoveByOffset(int xOffSet, int yOffSet)

As seen above, ByOffset(int x, int y) is used when we need to click on any particular location. We can do this by assigning locations for the x and y axes. Now let’s explore the code that we have used for execution.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class MoveByOffSet {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        Actions actions = new Actions(driver);
        WebElement eleInput = driver.findElement(By.name("q"));
        actions.sendKeys(eleInput, "Eiffel").build().perform();
        actions.sendKeys(Keys.ENTER).build().perform();
        Thread.sleep(2000);
        actions.moveByOffset(650, 300).contextClick().build().perform();
        driver.close();
    }
}

10. ClickAndHold(WebElement element)

The action method that we will be seeing now in our Action Class Guide can be used when an element has to be clicked and held for a certain period of time.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class ClickAndHold {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        Actions actions = new Actions(driver);
        WebElement eleInput = driver.findElement(By.name("q"));
        actions.sendKeys(eleInput, "Flower").build().perform();
        actions.moveByOffset(500,300).click().build().perform();
        Thread.sleep(2000);
        WebElement BtnSearch = driver.findElement(By.xpath("//div[@class='FPdoLc lJ9FBc']//input[@name='btnK']"));
        actions.clickAndHold(BtnSearch).build().perform();
        driver.close();
    }
}

In the above code, we have first opened Google and then searched using ‘Flower’ as the input and then performed a left-click action at the defined location. After which, we have performed a click and hold action on the search button.

Note:

In addition to that, if we need the click to be released, we can use the release method to release the clicked element before using ‘build’.

actions.clickAndHold(BtnSearch).release().build().perform();

Uploading a File Using SendKeys Method:

We know that the SendKeys action method can be used to send a character sequence. But one interesting way to use it would be to upload a normal document. If you’re wondering how that is possible, let’s take a look at the code,

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FileUpload {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://www.leafground.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath(" //img[@alt='contextClick']")).click();
        WebElement eleSendFile = driver.findElement(By.cssSelector("input[name='filename']"));
        eleSendFile.sendKeys("C:\\Users\\OneDrive\\Desktop\\Sample.txt");
        Thread.sleep(2000);
        driver.close();
    }
}

In the above code, we have used the SendKeys action method to enter the file’s address path to locate the object that has to be uploaded. The document which we have used here is an input type document as this type of document is only ideal for sending the document using SendKeys.

Note:

Just in case you are not using Google Chrome and would like to execute these action class methods in Mozilla Firefox, all you have to do is just add the driver and set the system property for the gecko driver and initialize the driver object for it using the below line.

System.setProperty("webdriver.gecko.driver", "D:\\ActionClass\\src\\test\\java\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();

Conclusion:

We hope you have enjoyed reading our introductory action class guide that has covered the basic action class methods starting from a basic ‘Click’ to uploading a file using ‘SendKeys’. Action class helps one to perform all the basic actions on a webapp.Release() method, which can be used to release any element that was clicked or held. ‘Perform’ can be used alone, but if there are more composite actions, build and perform should be used together. As a leading test automation company, we are well aware of how resourceful action classes can be when used effectively. Once you have understood the basics, you would be able to perform so many more actions by using combinations of the action methods established in this blog.

Comments(0)

Submit a Comment

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

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility