[KShare] How to use the WebUI.setText() custom keyword successfully?

How to use the WebUI.setText() custom keyword successfully?

Have you ever experienced frustration when the system behaves differently during automation testing than it does during manual testing?

For example, a search box with autocomplete functionality. When you enter a value into the search box, the result will be displayed in real-time. However, it does not occur in all cases.

The reason for this is that not all system behavior could be perfectly replicated by Selenium keywords in all current instances.

We will be sharing 3 custom keywords below, which can cover Selenium keywords with the cases above:

  1. setTextWithJavascript
CustomKeywords.'EnhancedWebUI.setTextWithJavascript'(inputTestObject, 'hello')
  1. setTextWithJQuery
CustomKeywords.'EnhancedWebUI.setTextWithJQuery'(inputTestObject, 'hello')
  1. setTextWithBuiltInKeyword
CustomKeywords.'EnhancedWebUI.setTextWithBuiltInKeyword'(inputTestObject, 'hello')

:information_source: You can download the 3 files below and put them under the Keywords folder of the Katalon Projects.

Sample Test Script.groovy (2.0 KB)

JQueryMin.groovy (90.1 KB)

EnhancedWebUI.groovy (13.4 KB)

Moreover, we provide a custom keyword named EnhancedWebUI.verifyElementSetTextable

CustomKeywords.'EnhancedWebUI.verifyElementSetTextable'(inputTestObject, 'hello', true)

It helps you determine which mentioned methods above work for your case.

Let us know if the custom keywords that we have provided works for you. And if they do, then don’t forget to show us some love by clicking on the like button below :heart:!

And feel free to leave us your feedback so that we could improve our articles in the future!


We also have other topics related to working with custom keywords in Katalon Studio, check them out below :point_down:

:pushpin: How to use the Windows.switchToDesktop() custom keyword to switch window faster?

:pushpin: How to use the WebUI.setText() custom keyword successfully? → You are here

:pushpin: How to use the WebUI.dragAndDropToObject custom keyword successfully?

:pushpin: How to work better with Browser Alerts?

1 Like

Thank you Product Support team (@support.squad) as always for this insightful topic. And also a big shout-out to the two individuals below for your contribution to this topic:

Linh Nguyen Thong Tran
Linh Nguyen (@linh.nguyen) - Product Support Manager at Katalon Thong Tran (@thong.tran) - Senior Software Engineer at Katalon
Linh is the Product Support team Manager at Katalon. She spent many years working as an Automation Testing QA before joining Katalon Product Support as a technical support expert. Based on her experiences, she usually provides customers with highly applicable solutions. She now manages her team with a user-centric approach to guarantee customers greater success with Katalon Products. A passionate Katalon developer with a wealth of programming and testing expertise. Thong has been dedicated to providing exceptional enterprise support for the past five years, helping Katalon’s customers achieve their testing goals with ease.
1 Like

@support.squad @albert.vu I’m politely suggesting you take this post down – at least until EnhancedWebUI.groovy has been reviewed by a seasoned testing developer with at least some experience of JavaScript coding.

Meant to provide feedback earlier but it seems to me that most of the difficulty here is that the object captured is of the wrong sort eg any HTML other than than <input> tag. When you try to manually test it you click in a cell in a table for example, you are not aware that you are actually entering text in an input field, nested in a div, nested in a td element! I wonder what danger there is in overriding the logical DOM behaviour?

My main concerns on such topics is, the ‘solutions’ are provided by uploading files to the forum.
Which makes code review awfull (actually impossible because downloading them and post some thoughts back here it is not actually code review)
Does the squad team not afford an own git repo somewwhere where the code can be easilly reviewed and eventually commented / contributed?

Or this is propietary code?
If second, must be provided with a clear licence stating it
Actually, in any case should be licenced. It is opened or closed?
It has copyright or copyleft?
I am not kidding on the last sentence, read about:

therefore, without a clear licence stated, the Katalon forum rules apply so whatever is posted here is the property of Katalon
Therefore, if i will use this into my production process, i can be sued for infrigement, isn’t it?

so, if i cannot use it legally, what’s the point of it?
how it will help the comunity?

LE: ah, i get it. are provided as custom keywords so can be used only by licenced users. all clear for me

Okay, having politely suggested a takedown and as that suggestion has been left unaddressed…

In my view, users should not use EnhancedWebUI.groovy without having first exhausted all other possibilities to use WebUI and/or Selenium code directly. If you use this class and its methods, you will be sidestepping the robustness offered by Se which has been developed over ~2 decades to ensure testing methods do not create false positives and ensure test results are meaningful. Indeed, there are cases and Test Cases where the JavaScript offered by EnhancedWebUI.groovy will lead to a “pass” where it should lead to a fail.

Further, EnhancedWebUI.groovy uses synthetic events which do not conform to web standards which again renders any test using them largely meaningless.

I can’t emphasize this strongly enough. If you build a test suite incorporating this code…

  1. Your tests are wrong.
  2. Your results, pass or fail, don’t mean anything.
  3. You can’t look to anyone here for help and support because you are deviating from published, agreed upon, web standards.

#3 matters a lot. When you (we) write tests, you are claiming that the result conforms to (and is measured against) some kind of standard. If you throw out the standards and replace them with nothing…

Your test result means nothing.

1 Like

this can be written by code:

assert.true(true) 

Hi folks, :wave:

Sorry for our late reply from our team.

We have brought Russ’ suggestion to our Support team for review. Though given that it is already the weekend in Vietnam time (and also the coming Christmas), we don’t think they will be able to get back to you until next Tuesday.

In the meantime, we will unlist this thread to prevent further confusion / misinformation that it may cause.

Thanks all and have a nice holiday, :christmas_tree: :santa:
Albert

Hi Bionel,

Thanks for sharing your concerns and suggestions.

Please allow me to clarify each of them for you.

  1. Does the squad team not afford an own git repo somewwhere where the code can be easilly reviewed and eventually commented / contributed?
    => Thank you for the suggestion. We should use Git Repo to share ideas in the future in a more formal manner.

  2. this is propietary code?
    => Please for sure that, from the Support Squad, we simply want to contribute our ideas to the community. Thus, this is not a proprietary code.

Again, thanks for your advice to make us better.

We wish you a Merry Christmas and a Happy New Year with all the peace.

Hi @Russ_Thomas,

Thanks for reviewing this article and giving us a detailed suggestion on it.

Firstly, I totally agree with what you have pointed out. And our bad is that we weren’t clear on why there are some more “set text keywords” while we already have “Selenium set text”.

Yes, we shouldn’t use these custom keywords in the first place. And what our post missed here is to emphasize when to use these custom set text keywords.


So, come back to the time we created these keywords. That was a tough case. The Selenium set text keyword was just working as normal; the text had been written into the input correctly, but right after we unfocused from the input element, the inputted text was gone.

The reason behind that behavior is that the web page holds its own state of the inputs in memory, and the set text action performed by Selenium somehow cannot simulate exactly the user action.
Maybe it is a bug in Selenium, or maybe it is a bug in the browser. I don’t know. And after hours of trying multiple ways to set text with Selenium, nothing worked.

So, I’ve dug deeper into the source code of the web page to see what the logic behind that input is. And the logic is really complicated. It captured the native events and replaced them with some kind of JQuery event system that has custom event types and a custom event propagation flow.

So, my idea is that if we use JQuery to emit the set text event directly into that JQuery event system, that event can be recognized. And actually, it worked. But the drawback is that we will lose the ability to verify if a real user can perform that action successfully or not.

To patch that disadvantage, I’ve also put in some verifications, like clicking on that input with Selenium first, to make sure the input is interactable. And verify the inputted text again to make sure that the text has been set successfully.

And finally, what we have are the keywords above.


Reading the story, you can see that, the keywords we introduced above are merely suggestions for tackling complex text-setting problems. And I believe there are likely other effective methods to overcome this problem, but given the limited time constraints for each support ticket, we can only offer these suggestions for now.

If you have further experience dealing with this issue, please feel free to share it here, this will be valuable information for other users who encounter similar problems in the future.

1 Like

Thanks for the considered response, @thong.tran. I am on vacation at the moment. I will respond fully when I return.

Yah, I wish you a nice vacation :smiley:, :beach_umbrella: