Selenium Course
- 62k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
So, let’s get started.
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.
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.
There are basically two methods which help in working with the actions in Selenium, namely:
Let us learn about them in detail.
Keyboard interface
Example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL);
Mouse interface
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. You can even check out the details of the testing methodologies tool with the Software testing course online.
To handle these actions class in Selenium, you must follow a proper format.
Related Learning: Drag and Drop in Selenium
Now let’s consider working on a demo to understand how Actions class work.
I will be performing actions on two websites namely edureka.co and jqueryui.com.
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.
Clicks on “Software testing” and navigates to that page.
Performs keyboard functions like KeyUp, KeyDown, and sendKeys().
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:
Drags the corresponding element to the target and drops it.
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.
For details, You can even check out test automation strategies and methodology concepts with the Automation testing training.
Got a question for us? Please mention it in the comments section of “Actions class in Selenium” and we will get back to you.
Course Name | Date | Details |
---|---|---|
Selenium Course | Class Starts on 23rd November,2024 23rd November SAT&SUN (Weekend Batch) | View Details |
Selenium Course | Class Starts on 25th November,2024 25th November MON-FRI (Weekday Batch) | View Details |
Selenium Course | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
edureka.co