I am unable to store the input that i take using the command addScript in the following way:
addScript | var rate = prompt(“Please enter the rate for price reduction”) | i
I want to use the user input for filling a detail at a later stage.
Hey thanks, yes the user input is being given using addScript command. Actually I am working on a client project and he wants the script for katalon recorder.
That doesnt make any sense to me. Cant udnerstand why the client would not want you to script, and also how he has any say in the way you test the project. He is a client, not a test engineer. You should just tell him that you cant test his project then
No, it’s actually for an automation task that he needs to perform on a website. This is where I’m stuck. He wants that he would enter some value and then the task is done accordingly…
You can issue a prompt in JavaScript (in Katalon Studio) but the problem then is, you have to control the webdriver (and hence Katlon) while waiting for the Prompt to return a value. None of which is straightforward, but I think it could be done.
In pseudocode/English:
Don’t start coding until you have read the whole of this post!
Write a JavaScript routine (routine1) that creates a known location called window.katalon_prompt_result. Store the initial value “no-value” here.
Write a JavaScript routine (routine2) that issues a prompt and saves the result to window.katalon_prompt_result.
Write another JavaScript routine (routine3) that returns the result from window.katalon_prompt_result.
Call routine1 JavaScript from Groovy. This sets the stage.
Call routine2 from Groovy. This will generate the prompt and fill the window.katalon_prompt_result variable.
In Groovy, loop over calls to routine3, waiting for a value that is NOT “no-value”. You should set a limit on this loop, 10-30 seconds, perhaps.
When you have something other than “no-value”, you have the value the user entered in the prompt.
Personally, I would place all the JavaScript in a single file and inject it into the browser as part of your Groovy code.
caveat: This design is untried and may contain design bugs. But in essence, it contains the solution for Katalon Studio as it stands today. That said, I do some similar things (waiting and calling JS in a loop) every day and it works. The part that’s untried is issuing a prompt. If webdriver decides to complain about an unhandled alert, well, we’re out of luck.
RFC: If my suspicions are correct about webdriver getting in the way, does anyone know of a selenium command to suspend/restart webdriver? (@Brandon_Hein, @devalex88, @ibus, @Marek_Melocik )
I did next:
I created localhost html code where is embedded javascript
and I am able to read what user is typed in Katalon Studio
<!DOCTYPE html>
<html>
<head>
<script>
//set minutes
//var mins = 1;
//calculate the seconds
var secs = 20;
//countdown function is evoked when page is loaded
function countdown() {
setTimeout('Decrement()', 30);
}
//Decrement function decrement the value.
function Decrement() {
if (document.getElementById) {
//minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
//if less than a minute remaining
//Display only seconds value.
if (seconds < 30) {
seconds.value = secs;
}
//Display both minutes and seconds
//getminutes and getseconds is used to
//get minutes and seconds
else {
//minutes.value = getminutes();
seconds.value = getseconds();
seconds.style.color = "red";
}
//when less than a minute remaining
//colour of the minutes and seconds
//changes to red
//if (mins < 1) {
// minutes.style.color = "red";
// seconds.style.color = "red";
//}
//if seconds becomes zero,
//then page alert time up
if (secs < 0) {
alert('time up');
//minutes.value = 0;
seconds.value = 0;
}
//if seconds > 0 then seconds is decremented
else {
secs--;
setTimeout('Decrement()', 1000);
}
}
}
function getminutes() {
//minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
//take minutes remaining (as seconds) away
//from total seconds remaining
return secs;
}
</script>
<title>
Input info
</title>
</head>
<body onload="countdown();" style="text-align: center;">
<div>
Time Left to add value:
<input id="seconds" type="text" style="width: 20px;
border: none; font-size: 16px;
font-weight: bold; color: black;">
</div>
<h1 style="color:green;">
Input test
</h1>
<h2>
Give some info
</h2>
<button onclick="data()">Click to add user input!</button>
<p id="g"></p>
<script>
function data() {
var doc = prompt("Please enter value, you have 20 seconds to time add it");
if (doc != null) {
document.getElementById("g").innerHTML = doc;
}
}
</script>
</body>
</html>
And TESTCASE
def htmlPath = System.getProperty("user.dir")+"/Include/JavaScript/prompt.html"
WebUI.openBrowser(htmlPath)
WebUI.click(findTestObject('Object Repository/JavaScript/Page_Input-info/add-user-input'))
long start = System.currentTimeMillis();
//you have 20 secs time to add input
while(true){
WebUI.delay(1)
long end = System.currentTimeMillis();
float sec = (end - start) / 1000F;
System.out.println(sec + " seconds");
if (sec >= 20){
println "Time out"
break
}
}
while (true){
def val = WebUI.getText(findTestObject('Object Repository/JavaScript/Page_Input-info/given-value'))
if (!val.equals(""))
{
println val
break
}
WebUI.delay(2)
}
WebUI.delay(2)
WebUI.closeBrowser()
If I understand your answer correctly, it would require that the AUT developers modify their page(s) to include the prompt. Most devs would find that unacceptable.
I think that means, the boss wants the test case to ask for user input, not that the AUT asks for it. Therefore the “problem” is not how to handle a window.prompt in Groovy, it’s how to inject one from Groovy into the AUT.
If I misunderstood your answer, please let me know.