Using .trim() on a variable for SetText is not removing the trailing space in the variable

WebUI.setText(findTestObject(‘Admin Portal/Page_Certification App/input_First Name_firstName’), StudentFirstName.trim())

StudentFirstName is a variable and is databound to an excel spreadsheet. Just realised that all the firstnames in the spreadsheet have trailing spaces, like Nanci is "Nanci " in the spreasheet. When I use the above statement to set value into the first name field it is still setting "Nanci " instead of “Nanci”

Am I using the correct syntax for trim() ? Should I be using replace instead?

That looks good to me.

What does this look like?

println ">" + StudentFirstName.trim() + "<"

We’re expecting >John<

The response is >Nanci <

When I tried to get the text from the first name field, I got an error message for this line of code
String StuFirst = WebUI.getText(‘Admin Portal/Page_Certification App/input_First Name_firstName’).trim().

Am I missing any Java language references?

Your call to getText() is missing a call to findTestObject()

Try…

String StuFirst = WebUI.getText(findTestObject('Admin Portal/Page_Certification App/input_First Name_firstName')).trim()

Thanks! I saw that and changed it.

Also instead of trim(), I used replace(), still shows the trailing space. This is so bizarre. Can it something other than a space after "Nanci ", but not sure what character it can be?

CR/LF? (Carriage return, Linefeed)

Try using /\n/ in your replace call - that’s a regular expression, by the way.

Thanks Russ! But that is not helping either :frowning:

I tried for space and carriage return or linefeed so far. I can just update my spreadsheet but I wanted to adjust my code so that I dont have to worry about the values in the spreadsheet.

Try /\s/

I’ll have to cleanup this thread when we nail it.

@mridula.palivela

You need to see exactly what bytes there are at the tail of Nanci.

The following test case code shows how to convert a string to HEX format. Learn it and apply the method to your case.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('')

WebUI.navigateToUrl('https://katalon-demo-cura.herokuapp.com/')

String text = WebUI.getText(findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment'))

WebUI.comment(">${text}<")

String hex = text.getBytes().encodeHex()   // convert byte[] to a string of HEX 

WebUI.comment(">${hex}<")

WebUI.delay(5)

WebUI.closeBrowser()

This gives me an output like:

...
>Make Appointment<
...
>4d616b65204170706f696e746d656e74<
...
1 Like

Russ,
I tried /\s/
Got error message to remove the \

2020-02-05 20:44:07.918 INFO c.k.k.c.keyword.builtin.CommentKeyword - >Nanci <
2020-02-05 20:44:07.925 DEBUG testcase.CertifyStudents - 7: hex = getBytes().encodeHex()
2020-02-05 20:44:08.996 DEBUG testcase.CertifyStudents - 8: comment(>$hex<)
2020-02-05 20:44:11.451 INFO c.k.k.c.keyword.builtin.CommentKeyword - >4e616e6369c2a0<

is NO-BREAK SPACE character in UTF-8 (also called &nbsp; in HTML specification) Unicode Character 'NO-BREAK SPACE' (U+00A0)

By definition, java.lang.String.trim() call will not trim NO-BREAK SPACE.

Now please try to trim the 0xC2A0 yourself. You would be able to find some resources by Google.

1 Like

Feeling silly that I did not check the html after I entered the name in the input field using the variable… it showed Nanci&nbsp. But thanks @kazurayam for helping me figure this out!

Thanks @Russ_Thomas for you help!

Used this post to figure out what to use to replace the no break space - WebUI.getText not trimming trailing space

1 Like