Actions Class in Selenium Webdriver: From our previous tutorials, if you have checked, we have covered various topics like WebDriver Element Methods to click a button using click() and submit() method. Also, we have discussed in our earlier articles like how to handle dropdown values using Selenium.
After discussed the above topics now, we are moving towards another important concept of Java Selenium WebDriver that is Action class. Action class can handle various types of Keyword and mouse operations. So, in this post, we are going to learn about the Selenium Action class, and with this, how can we interact with the web applications.
What is the Actions class in Selenium?
Selenium Action class is an API that helps us in performing various complex user web interactions like a double click, right-click, and drag and drop operations, etc. You can get a huge number of methods which we can use for performing various operations.
What Is an Action Interface?
In Selenium WebDriver, you can get Actions class and Action Interface. When you want to perform a single operation, then that time, you can use the Action interface, and when your requirement is a series of actions, then you can use the Actions class.
How to Use Actions class in Selenium?
Let us we try to understand the step by step of how to use Action class With Selenium WebDriver
- Import Packages
- Create Actions class Object
- Generate The Actions Sequence
- Build The Actions sequence
- Perform the actions sequence
Now let us go through each step and try to understand what we have to do in each step to successfully use the actions class in our automation script.
Import Packages:
As we all know, to implement something in our automation script, we have to import the packages for that, then only we can use them in our script. Action and Actions class are defined inside the below packages, so we have to import them in our script.
import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Action;
Create Actions class Object
As we have mentioned in the image that there are so many methods that are present inside the Actions class. So to use those methods, we need first to create an object of Actions class, and with the help of that object, we can able to call those methods to perform different operations. So to instantiate the Actions class, we need the WebDriver object. Here is the below syntax for Instantiate Actions class:
Actions action = new Actions(webdriver object);
Generate actions sequence:
To achieve our requirement, we are performing some series of actions. For example, suppose you want to close a browser, so for that in the keyboard, you have press CTRL + F4. Hence for doing such operations, Actions class provides some methods like keyDown,sendKeys, and keyUp, etc.
If you have to think about CTRL + F4 for close, you Need t
- Press CTRL Key
- Press F4 key
- Release CTRL key
So for that, if you have created a Sequence, then that should look something like below
- actions.keyDown(Keys.ALT);
- actions.sendKeys(Keys.F4);
- actions.keyUp(Keys.ALT);
Build the actions sequence
In the above step, we have created a sequence of actions. Now the next thing is we have built the sequence using the build() method of actions class. This build() method will generate composite actions by containing all the actions which we have generated previously.
Action action = actions.build();
Perform actions sequence
The final step is to perform the action, and you can do this by calling the perform() method of Action interface. After the execution of this statement, you can see the action in the browser.
action.perform();
Actions class Methods of Selenium
In Actions class, we can get various methods, but according to the behavior, it is categorized into 2 types:
- Keyboard Events
- Mouse Events
Keyboard Events Methods
- sendKeys(keystone): sends a series of keystrokes onto the element
- keyDown(theKey): Sends a keypress 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 events Methods
- click (): click on the element
- doubleClick (): Double clicks onElement
- contextClick(): Performs a context-click (right-click) on an element
- clickAndHold(): Clicks at the present mouse location (without releasing)
- dragAndDrop(source, target): Invokes click-and-hold 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, then frees the mouse. xOffset – to shift horizontally, yOffset – 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 depressed left mouse button at the existing mouse location
In our Next Articles, we are going to discuss other operations which we can perform through Actions class:
- How to handle mouse hover actions using Actions in Selenium
- How to do Drag and Drop using Actions in Selenium
- How to Scroll Web Page Down or Up Using Selenium
- How to Perform Context Click / Right Click using Actions in Selenium
- How to Perform Double Click using Actions in Selenium
How to Click Element Using Action Class In Java Selenium Program?
package com.selenium.actions; 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.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Action_Click { WebDriver driver=new FirefoxDriver(); Actions act=new Actions(driver); @BeforeTest public void openwebsite() { driver.manage().window().maximize(); driver.get("https://www.google.co.in/"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); } @Test public void operation() throws InterruptedException { WebElement Element=driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")); Actions act=new Actions(driver); act.moveToElement(Element).contextClick().build().perform(); Thread.sleep(5000); act.sendKeys(Keys.DOWN,Keys.ENTER).build().perform(); //act.click().build().perform(); } @AfterTest public void close() throws InterruptedException { Thread.sleep(5000); driver.close(); } }
How to Do Double Click Using Action Class In Java Selenium Program?
At the time of automate a web application, we may need to double click on some element. So, in this post, we are going to learn how we can do double click an element with the help of action class with a simple example.
In our previous post regarding action, the class uses in selenium we have mentioned that by the help of Action class we can handle the keyboard and mouse actions.
Scenario to be automated:
In this example we are going to automate the below scenario:
1. Open the Google.com in a browser
2. Find the Gmail Link on the Webpage and do the double click on the element
3. Close the browser
Automation Script Of Above Scenario:
package com.selenium.actions; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Action_ContextClick { WebDriver driver=new FirefoxDriver(); Actions act=new Actions(driver); @BeforeTest public void openwebsite() { driver.manage().window().maximize(); driver.get("https://www.google.co.in/"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); } @Test public void operation() throws InterruptedException { WebElement Element=driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")); act=new Actions(driver); act.moveToElement(Element).build().perform(); Thread.sleep(5000); act.click().build().perform(); } @AfterTest public void close() throws InterruptedException { Thread.sleep(5000); driver.close(); } }
If you are still struggling to understand the above program then you can comment us in the comment section or you can use our contact us page by which you can drop your concern and we will try to clear your confusion as soon as possible.
How to Move Specific Element Using Action Class in Java Selenium Program?
package com.selenium.actions; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Actions_Dummy { public static void main(String[] args) throws Exception { WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://flipkart.com/"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); WebElement we=driver.findElement(By.xpath(".//*[@id='container']/div/header/div[2]/div/ul/li[3]/a/span")); Actions act=new Actions(driver); act.moveToElement(we).build().perform(); } }
How to Mouse Operation Using Action Class In Java Selenium Program?
package com.selenium.actions; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Action_MouseMove { /* Some Time You Got WebDriverException: Permission Denied When Ur Browser is not Compatible*/ WebDriver driver=new FirefoxDriver(); Actions act=new Actions(driver); @BeforeTest public void openwebsite() { driver.manage().window().maximize(); driver.get("http://apps.qaplanet.in/qahrm/login.php"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); } @Test public void operation() throws InterruptedException { driver.findElement(By.name("txtUserName")).clear(); driver.findElement(By.name("txtUserName")).sendKeys("qaplanet1"); driver.findElement(By.name("txtPassword")).clear(); driver.findElement(By.name("txtPassword")).sendKeys("user1"); driver.findElement(By.name("Submit")).click(); WebElement Element=driver.findElement(By.xpath(".//*[@id='leave']/a/span")); Actions act=new Actions(driver); act.moveToElement(Element).build().perform();; WebElement Element2=driver.findElement(By.xpath(".//*[@id='leave']/ul/li[3]/a/span")); act.moveToElement(Element2).build().perform(); } @AfterTest public void close() throws InterruptedException { Thread.sleep(5000); driver.close(); } }
How to Press Tab Key Using Action Class In Java Selenium Example Program?
package com.selenium.actions; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Action_Keyboard { WebDriver driver=new FirefoxDriver(); Actions act=new Actions(driver); @BeforeTest public void openwebsite() { driver.manage().window().maximize(); driver.get("http://www.google.com"); driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS); } @Test public void operation() throws InterruptedException { act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); act.sendKeys(Keys.TAB).build().perform(); //act.sendKeys(Keys.TAB).build().perform(); act.sendKeys("www.indjobupdate.in").build().perform(); Thread.sleep(3000); } @AfterTest public void close() throws InterruptedException { Thread.sleep(5000); driver.close(); } }
Leave a Reply