We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Combine multiple actions into a single complex sequence using the builder pattern
In this practice lab, you will learn how to chain multiple actions together into a single, atomic sequence using the Actions class builder pattern.
Instead of calling perform() after every single action, you can:
perform() callLess efficient approach:
perform() is a separate network call to the browserEfficient approach:
perform()
Builds the action sequence and immediately executes it. This is what you'll use 99% of the time.
build()
Only builds the action sequence without executing it. Rarely used—typically for very advanced scenarios or custom action handling.
Imagine a login form where you need to perform a complex action sequence:
No actions yet. Interact with the form or click "Simulate Chain"
Interact with the form to see individual actions, or click "Simulate Chain" to see a complete chained sequence!
If you were doing this manually:
Your Selenium code will chain all these actions into a single atomic sequence using the builder pattern.
123456789101112131415161718WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
Actions actions = new Actions(driver);
// Chained action sequence - all executed atomically
actions
.moveToElement(username)
.click()
.sendKeys("testuser")
.keyDown(Keys.TAB)
.keyUp(Keys.TAB)
.sendKeys("mySecurePassword123")
.keyDown(Keys.ENTER)
.keyUp(Keys.ENTER)
.perform();
driver.quit();perform() yet—we're still building the chain.Group logically related actions into a single chain with one perform(). This makes your tests faster, more atomic, and easier to read.
Format your chained actions with each action on its own line. This makes it easy to see the sequence of user interactions at a glance, like reading a script.
While chaining is powerful, don't chain unrelated actions together. If you need to verify state between actions, use separate chains with assertions in between.