Storing user input in a variable


#1

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.

Also attaching a ss of the log

Any help would be appreciated.

Thanks


#2

why dont give the vaule to a variable before run the test case?

you can use a variable, global.variable or data test.


#3

Value to the variable? I want the user input to be stored in the variable rather than a pre-defined value for the variable.


#4

hello,

execute your test by cmd and give variable as attribute
use -g_variableName in you execution command
check this page

i have used user input next in .bat file
C:“path_where_your_bat_is” execute.bat 4 (given argument 4 (will get -retry as four times)

@echo off
cd C:\Users\xxxxx\KatalonStudio\Katalon6.1.5
katalon -noSplash -runMode=console -consoleLog -noExit -projectPath=“C:\Users\xxxx\KatalonFromGitHub\KatalonProject\KatalonProject.prj” -retry=%1 -testSuitePath=“Test Suites/SpamTests” -executionProfile=“default” -browserType=“Chrome”


#5

Just store whatever the user input is into a variable. How you get this value is up to you, if its text just use

result = WebUI.getText(findTestObject('link_Register'))

This will store whatever that value is into the variable result, which you can then user later on in the script.

Is this user input performed earlier on in the script?


#6

Thanks, but I’m working for a client project and he wants the script to execute in katalon recorder only. That’s the problem.


#7

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.


#8

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 :rofl::rofl:


#9

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…


#10

Ah i think i misunderstood, so your just using the Katalon recorder extension? Not KS itself


#11

He wants that he would enter some value and then the task is done accordingly…

Yeah. Something like the Prompt command (from another IDE software) would solve it. That would be nice to have in Katalon Recorder, too.


#12

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:

  1. Don’t start coding until you have read the whole of this post!

  2. Write a JavaScript routine (routine1) that creates a known location called window.katalon_prompt_result. Store the initial value “no-value” here.

  3. Write a JavaScript routine (routine2) that issues a prompt and saves the result to window.katalon_prompt_result.

  4. Write another JavaScript routine (routine3) that returns the result from window.katalon_prompt_result.

  5. Call routine1 JavaScript from Groovy. This sets the stage.

  6. Call routine2 from Groovy. This will generate the prompt and fill the window.katalon_prompt_result variable.

  7. 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.

  8. 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 )


#13

No, I don’t think such command exists :frowning: the only way to “refresh” the current driver is to:

driver.quit();
driver = new ChromeDriver();

This of course will close the browser and end your session…


#14

hello,

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()

time

2019-06-06 20:33:09.554 DEBUG testcase.JavaScriptPrompt - 1: println(val)
4

not sure is this anything you will :slight_smile:


#15

Hi @Timo_Kuisma

The OP said…

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.


#16

hello,

ugh :grinning:


#17

Hi Saurabh,

If I understood your query correctly. I believe below code could help you in KAR

runScript | return prompt(‘Please enter the rate for price reduction’) | r
echo | ${r} |

Hope it helps!!


#18

Thank you Ankesh.

It solves my purpose. Really appreciate it.

Regards,
Saurabh