How to Work with Hovers
Intro
If you need to work with mouse hovers in your tests, it might not be obvious how to do this with Selenium. A quick search through the official Selenium documentation can also be confusing, and might leave you scouring online forums and search engines for the solution.
A Solution
The most popular use case for a hover action is to display additional menu options(submenu.)
By leveraging Selenium's Action Builder we can handle more complex user interactions like hovers. This is done by telling Selenium which element we want to move the mouse to, and then performing what we need to after.
Example
Our example application is available here on the-internet. It has a few avatars displayed in a grid layout. When you hover over an avatar, additional user information appears in a caption.
We're going to write a test that will hover over the first avatar and make sure that this additional information appears. After loading the page we find the first avatar, and its caption within it. We check that the caption is not displayed yet, use Selenium's Actions API to move the mouse to the avatar (which triggers the hover), and then check that the caption is displayed.
Expected Behavior
- Open the browser
- Visit the page
- Hover over the first avatar
- Assert that the caption appeared on the page
- Close the browser
Summary
Hopefully this will help you handle more complex user interactions like hovers.
Happy Testing!