i

Selenium Step By Step Guide

Actions Class

We have got a variety of methods available in Selenium to handle events like a simple click() to click on some element, getText() to fetch the text out of an element, and sendKeys() to enter the texts in text boxes. We can handle the drop-down windows and radio buttons effectively.

Now the question comes why do we need to use Actions class then? The answer is, we see some critical situations where basic methods do not work correctly, and we need to have a more reliable solution to handle such complex scenarios such that double-click, drag and drop and element, Mouse hover, and many more.

What is the Actions class:

Selenium provides Actions class, which has compelling methods to solve a variety of complex operations.

Let us see the various methods of the Actions class by creating an instance of the Actions class.

So, we can see that a variety of methods are available under Actions class to automate situations like double-click, Click and hold, drag and drop, Mouse, and keyboard interactions.

 

We can test the login page of https://selflearning.io/login, where a user needs to click the "Shift down"+ "text"+ "Shift up" combination, then this set of sequential operations must be handled by a reliable solution like Actions class. Let us see how to handle this situation using Actions class.

 

Instantiation of Actions class:

We need to instantiate the Actions class first, and then we can perform the sequence of actions on the instance of Actions class.

        Actions action=new Actions(driver);

Performing the Actions sequence:

So overall, we need to see the sequence of the actions:

1-Clicking on the "Shift" key—Using the "keyDown” method of Actions class.

2- Entering text on the text box- Using the "sendKeys” method of Actions class.

3-Releasing the "Shift" key- Using the "keyUp” method of Actions class.

        WebElement eleSearch=driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input"));

   action.keyDown(eleSearch,Keys.SHIFT).sendKeys("Testing").keyUp(eleSearch,Keys.SHIFT);

Now build the sequence:

As we have created a sequence in above step, now we need to build a composite action from all the actions in sequence. We use build() method of Action interface to collect the composite build action of all the actions present in Actions sequence.

        Action builder=action.build();

Perform the composite action:

Now we have built the composite action. We need to perform this composite action by using the perform() method of Action interface.

        builder.perform();

We can also perform the composite action without using the Action interface as below:

action.keyDown(eleSearch,Keys.SHIFT).sendKeys("testing").keyUp(eleSearch,Keys.SHIFT).perform();

Note: Here we can observe that we have not used build() method before using perform() method. This is because the perform() method is also available in Actions class and this method automatically builds the sequence internally, so we do not need to explicitly expose it.