About this question
I am currently designing a test scenario using Selenium. How can I differentiate between using “action” and “actions” in my test scripts?
I am currently designing a test scenario using Selenium. How can I differentiate between using “action” and “actions” in my test scripts?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask QA Expert
Answered on Apr 17, 2024
In the context of selenium, here is the difference given below between action and actions in selenium:-
Action refers to a single interaction with an element, like clicking a button or entering text into a field. It is generally used with methods like click(), sendkeys(), etc.
Actions refer to mainly the series of Interactions that are performed as a sequence. It is generally used for operations that are complex like drag and drop, mouse hovering or even performing key combinations.
Here is the example given which would show the use of action and actions in selenium WebDriver by using the Java programming language:-
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 SeleniumActionsExample {
Public static void main(String[] args) { // Set the path to the chromedriver executable
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
// Open the webpage
Driver.get(https://example.com);
// Find the element to interact with using “action”
WebElement buttonElement = driver.findElement(By.id(“myButton”));
// Perform a single action – clicking the button
Actions action = new Actions(driver);
Action.click(buttonElement).build().perform();
// Pause for 2 seconds to see the result
Try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Find source and target elements for a drag-and-drop operation using “actions”
WebElement sourceElement = driver.findElement(By.id(“source”));
WebElement targetElement = driver.findElement(By.id(“target”));
// Perform a series of actions – drag-and-drop from source to target
Actions actions = new Actions(driver);
Actions.moveToElement(sourceElement)
.clickAndHold()
.moveToElement(targetElement)
.release()
.build()
.perform();
// Pause for 2 seconds to see the result
Try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Close the browser
Driver.quit();
}
}Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step QA Testing guides from industry experts
Common QA Testing interview questions, answered
Guides, tips and career advice on QA Testing from JanBask experts.
Selenium Selenium Tutorial: Complete Guide to Automation Testing for Beginners
The Selenium tutorial covers both fundamental & advanced Selenium concepts. It is for individuals at all levels. Learn about…
QA Testing Software Testing Basics: Fundamentals, Principles, Lifecycle & Best Practices
Learn software testing basics, including key principles, lifecycle stages, types of testing, and best practices. A complete…
QA Testing You Can’t Miss the Top 20 Security Testing Interview Questions
Are you getting ready for your information security interview? Get a hold of the top 20 information security interview questions…