Hi, I have a problem with my web automatic testing.
My problem is the web keep refreshing after execute ‘set text’ command. For your information, it caused by the auto-calculate function from the web (so every time I set the input of number, it will do calculate itself), maybe because there is an onchange function on that field (if yes, I wan t to delay the trigger after complete my input of number, because the trigger causes the input back to 0)
My concern is, when I do the recording, it was fine at all (the text just set first, then click anywhere, and following with the calculate function (which causes refresh the page)), but the input stay there. In case to execute that test case, the text just set in the same time with the calculate function (onchange was triggered too early), which causes the input cleared (back to 0).
I’ve tried using ‘wait’ or delay command, but it doesn’t work at all.
Can you help me how to solve it, or how to delay the trigger (onchange function)? I’ll appreciate all of your suggestion
If the setText function is not working as expected with your website, maybe you can try to set the text using JavaScript. In my opinion it is not the ideal solution, but maybe it can help you anyway.
For example here is the script to change the value of the input with some id via jQuery:
WebUI.executeJavaScript("\$(\"#inputID\").val( \“your number here\” );", null)
Don’t forget to put the escape character ( \ ) where it is needed.
I haven’t tested this and I am not sure will this trigger onChange event. In case it doesn’t you can just add this code as well to trigger it manually:
First of all, thanks for your response
I’ve tried your solution, but I think that it still doesn’t work for my case
I want to said that my problem is not about how to trigger the onChange event, but the opposite, I want to delay/disable it because it makes my input on the fields are become empty. I will explain more about the what happened to my automation while the execution running below:
- I give an input of number (setText) to the field (which is have onChange event on there)
- in normal case, onChange event will called after I click another object
- but what happened to me, even when the field cleared, the onChange event already running (which is causes resetting my input of number (the input disappear/empty again))
Is there any solution to delay or disable onChange event from my test case? Or how can I do to prevent the onChange being triggered? Thanks before
The input of WebUI.executeJavaScript("\$( \“input\” ).unbind( \“change\”);", null), it must be filled by the object id, isn’t it? or can I using xpath of the object?
I also wanna ask you about ‘unbind’ script, how can I know that the onChange events already removed when I executing my test case?
I strongly advise against modifying the event bindings. If you modify the AUT, any test you make is no longer testing the AUT. You are testing something else entirely.
If the user can’t do it, through normal page interactions, your test should not do it, either. You should strive to test what is there, not modify it so you can test something else instead.Here is a better approach_:_ test the outcome of user interactions, even if that includes page changes due to inputs becoming reset back to zero. If that is how the page works, that is what you should test.
If you believe the page is behaving improperly, tell the developers.
If you believe the page is untestable, tell the developers.
If you believe the developers could modify the page to aid testing, ask them to do that.
_If you modify the page, your results (passes and failures) are meaningless.
_
I agree with Russ, that is what I meant by “In my opinion it is not the ideal solution”, but just the long version. I will still answer your last question, in case if you decide to go with modifying the page. I have run into some actions that I could not (or maybe don’t know how to) reproduce with selenium/katalon so I had to use JavaScript.
-"…it must be filled by the object id, isn’t it? or can I using xpath of the object?" The code executes JavaScript (jQuery library), so the selector is jQuery selector. Lets look at the JS code without the escape characters:
$( “input” ).unbind( “change”);
So if you want to remove change event from element with id “firstNumber” the command will be:
$( “#firstNumber” ).unbind( “change”);
Don’t forget the # at the begining of ID.
Please note that this is related to HTML itself and not Objects from ObjectRepository.
So finally if you want to remove onChange from some input with firstNumber id, katalon code would be: WebUI.executeJavaScript("\$( \"#firstNumber\" ).unbind( \“change\”);", null)
-“how can I know that the onChange events already removed when I executing my test case?” The code that I provided is executed by katalon. It will remove onChange event DURING test execution. The onChange event will be removed when the code “WebUI.executeJavaScript(”\$( \“input\” ).unbind( \“change\”);", null)" is executed. So be sure to put it before you set text, BUT also after the page loads.
This is maybe a little bit confusing if you lack JavaScript knowledge and I would highly advise you to learn at least basics of JavaScript (and jQuery), and then this will be all clearer. I would also advise you to go with the Russ’s advice if you can.