Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver - Java - Verify partial text display with three dots at the end

The following text is displayed in a webpage.

screenshot

The HTML code is as follows -

<tr>
  <td valign="middle" align="left">
    <div 
      class="ellipsis" 
      style="max-width: 425px; display: inline-block;">
      HKCR\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop
   </div>
 </td>

Using Selenium WebDriver with Java, I want to check that only partial text is displayed with 3 dots at the end. From the HTML code, I'm only able to see the full text

HKCR\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop

instead of partial text with 3 dots at the end. When I wrote the following code, it outputs the full text instead of partial text with 3 dots.

String text1 = webDriver.findElement(
  By.xpath(".../tr/td[1]")
).getText();

So, I'm confused as to how to verify that only partial text is actually displayed. Can someone please help?

like image 979
user3657330 Avatar asked Mar 01 '26 00:03

user3657330


1 Answers

The issue here is that Selenium detects "visibility" in a way that doesn't capture text overflow hidden by ellipsis. Visibility is looking the absence of a set of criteria, listed here in the webdriver specs, and the CSS property/value "text-overflow: ellipsis" is not one.

You can still test that the elements on your page are using this CSS property. Use Selenium to verify that the divs you want have the CSS text-overflow value you want them to have with .getCssValue(), for example,

String cssvalue = Login.driver.findElement(By.xpath(".//*[@id='ccleaner_options_exclude_div']/table/tbody/tr/td/div")).getCssValue("text-overflow");

and check that the value is "ellipsis". As long as that value is set, in theory, the browser should be truncating the text and adding an ellipsis before it overflows!

like image 178
NoSuchElephantException Avatar answered Mar 02 '26 14:03

NoSuchElephantException