testsigma
Topics
left-mobile-bg

Action Class in Selenium: Its Method and Implementation

August 9, 2023Shreshtha Gokhru
right-mobile-bg
Action Class in Selenium: Its Method and Implementation
imageimage

Start automating your tests 5X Faster in Simple English with Testsigma

Try for free

Selenium stands apart from other testing tools by offering a variety of techniques to test applications, which has led to its widespread use in automating web applications. No matter how complicated the user interface (UI) is, Selenium offers several methods to automate web apps, decreasing or eliminating the need for manual labor.

So, how can we automate online applications when using a mouse or keyboard to communicate with the browser?

In Selenium WebDriver, you can interact with a keyboard or mouse using the Actions class. Action class in Selenium provides different methods to perform a series of actions in the browser.

In this blog, we will discuss Actions class and how we can implement its method.

Let’s get started.

What is the Action Class in Selenium?

The Action class in Selenium WebDriver is a utility class that enables advanced user interactions such as mouse and keyboard events. This class provides a way to simulate complex user interactions like double-clicking, right-clicking, holding down a key, etc.

By using the Actions class, you can interact with a website in the same way a user would, allowing you to test more complex interactions that can’t be accomplished with just simple method calls. 

The Actions class provides a fluent interface for building and performing actions, which can be performed on specific web elements.

Methods of Actions Class

Actions class is broadly divided into two categories:

1) Mouse actions

2) Keyboard actions

Mouse Actions

Mouse actions in Selenium are the actions that can be performed using a mouse, such as clicking, double-clicking, right-clicking, dragging and dropping, etc. These actions simulate a user’s interactions with a website through the mouse.

The Actions class in Selenium WebDriver provides the following mouse action:

1) click(): performs a single mouse click on the specified element.

2) clickAndHold(): holds down the left mouse button on the specified element.

3) contextClick(): performs a right-click on the specified element.

4) doubleClick(): performs a double-click on the specified element.

5) dragAndDrop(): performs a drag and drop operation between two elements.

6) release(): releases the left mouse button on the specified element.

7) moveToElement(): moves the mouse cursor to the middle of the specified element.

Keyboard Actions

Keyboard actions in Selenium are the actions that can be performed using a keyboard, such as pressing keys, holding down keys, releasing keys, etc. These actions simulate a user’s interactions with a website through the keyboard.

The Actions class in Selenium provides the following keyboard actions:

1) sendKeys(CharSequence… keysToSend): sends a series of key presses to the specified element.

2) keyDown(Keys theKey): holds down the specified key.

3) keyUp( Keys theKey): releases the specified key.

Implementation of Actions class method

It’s time to start including the Selenium Actions class in your test automation scripts. Follow the steps listed below:

Step 1 – Import the necessary packages

import org.openqa.selenium.WebDriver; import org.openqa.selenium.interactions.Actions;

Step 2 – Create a webdriver instance

System.setProperty(“webdriver.chrome.driver”, “.\\ChromeDriver”); WebDriver driver = new ChromeDriver();

Step 3 – Navigate to the URL of the web page you want to interact

driver.get(“https://www.google.com/”);

Step 4 – Create an instance of the Actions class.

Actions actions = new Actions(driver);

Step 5 – Use the methods of the Actions class to perform actions on the web page

actions.click(element).build().perform();

Step 6 – Close the webdriver after you’re done

driver.quit();

Let’s understand the implementation of each method:

1.   SendKeys

SendKeys() is a method in the Action class in Selenium WebDriver that sends keyboard input to a web page. It simulates keyboard events such as pressing keys, holding keys, and releasing keys. The method takes a string argument representing the keys to be sent to the page.

Here is an example of how sendKeys() can be used in Selenium WebDriver:

Code snippet:

package demo; 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 SendKeysDemo { public static void main(String[] args) { //specify the driver location System.setProperty(“webdriver.chrome.driver”, “.\\chromedriver.exe”); //instantiate the driver WebDriver driver = new ChromeDriver(); //specify the URL of the webpage driver.get(“https://www.google.com/”); //maximize the window driver.manage().window().maximize(); //specify the locator of the search box WebElement element = driver.findElement(By.xpath(“//input[@title=’Search’]”)); //create an object for the Actions class Actions action = new Actions(driver); //pass the product name that has to be searched on the website action.sendKeys(element, “iphone”).build().perform(); //Close the browser driver.quit(); } }

2. Mouse Click

MouseClick() is a method in the Actions class in Selenium WebDriver that simulates a mouse click on an element on a web page. It performs click events on a web page, such as clicking a button or a link. The method takes no arguments and clicks the element that was last targeted by the moveToElement() method.

Code snippet:

package demo; import static org.testng.Assert.assertEquals; 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 MouseClick { public static void main(String[] args) { //specify the driver location System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); //instantiate the driver WebDriver driver = new ChromeDriver(); //specify the URL of the webpage driver.get(“https://www.amazon.in/”); //maximize the window driver.manage().window().maximize(); //create an object for the Actions class Actions action = new Actions(driver); //specify the locator of the search box in which the product has to be typed WebElement elementToType = driver.findElement(By.xpath(“//input[@placeholder=’Search Amazon.in’]”)); //pass the value of the product elementToType.sendKeys(“Iphone14”); //specify the locator of the search button WebElement elementToClick = driver.findElement(By.xpath(“//input[@type=’submit’]”)); //Click on the search button action.click(elementToClick).build().perform(); //verify the title of the website after searching for the product assertEquals(driver.getTitle(), “Amazon.in : Iphone14”); driver.close(); } }

3. MoveToElement

MoveToElement is a method in Selenium’s Actions class. It moves the mouse cursor to a specific element on a web page. By simulating mouse movement, you can perform actions like hover, click, and drag-and-drop. 

This is useful for testing dynamic UI elements, such as drop-down menus, tooltips, and mouse-over effects. To use this method, you’ll first need to create an instance of the Actions class, and then chain the moveToElement method to it.

Code snippet:

package demo; import static org.testng.Assert.assertEquals; import java.util.concurrent.TimeUnit; 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 MouseHover { public static void main(String[] args) { // specify the driver location System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); // instantiate the driver WebDriver driver = new ChromeDriver(); // specify the testSigma URL driver.get(“https://testsigma.com/”); //Maximize the browser driver.manage().window().maximize(); driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); assertEquals(driver.getTitle(), “Open Source & Cloud-based Test Automation Platform for Modern Teams”); //Identify resource object through xpath WebElement element = driver.findElement(By.xpath(“//a[text()=’Resources’]”)); Actions action = new Actions(driver); //mouse hover the Resources element action.moveToElement(element).build().perform(); //Identify the locator for the element Blog and click driver.findElement(By.linkText(“Blog”)).click(); assertEquals(driver.getCurrentUrl(), “https://testsigma.com/blog/”) driver.close(); } }

4. DoubleClick and ContextClick

DoubleClick performs a double-click action on a web page element. This is useful for testing interactive UI elements that respond to double clicks, such as a file or folder in a file explorer.

ContextClick performs a right-click (or context-click) action on a web page element. This is useful for testing context menus that appear on a right-click.

Code Snippet:

package demo; import java.util.concurrent.TimeUnit; 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 DoubleAndContectClick { public static void main(String[] args){ // specify the driver location System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); // instantiate the driver WebDriver driver = new ChromeDriver(); driver.get(“https://demoqa.com/buttons”); //maximise the window driver.manage().window().maximize(); WebElement element = driver.findElement(By.xpath(“//button[text()=’Double Click Me’]”)); Actions action = new Actions(driver); action.doubleClick(element).build().perform(); driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); WebElement element1 = driver.findElement(By.xpath(“//button[text()=’Right Click Me’]”)); action.contextClick(element1).build().perform(); driver.close(); } }

5. DragAndDrop

DragAndDrop is a method in the Selenium Actions class. It simulates the dragging and dropping of an element on a web page. This is useful for testing interactive UI elements that respond to drag-and-drop actions, such as sorting elements in a list or moving an element from one container to another.

Code Snippet:

package demo; import java.util.concurrent.TimeUnit; 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 dragAndDrop { public static void main(String[] args) throws InterruptedException { // specify the driver location System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); // instantiate the driver WebDriver driver = new ChromeDriver(); driver.get(“http://demo.guru99.com/test/drag_drop.html”); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); driver.manage().window().maximize(); Thread.sleep(3000); Actions action = new Actions(driver); WebElement source = driver.findElement(By.xpath(“//*[@id=’credit2′]/a”)); Thread.sleep(1000); WebElement target = driver.findElement(By.xpath(“//*[@id=’bank’]/li”)); action.dragAndDrop(source, target).build().perform(); Thread.sleep(3000); // we have put hard sleep so that the user can see whether the item got dropped to its location or not //close the browser driver.close(); } }

6. KeyUp / KeyDown

keyDown and keyUp are methods in the Selenium Actions class. They simulate the pressing and releasing of keyboard keys. This is useful for testing web pages that respond to keyboard input, such as form fields or game controls.

Code Snippet:

package demo; 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 KeyDown { public static void main(String[] args) throws InterruptedException { // specify the driver location System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); // instantiate the driver WebDriver driver = new ChromeDriver(); driver.get(“https://www.google.com/”); driver.manage().window().maximize(); WebElement element = driver.findElement(By.xpath(“//input[@title=’Search’]”)); Actions action = new Actions(driver); //holds the SHIFT key and converts the text to uppercase action.keyDown(element,Keys.SHIFT).sendKeys(“testSigma”).build().perform(); Thread.sleep(5000); driver.close(); } }

Conclusion

In this article, we have looked at the Selenium Actions class and the many methods for interacting with browsers. Various actions have been demonstrated, including clicking the mouse, inputting text, dragging and dropping, moving to a particular element, double-clicking, right-clicking, copying material, refreshing a page, and changing text’s case, etc. I hope you now recognize the significance of the Selenium Actions class and its applications.

Happy testing!

Frequently Asked Questions

1.  What is the use of build () and perform () in actions?

Build and Perform are methods in the Selenium Actions class. They are used together to create and execute complex user interactions on a web page.

Build is used to create an instance of the Actions class and associate it with a specific WebDriver instance. The methods keyDown, keyUp, click, doubleClick, contextClick, moveToElement, and dragAndDrop can be chained together to create a series of actions. Once you have specified all the actions you want to perform, you can call the build method to build the Actions object.

Perform is used to execute the actions that were built using the build method. Once you call to perform, Selenium will execute the actions in the order that they were specified.

2. Why is the ContextClick () used?

The contextClick method is used to simulate a right-click on an element on a web page. This is useful for testing web pages that have context menus that are triggered by right clicks.

Here’s an example of how to use the contextClick method:

WebElement element = driver.findElement(By.id(“element-id”)); Actions action = new Actions(driver); action.contextClick(element).build().perform();

3. What is the use of release() in actions?

In Selenium, the Action class provides a way to define a series of complex user interactions that simulate a user’s interactions with a web page. The release() method in the Action class is used to release any system resources (e.g. keyboard, mouse) that were acquired during the performance of an action, such as clicking a button or typing into a text box.

The release() method is typically called after the action has been performed, to ensure that the system resources are freed up and can be used by other parts of the application. This helps to avoid resource leaks, which can cause stability and performance problems over time.

In summary, the release() method in the Selenium Action class is used to free up system resources that were acquired during the performance of an action so that they can be used by other parts of the application

imageimage
Subscribe to get all our latest blogs,
updates delivered directly to your inbox.

RELATED BLOGS


Power of POC in Testing: Your Exclusive Guide to Success

TESTSIGMA ENGINEERING TEAM
13 MIN READ
AUTOMATION TESTINGTEST AUTOMATION

Test objects in software testing | Types & How to Create it?

RANJANA KODLEKERE
8 MIN READ
TEST AUTOMATION

How To Write Calculator Test Cases? With Sample Test Cases

AMY REICHERT
13 MIN READ
TEST AUTOMATION