Skip to main content

Quality Assurance

Exploring Selenium 4 Actions API: Part 2

Female It Specialist

Welcome back to our exploration of Selenium 4’s Actions API. In this second part, we’ll delve into advanced techniques and real-world applications of the enhanced Actions API. The focus will be on mouse actions, including click and release, alternate button clicks, double-clicking, moving to elements, and drag-and-drop functionalities. Additionally, we’ll provide insights into when to leverage these advanced actions and their impact on test automation.

 

Advanced Mouse Actions in Selenium 4

  1. Click and Release

The clickAndRelease method is a combination of moving the mouse to the center of an element and pressing and releasing the left mouse button. This simulates a standard click operation.

WebElement clickable = driver.findElement(By.id("clickable"));

new Actions(driver)

    .clickAndRelease(clickable)

    .perform();

 

This method is particularly useful when you want to ensure that a click event is performed without holding the mouse button.

 

  1. Alternate Button Clicks

Selenium 4 supports five defined buttons for a mouse: Left (0), Middle (1), Right (2), X1 (Back – 3), and X2 (Forward – 4). You can perform clicks with these alternate buttons.

Context Click (Right-Click)

WebElement clickable = driver.findElement(By.id("clickable"));

new Actions(driver)

    .contextClick(clickable)

    .perform();

 

Back Click (X1 Button)

PointerInput mouse = new PointerInput(PointerInput.Kind.MOUSE, "default mouse");

Sequence actions = new Sequence(mouse, 0)

        .addAction(mouse.createPointerDown(PointerInput.MouseButton.BACK.asArg()))

        .addAction(mouse.createPointerUp(PointerInput.MouseButton.BACK.asArg()));

((RemoteWebDriver) driver).perform(Collections.singletonList(actions));

 

  1. Double Click

Double-clicking is a common user action, and Selenium 4 simplifies it with the dedicated doubleClick method.

 

WebElement clickable = driver.findElement(By.id("clickable"));

new Actions(driver)

    .doubleClick(clickable)

    .perform();

This method streamlines the process of executing double-click actions in your tests.

 

  1. Move to Element (Hovering)

The moveToElement method moves the mouse to the in-view center point of the element, simulating a hover action.

 

WebElement hoverable = driver.findElement(By.id("hover"));

new Actions(driver)

    .moveToElement(hoverable)

    .perform();

This is particularly useful for scenarios where hovering triggers additional UI elements.

 

  1. Drag-and-Drop

The dragAndDrop method performs a click-and-hold on the source element, moves to the location of the target element, and then releases the mouse.

WebElement draggable = driver.findElement(By.id("draggable"));

WebElement droppable = driver.findElement(By.id("droppable"));

new Actions(driver)

    .dragAndDrop(draggable, droppable)

    .perform();

This is a fundamental action for scenarios involving rearranging elements or moving items between containers.

 

Real-World Applications

Canvas Drawing Applications

For applications where users draw on a canvas, the enhanced Actions API provides better support for handling various pointer events.

Testing Right-Click Functionality

When testing applications with context menus or right-click functionalities, the contextClick method simplifies the testing process.

 

When to Leverage Advanced Mouse Actions

UI Testing with Complex Interactions:

If your web application involves intricate user interactions, leveraging advanced mouse actions in Selenium 4 is beneficial.

Enhancing Test Scenarios:

Consider these actions when your test scenarios require realistic user interactions, such as dragging and dropping elements or simulating right-click functionalities.

 

Conclusion

Mastering Selenium 4’s Actions API opens up new dimensions for crafting expressive and efficient test scripts. The advanced mouse actions provide a more intuitive way to handle user interactions, enhancing the realism of your automated tests.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Jeet Palan

Jeet Palan is an Technical Consultant at Perficient. He has experience in Manual and Automation testing. In addition to this, he is willing to learn different types of testing and likes to know and learn about new trending technologies.

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram