WebUI.getText not trimming trailing space

staff-20

#1

I’ve come across an issue where WebUI.getText is not able to trim the trailing space in actual text.

The actual text rendered in webpage HTML has &nbsp at the end. This is causing the trailing space. I thought WebUI.getText will take care of it but it didn’t work.

Example:
Actual Text in webpage HTML: sample text 1&nbsp
Actual Text grabbed by WebUI.getText : "sample text 1 "
Expected Text in Test Case: “sample text 1

Expected Fix: WebUI.getText must remove all leading and trailing whitespace, including HTML entities like     etc.


[WebUI] Get Text
#2

And is it a bug? getText should pick innerHTML attribute of the element - as-is - including all characters (special chars as well)

Note: If a <div> , <span> , or <noembed> node has a child text node that includes the characters (&) , (<) , or (>) , innerHTML returns these characters as the HTML entities "&amp;" , "&lt;" and "&gt;" respectively. Use Node.textContent to get a raw copy of these text nodes’ contents.

The same would be probably valid for non-breakable space character, so you may need to use

WebUI.getText(xxx).replace("\u00a0","")

\u00a0 is a hexadecimal representation of nbsp character.


#3

The description for WebUI.gettext says -

Get the visible (i.e. not hidden by CSS) inner text of the web element, including sub-elements, without any leading or trailing whitespace.

So my understanding is it’ll get all text and if comes across any leading or trailing whitespace (even due HTML entities), then it’ll remove that whitespace.

Is my understanding wrong ?
Is there any way to get around this? I need to compare multiple values in form like this and the test case keeps failing due to trailing space.


#4

Either the code or the documentation is buggy.


#5

Well, the description seems to be misleading in this case - maybe regular spaces are trimmed, but special characters such as nbsp keeps untouched.

I’ve posted one possible workaround above.

WebUI.getText(xxx).replace("\u00a0","")

It replaces nbsp character by empty string - if your text contains more than one nbsp, you may need to use String.replaceAll()

Edit:
I’ve just reviewed the source code of WebUI.getText() keyword and it just calls WebElement.getText() from Selenium library, no more magic is happening there. And WebDriver’s API doc says this:

getText

java.lang.String getText()

Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.

Returns:

The visible text of this element.


#6

Moving to correct category.


#7

I found a workaround.

Thanks Mareck_Meloick,

Knowing what gettext method is doing, I was able to get around this issue by using trim method.

java.lang.string.trim()

This is what I did.
WebUI.getText(xxx).trim()


#8

For some reason, I can’t use the replace function. Katalon just doesn’t seem to recognize it as a function. Is there a step that I a mmissing?


#9

Hi @marias

Can you give an example code of what you’re trying co callreplace() on >


#10

It’s working for me. thanks


#11

I’m finding that the trimming of white space on calls to WebUI.verifyElementText() only works when test cases are run in Chrome, Firefox or IE11. Trailing white space does not get removed when test cases are run in the Edge browser. Assume a bug with the MicrosoftWebDriver.exe?