Action chain execution (perform) in Selenium Java - Build an Automation Script
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; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.Assert; import java.time.Duration; public class DragAndDropTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); try { driver.get("https://example.com/drag_and_drop_page"); // Wait for draggable element WebElement draggable = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("draggable"))); // Wait for droppable element WebElement droppable = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("droppable"))); Actions actions = new Actions(driver); actions.clickAndHold(draggable) .moveToElement(droppable) .release() .perform(); // Verify the droppable text changed to 'Dropped!' String dropText = droppable.getText(); Assert.assertEquals("Dropped!", dropText); System.out.println("Test Passed: Drag and drop performed successfully."); } catch (Exception e) { System.out.println("Test Failed: " + e.getMessage()); } finally { driver.quit(); } } }
This test script uses Selenium WebDriver with Java to automate a drag and drop action.
First, it opens the browser and navigates to the test page.
It waits explicitly for the draggable and droppable elements to be visible using WebDriverWait and ExpectedConditions.
Then, it creates an Actions object to build the action chain: click and hold the draggable element, move it to the droppable element, and release the mouse button.
The perform() method executes the chain.
Finally, it asserts that the droppable element's text changed to 'Dropped!' to confirm the action succeeded.
The test prints a pass or fail message and closes the browser.
Now add data-driven testing with 3 different draggable and droppable element pairs on the page
Practice
perform() method do in Selenium's Action Chains?Solution
Step 1: Understand the role of
Theperform()perform()method triggers the execution of all the actions queued in the action chain.Step 2: Differentiate from other methods
Other methods likebuild()prepare the chain, butperform()actually runs it.Final Answer:
It executes all the actions added to the chain in order. -> Option DQuick Check:
perform()runs actions [OK]
- Confusing perform() with build()
- Thinking perform() initializes the chain
- Assuming perform() clears actions
Solution
Step 1: Review correct method chaining syntax
In Selenium Java, you add actions likeclick(element)and then callperform()to execute.Step 2: Identify the correct method names
perform()is the method to run the chain;execute()andrun()do not exist.Final Answer:
actions.click(element).perform(); -> Option AQuick Check:
Action then perform() [OK]
- Calling perform() before adding actions
- Using non-existent methods like execute()
- Incorrect method order
Actions actions = new Actions(driver); actions.moveToElement(menu).click(subMenu).perform();
Solution
Step 1: Analyze the action chain sequence
The code first moves the mouse to the 'menu' element, then clicks on the 'subMenu' element.Step 2: Understand method chaining effect
Actions are executed in order: moveToElement, then click, then perform runs them.Final Answer:
The mouse moves to 'menu' element and clicks on 'subMenu' element. -> Option BQuick Check:
moveToElement then click then perform [OK]
- Assuming click happens before moveToElement
- Thinking only one element is affected
- Believing code has syntax errors
Actions actions = new Actions(driver); actions.click(element); actions.perform();
Solution
Step 1: Check method chaining and object usage
The code adds a click action, then calls perform() on the same actions object, which is valid.Step 2: Confirm no syntax or logical errors
Calling perform() separately after adding actions is allowed and executes the chain.Final Answer:
There is no error; the code will work as expected. -> Option CQuick Check:
Separate perform() call is valid [OK]
- Thinking perform() must be chained immediately
- Assuming new Actions object needed for perform()
- Believing code has syntax errors
Solution
Step 1: Understand the required sequence of actions
The task is to hover (moveToElement) over 'menu', then right-click (contextClick) on 'subMenu', then double-click (doubleClick) on 'option'.Step 2: Match methods to actions and order
actions.moveToElement(menu).contextClick(subMenu).doubleClick(option).perform(); correctly chains moveToElement(menu), contextClick(subMenu), and doubleClick(option), then calls perform() to execute.Final Answer:
actions.moveToElement(menu).contextClick(subMenu).doubleClick(option).perform(); -> Option AQuick Check:
Hover, right-click, double-click in order [OK]
- Mixing order of actions
- Using click instead of contextClick for right-click
- Calling perform() before all actions
Ready to prove it?
Pass the test and this pattern moves unknown
