WebUI.getText not trimming trailing space

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.

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.

1 Like

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.

Either the code or the documentation is buggy.

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.

1 Like

Moving to correct category.

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()

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?

Hi @marias

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

It’s working for me. thanks

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?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.