{"id":15178,"date":"2024-02-01T15:17:31","date_gmt":"2024-02-01T09:47:31","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=15178"},"modified":"2024-02-01T15:22:29","modified_gmt":"2024-02-01T09:52:29","slug":"tooltip-in-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/tooltip-in-selenium\/","title":{"rendered":"ToolTip in Selenium"},"content":{"rendered":"\n<p>A tooltip in Selenium is a text that shows up on a web page when the mouse is over an object. The item may take the form of a button, picture, text field, link, etc. When the user hovers the mouse cursor over an object, further information is frequently displayed in the tooltip text.<\/p>\n\n\n\n<p>Traditionally, tooltips were applied to elements as a &#8220;title&#8221; attribute. When the cursor was over this attribute, a tooltip with its value was displayed. This text is static and provides element information without any styling.<\/p>\n\n\n\n<p>Currently, there are a ton of plugins available for using &#8220;tooltips.&#8221; Plugins for JavaScript and JQuery or CSS Tooltips are used to construct advanced tooltips that include styling, rendering, images, and links.<\/p>\n\n\n\n<p>We can utilize the getAttribute(&#8220;title&#8221;) method of the WebElement to retrieve or validate the static tooltips that are implemented using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTML\" rel=\"nofollow noopener\" target=\"_blank\">HTML<\/a> &#8220;title&#8221; attribute. For verification, the tooltip text, the value returned by this method, is compared to the expected value.<\/p>\n\n\n\n<p>We will need to build the mouse hover effect and then retrieve the tooltip for the element using the &#8220;Advanced User Interactions API&#8221; that the Web Driver provides for other tooltip implementations.<\/p>\n\n\n\n<p>It is a typical occurrence on many web pages for links, text, or occasionally images to appear when hovered over. For instance, when a user hovers over the menu selections on the right side of their Gmail inbox, a little text-filled &#8220;hover box&#8221; appears. We refer to this as a tooltip of web elements.&nbsp;<\/p>\n\n\n\n<p>This text often provides a succinct explanation of the object&#8217;s functionality, however occasionally it provides a more in-depth description. It might occasionally just show the object&#8217;s entire name. In summary, a tooltip&#8217;s main function is to give the user some context for the object. It is often necessary to confirm that this text description is being shown as intended.<\/p>\n\n\n\n<p>It is necessary to retrieve and validate the text within the tooltip due to the above-mentioned reasons. There are various ways to accomplish this depending on how the tooltip is added to the HTML. Check out the <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\/\">Selenium training online<\/a> to learn more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>A Brief of the Advanced User Interactions API<\/strong><\/h2>\n\n\n\n<p>The API for Advanced User Interactions allows users to perform keyboard and mouse operations on a webpage, including drag and drop, hovering, multi-selection, key press and release, and other similar operations.<\/p>\n\n\n\n<p>Let&#8217;s look at the classes and methods we&#8217;ll need to employ in order to move a slider element by an offset now.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> The following packages and classes must be imported in order to access the API.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Build the user action sequence by creating an object of the &#8220;Actions&#8221; class. The user action sequence, such as dragAndDrop() and moveToElement(), is constructed using the Actions class. API offers a variety of functions pertaining to user actions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The constructor of the driver object takes the driver object as an argument.<\/strong><\/h2>\n\n\n\n<p><strong>Step 3:<\/strong> Use the &#8220;Actions&#8221; class&#8217;s build() function to create an Action Object. To run every action created by the Actions object (builder in this case), call the perform() function.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/SGg3Q0j4gbx1T0cAax5OIvCJg5HG--EpnHARq1WfYs1hEoB8rsdYxkeCH9tHDURfW4EQPEdks1itiZVoEuZPfqxPwPqZQ-EiTReM8wZOciLZlr_4aITkWn0r2R6nCufSIrAFkdF7jQogAIK7n-_QHg\" alt=\"ToolTip in Selenium\" title=\"\"><\/figure>\n\n\n\n<p>We have now demonstrated the usage of a few of the API&#8217;s user Actions functions, including clickAndHold(element), moveByOffset(10,0), and release(). The API offers a ton of these techniques.<\/p>\n\n\n\n<p>Let&#8217;s examine the following cases:<\/p>\n\n\n\n<p><strong>Case 1:<\/strong> When the &#8216;title&#8217; attribute contains the tooltip. We can get the tooltip from here. By tactic<\/p>\n\n\n\n<p><strong>Case 2:<\/strong> When &#8216;div&#8217; has the tooltip accessible. Here, we can use the Actions class methods to fetch the tooltip.<\/p>\n\n\n\n<p>We&#8217;ll talk about both cases. First, let&#8217;s examine the case where the tooltip appears in the title attribute.<\/p>\n\n\n\n<p>When a user hovers over an HTML object, ToolTip appears. This is also evident when using the browser&#8217;s Developer tool, where the tooltip text is displayed as a value assigned to the &#8216;title&#8217; attribute.<\/p>\n\n\n\n<p>Most of the time, getting the tooltip text is rather simple. Finding the site Selenium element for which a tooltip has to be retrieved is all that is required.<\/p>\n\n\n\n<p>WebElement webElement = driver.findElement(Any By strategy &amp; locator);<\/p>\n\n\n\n<p>Any By technique, such as discovering an element by its id, name attribute, etc., can be used in this situation to find the WebElement.&nbsp;<\/p>\n\n\n\n<p>Now, get the webElement&#8217;s &#8220;title&#8221; attribute value.<\/p>\n\n\n\n<p>String tooltipText = webElement .getAttribute(&#8220;title&#8221;);<\/p>\n\n\n\n<p>The text from the tooltip will be saved in the tooltipText variable after the aforementioned statement is executed.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/1xczYvzL4tkkkyDiAix1XFzxTPxxyJZQrsfZLi326He0A7oLJuw_pcWdO47srUwEHePHhBb8d8yt9wOMKRaA5awxqqVp6jwXhfSZKe4yogYvBLXopnIFpOqPpnbPpDpUQRZb3OgJVPQnM47fcIweNg\" alt=\"ToolTip in Selenium\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to capture a tooltip in Selenium using Actions Class?<\/strong><\/h2>\n\n\n\n<p>The HTML page can display ToolTip in a variety of ways. It can be positioned inside the title tag, as was previously described. In a similar manner, it can be removed in many ways. Above, we simply used Selenium&#8217;s GetAttribute function to locate the title element and read its property. The Div element can also be used to show data in the same manner.<\/p>\n\n\n\n<p>However, this time we make an effort to emulate the actions of every other user on the page. Try to read the tooltip&#8217;s explanation by hovering your cursor over the object. And the only way to accomplish this is with Selenium&#8217;s Actions Class.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong>&nbsp;To learn more about ToolTips, check out the <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\/\">Selenium online training<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A tooltip in Selenium is a text that shows up on a web page when the mouse is over an object. The item may take the form of a button, picture, text field, link, etc. When the user hovers the mouse cursor over an object, further information is frequently displayed in the tooltip text. Traditionally, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15184,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-15178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/15178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=15178"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/15178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/15184"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=15178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=15178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=15178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}