How can we handle truncated label or text

Viewed 1780

In order to test few features of the application I need to validate long alphanumeric string. And this string/label/text (whatever you can call here) supposed to truncate once given by user.

I googled and tried to find - 'how to validate truncated string ' and did not find useful info so thought question should be here.

Challenge - I can write up a xpath for long string to validate the tag/label/text/string itself but complex part is string shows few dotes (ex thisIsLongStrin.....) at the end when got truncated.

I would like to make sure that these 'dotes' are displaying when long string is given by user.

Remember I can not see these 'dotes' in page source.

any thoughts or suggestions on this ?

Thanks guys in Advance.........!!!!!

suppose string is : "thisIsLongString"

on page it shows after truncating : "thisIsLongStri..."

this is what working:

//*[@class='abc']//*[contains(text(),'thisIsLongString')]

this is what working:

//*[@class='abc']//*[contains(text(),'thisIsLongStri')]

this is Not working :

//*[@class='abc']//*[contains(text(),'thisIsLongStrin...')]

(since dotes are not part of page)

I am not sure what should be the approach to make sure that these dotes are there.

1 Answers

Truncated text is the result of applying CSS property text-overflow: ellipsis. To check whether text truncated or not, you can use Selenium built-in method:

Python example

element = driver.find_element_by_xpath("//*[@class='abc']//[text()='thisIsLongString']")
assert element.value_of_css_property("text-overflow") == "ellipsis"

P.S. Of course you should also check (if you don't know for sure) whether string is long enough to be truncated as even if text-overflow: ellipsis property is applied, short string will NOT be truncated, so it might looks like

assert element.value_of_css_property("text-overflow") == "ellipsis" and len(element.text) > 20 

This should check whether property applied and length of string is greater than 20 characters...

Related