Testing With Selenium WebDriver (68 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (58 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to handle Actions class in Selenium WebDriver?

Published on May 31,2019 9.5K Views
5 / 14 Blog from Selenium Webdriver

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Actions play a vital role in testing an application using Selenium. In order to perform any operations on the web application like accessing the drop-down box, double click, right click and many more, actions class is a necessity. On that note, I’m writing this article on how to handle the Actions class in Selenium to help you understand how actions work.

I’ll be discussing this topic in the following sequence:

So, let’s get started.

What is the Actions class?

Actions class is a built-in ability to handle various types of keyboard and mouse events. In Selenium Webdriver, handling these events including operations such as drag and drop or clicking on multiple elements with the help of the control key are done using the advanced user interactions API. It mainly consists of Actions which are needed while performing various operations.

Actions-Class-in-Selenium - Edureka

This Actions class is the user-facing API for emulating complex action events. You can directly use this class rather than using the input devices, i.e. keyboard and mouse.


Actions action = new Actions(driver);
action.moveToElement(element).click().perform();

I will use perform() to execute the actions.

Using this Actions API, keyboard interactions can be easily handled by the  WebDriver. To use mouse actions, I will use the current location of the web element and then perform some kind of operation.

Now, let us learn about the different methods under this class that are used to perform some actions.

Different methods under Actions class

There are basically two methods which help in working with the actions in Selenium, namely:

  • Keyboard interface
  • Mouse interface

Let us learn about them in detail.

Keyboard interface

  • sendKeys(keysToSend): Sends a series of keystrokes onto the element.
  • keyDown(theKey): Sends a key press without release it. Subsequent actions may assume it as pressed.

Example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL);

  • keyUp(theKey): Performs a key release.

Mouse interface

  • click(): Clicks on the element.
  • doubleClick (): Double clicks on the element.
  • contextClick() : Performs a context-click (right-click) on the element.
  • clickAndHold(): Clicks at the present mouse location without releasing.
  • dragAndDrop(source, target): Clicks at the source location and moves to the location of the target element before releasing the mouse. source (element to grab, target – element to release).
  • dragAndDropBy(source, xOffset, yOffset): Performs click-and-hold at the source location, shifts by a given offset value, then frees the mouse. (X offset – to shift horizontally, Y Offset – to shift vertically).
  • moveByOffset(x-offset, y-offset): Shifts the mouse from its current position (or 0,0) by the given offset. x-offset – Sets the horizontal offset (negative value – shifting the mouse to the left), y-offset – Sets the vertical offset (negative value – shifting the mouse to the up).
  • moveToElement(toElement): It shifts the mouse to the center of the element.
  • release(): Releases the pressed left mouse button at the existing mouse location.

Once you get a proper idea about the different methods under the Actions class, it will be easy to implement a use case of the same.

How to handle actions class in Selenium

To handle these actions class in Selenium, you must follow a proper format.

  • Create an object of the Actions class ‘action‘ 
  • Focus on the element using WebDriver: action.moveToElement(element).build().perform();
  • Build().perform() is used to compile and execute the actions class.
  • Use the different methods under the actions class to perform various operations like click(), drag and drop and so on.

Now let’s consider working on a demo to understand how Actions class work.

Demo

I will be performing actions on two websites namely edureka.co and jqueryui.com.

  • Download the Java libraries on to your system.
  • The first thing to do when you want to work with Selenium is to make sure you know how to install it to your system.
  • An IDE to work on. I prefer working on the Eclipse IDE because it is user-friendly and it is easy to execute Java projects.
  • Select the browser driver of your choice.
  • Get the URL of the web page that you want to test.
  • Perform corresponding actions like clicking the button, mouse move, key press and so on.

Refer this code to understand how actions are helpful while testing an application.


package seleniumdemo;

import java.util.concurrent.TimeUnit;
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.Action;
import org.openqa.selenium.interactions.Actions;

public class Selenium {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.edureka.co/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.id("header_topcat"))).build().perform();
Thread.sleep(3000);
WebElement link = driver.findElement(By.cssSelector("#software-testing-certification-courses"));
builder.moveToElement(link).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//ul[@class='dropdown-menu']//li//a[text()='Software Testing']")).click();
Thread.sleep(4000);
WebElement act = driver.findElement(By.id("search-inp"));
builder.moveToElement(act).build().perform();
Thread.sleep(3000);
WebElement search = driver.findElement(By.xpath("//span[@class='typeahead__button']"));
builder.moveToElement(search).build().perform();
Thread.sleep(3000);
Action seriesOfActions;
seriesOfActions = builder
.sendKeys(act, "Selenium")
.keyDown(search, Keys.SHIFT)
.keyUp(search, Keys.SHIFT)
.build();
seriesOfActions.perform();
Thread.sleep(3000);
driver.quit();
}

}

The output for the above code is depicted below:

Searches for the drop down and clicks on it and then perform actions on the drop down.

Dropdown - Actions class in Selenium - Edureka

Clicks on “Software testing” and navigates to that page.

MouseClick - Actions class in Selenium - Edureka

Performs keyboard functions like KeyUp, KeyDown, and sendKeys().

Keyboard functions - Actions class in Selenium - Edureka

To perform drag and drop, I have considered this website which solely deals with drag and drop, jqueryui.com.


package seleniumdemo;

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 demo {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().window().maximize();
driver.switchTo().frame(0);
WebElement SourceElement= driver.findElement(By.id("draggable"));
WebElement TargetElement= driver.findElement(By.id("droppable"));
Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(SourceElement, TargetElement).build().perform();
//action.clickAndHold(SourceElement).moveToElement(TargetElement).release().build().perform();
Thread.sleep(3000);
driver.quit();

}

}

The output for the above code is depicted below:

 

Drag - Actions class in Selenium - Edureka

Drags the corresponding element to the target and drops it.

Drop - Actions class in Selenium - Edureka

With this, we come to an end to this “Actions class in Selenium” blog. I Hope you guys enjoyed this article and understood what are the actions and what are the different methods under these actions class in Selenium. Now that you have understood how to perform actions in Selenium, check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software.

Got a question for us? Please mention it in the comments section of “Actions class in Selenium” and we will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.