ToolTip Text in Selenium: This is another continuation post of the Java Selenium tutorial series. In this post, we are going to learn how we can verify the tooltip text. Before the discussion about tooltip, I would like to share some basic information about tooltip so that a new learner could understand easily.
What is the Tooltip Text?
The tooltips text is a hidden text which is behind the elements like images, buttons, link, etc. and when you place your mouse over those elements, then some text appears which shares some of the information about that element or object.
For example, if you open facebook.com in your browser and when you place your mouse over the facebook image, then you may see a message “Go to Facebook Home.” which is displays after place your mouse over the logo. So that type of text messages called tooltips text.
Tooltips is traditionally implemented with the “title” attribute, so when you place the mouse of the element where the element is declared with the “title,” then whatever value is assigned to that element, that value will be displayed. Those elements are declared with the title tag. That element will display a static text which gives the pieces of information with no styling.
You can handle the tooltip in 2 different ways:
- Using the “title” Attribute
- Using Actions class
Verify tooltip Using “title” Attribute
For those elements, implement the tooltip using the HTML “title” attribute. Then, in that case, you can use the getAttribute(“title”) method, which will return the information text. So to verify that you can compare the expected text with this text.
Tooltip Text Verify Using Actions Class
So, in that case, if you are thinking about the use of title attribute, then in many cases, there is no title attribute also. It was developed in such a way that when you place your mouse over there, it will generate a div tag, and inside that, you will find out the tooltip text message. So in those cases, you can not use the getAttribute() method. To handle such type of situation, we can take the help of the Actions class to verify the tooltip message.
I hope this article will help you verifying the tooltip message on your application. If you are using any other way or If you are facing any difficulties, then feel free to drop a comment, and we are happy to help you.