How to allow only numbers using setText method?

Hey guys,

Whenever i set text in my textbox, it should be allows numbers only.
i have following code to setText

WebUI.setText(findTestObject('Object Repository/Textbox1'), FF200, FailureHandling.STOP_ON_FAILURE)

when i enter numbers with string it should be give error.

i have following code to setText

WebUI.setText(findTestObject('Object Repository/Textbox1'), FF200, FailureHandling.STOP_ON_FAILURE)

What is FF200? Is that a variable or did you miss quotes?

when i enter numbers with string it should be give error.

Please show the HTML of the input element.

Ohh sorry i missed quotes for FF200
you should be able to enter only numbers using setText method.
if i setText like below code it should be throw an error

My HTML is :

<input name="QtyToOrder" value="1" id="QtyToOrder" type="text">
WebUI.setText(findTestObject('Object Repository/Textbox1'), 'FF200', FailureHandling.STOP_ON_FAILURE)

The HTML is not enforcing numeric values, perhaps JavaScript is enforcing the restriction. If so, you will need to ensure that you also trigger the correct event to allow the JavaScript to execute.

The event might be onchange, onblur, or any of the key-related events. Usually, onchange.

BTW, setText itself does not offer any restrictions on the value supplied to the target element.

The scenario in my site:
I have below textbox and also javascript enforce the range validation “1 to 1000” and allows only numeric value on it.
2019-02-27_125943

Problem in Katalon:
Now, whenever I use a setText keyword for this input then it will give red validation message many times. not sure setText call onchange or blur.

Here is my code.
WebUI.setText(findTestObject('IS/Product Detail/Quantity Block/Quantity Textbox'), '')
or
WebUI.setText(findTestObject('IS/Product Detail/Quantity Block/Quantity Textbox'), '5')

WebUI.executeJavaScript('document.querySelector("#QtyToOrder").onchange();', null)

@Russ_Thomas
executeJavaScript is fine

is there any way in which I use setText keyword and it does not fire any blur or onchange events on the textbox at the same time?

because in my site, I have displayed those validations on Quantity Textbox blur.

I strongly suspect setText does NOT call onchange or any other event. But I confess, I do not KNOW this to be true.

Now I’m not sure what you’re asking.

(Aside: Mihir, if I have answered your question and provided a working solution, please consider marking the post as a solution. It helps other users find the solution when they are searching. Thanks.)

As with all other WebUI methods, all it does is wrap a Selenium implementation with a bunch of other code related to Katalon’s test framework. In this case, it uses the sendKeys() method:

According to the Se documentation, this directly simulates typing keys on the keyboard.

I agree that it does not call any events explicitly, but it would (and should) trigger any events that would be triggered by a manual user hitting keys on a keyboard. I’ve found this to be true whenever I use sendKeys(), so long as the WebElement is referencing the correct element in the DOM (the one with an event listener).

1 Like

And my experience differs.

To be certain (at least, to be able to argue either way) I’d want to see the implementation of sendKeys.

Most certainly, setting a value of an input from code (i.e. JavaScript) does not issue any change event(s). I’d really like to know for sure what sendKeys does (or doesn’t) do.

And as you’ll no doubt agree, Brandon, mostly we’re looking to reduce the pain points - and flakey code that does, maybe, sometimes, if it’s sunny outside, work, is to be dealt with pragmatically.

1 Like