testsigma
left-mobile-bg

Action Class in Selenium: Its Method and Implementation

October 24, 2024
Shreshtha Gokhru
right-mobile-bg
Action Class in Selenium: Its Method and Implementation
image

Start automating your tests 10X 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 web 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:

<styled-code>
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();  	
            		}
     		}
</styled-code>

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:

<styled-code>
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();
   	}
 
}
</styled-code>

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.  Read here about mouse hover in selenium.

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:

<styled-code>
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 &amp; 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();
   	}
 
}
</styled-code>

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:

<styled-code>
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();
   	}
 
}
</styled-code>

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:

<styled-code>
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();
   	}
 
}
</styled-code>

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:

<styled-code>
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();
   	}
 
}
</styled-code>

Explore Testsigma, a low code test automation tool, to automate your end-to-end UI tests for web, mobile, desktop and APIs, from the same place, 10x faster.

Start here

When to look for an alternative to Selenium and how to do the same actions using Testsigma?

While Selenium is a powerful and widely used tool for web automation, you should consider an alternative to Selenium when there are certain scenarios like:

  1. Complex Test Scenarios:

If your testing requires handling dynamic content or complex user interactions that Selenium struggles with, consider alternatives.

  1. Maintenance Overhead:

If maintaining your Selenium scripts is becoming burdensome and time-consuming, you might want to look for simpler solutions.

  1. Lack of Built-in Features:

If you need features like cross-browser testing, visual testing, or easy integration with CI/CD tools that require additional plugins or libraries in Selenium, alternatives may offer these out of the box.

  1. Team Skills:

If your team lacks expertise in coding or programming languages that Selenium requires.

  1. Need for Collaborative Testing:

If your team requires a more collaborative approach to testing, where non-technical stakeholders can contribute, Testsigma’s user-friendly interface may be beneficial.

If you’ve decided to explore Testsigma as an alternative to Selenium, here’s a general approach:

Test Case Creation

  • Selenium: Requires coding skills to define test cases using programming languages like Java, Python, or C#.
  • Testsigma: Offers a codeless approach, allowing users to create test cases using simple English language instructions.

Test Execution

  • Selenium: Tests are executed by running scripts.
  • Testsigma: Provides a user-friendly interface for executing tests with a simple click, and also offers features for managing test environments.

Cross-Browser Testing

  • Selenium: Requires setting up and managing drivers for each browser.
  • Testsigma: Handles cross-browser testing automatically, providing access to multiple browsers without requiring additional configuration.

Integrations

  • Selenium: Integration with CI/CD tools often involves manual configuration.
  • Testsigma: Offers seamless integration with popular CI/CD tools, simplifying the setup and monitoring of test runs.

Reporting and Analytics

  • Selenium: Reporting typically requires integration with 3rd party reporting tools.
  • Testsigma: Provides built-in reporting features with real-time analytics and dashboards for tracking test results.

Overall, Testsigma offers several advantages over Selenium!

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.

How to Handle Multiple Tabs in Selenium
Selenium: Managing Tabs and Windows Like a Pro
Run Selenium Tests in Docker
How to Run Selenium Tests in Docker
Scrolling in Selenium
How To Scroll a Page Using Selenium WebDriver with Java?
Testsigma Author - Shreshtha Gokhru

Shreshtha Gokhru

An astute professional with over 8+ years of experience in Software testing, Agile methodologies, and product sales. I am also keen to invest in the Stock market and Mutual funds. Apart from that, I like travelling and playing cricket.

image

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

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

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


How to Write Test Cases for Trading Application Testing?
RITIKA KUMARI
TEST AUTOMATIONTESTING DISCUSSIONS
Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION