Simulating Key Press is not working

Hi Team,

I have a script which needs key press simulation (ENTER, TAB, CTRL+A etc). I have used the code to simulate key press as suggested in the below link -

_http://forum.katalon.com/discussion/4363/how-to-simulate-enter-key-press-in-katalon-studio_

However, I am getting an error with the following error -

Test Cases FAILED because (of) Variable ‘Keys’ is not defined for test case.

Please help to resolve this issue.

Can I see your script?

I hope the below line is sufficient, because I cannot share the entire script which has some sensitive data.

WebUI.sendKeys(findTestObject(‘Homepage/SearchField’), Keys.chord(Keys.ENTER))

I have made the sensitive data ciphered and pasted the entire code below.

I am trying to press enter after typing “Pen” inside an input field.

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
WebUI.openBrowser('')
WebUI.navigateToUrl('xxxx')
WebUI.sendKeys(findTestObject('Login/input_tempEmailAddress'), 'xxxx')
WebUI.sendKeys(findTestObject('Login/input_pass_temp'), 'xxxx')
WebUI.click(findTestObject('Login/input_signIn'))
WebUI.sendKeys(findTestObject('Homepage/SearchField'), 'Pen')


WebUI.sendKeys(findTestObject('Homepage/SearchField'), Keys.chord(Keys.ENTER))

You need the import statement for keys

https://docs.katalon.com/display/KD/[WebUI]+Send+Keys

import org.openqa.selenium.Keys ``as Keys
that is why, also why are you sendKeys for text?

I would use
WebUI.setText(findTestObject(``'Page_Login/txt_Password'``), Password)https://docs.katalon.com/display/KD/[WebUI]+Set+Text

If you have to hit enter ok, but for the text part try setText. Just my opinion. Hope this helps.

B L said:

You need the import statement for keys

https://docs.katalon.com/display/KD/[WebUI]+Send+Keys

import org.openqa.selenium.Keys ``as Keys
that is why, also why are you sendKeys for text?

I would use
WebUI.setText(findTestObject(``'Page_Login/txt_Password'``), Password)https://docs.katalon.com/display/KD/[WebUI]+Set+Text

If you have to hit enter ok, but for the text part try setText. Just my opinion. Hope this helps.

After importing the statement for Keys, the exception is resolved, but this is not actually simulating the Enter key press.It is just executing the statement and that’s it.

As far as setText() is concerned, I found that in my case both sendKeys() and setText() are behaving the same way, so I am not worried about it.

What I am concerned about is, I need to simulate the Enter Keypress.

Based on your original issue: “**Test Cases FAILED because (of) Variable ‘Keys’ is not defined for test case” => **All you need is to include this import statement: import org.openqa.selenium.Keys ``as `Keys

Now you can try to use your ‘sendKeys’ keyword again.`

@Keyword
	def clearInputField (TestObject fieldname)
	{
			WebUI.sendKeys(fieldname, Keys.chord(Keys.CONTROL, 'a'));
			WebUI.sendKeys(fieldname, Keys.chord(Keys.DELETE));

}
this is the method to clear input filed which do ctr+a and delete. hope this will gives you some idea

I have the same issue as Prabodh_Ghosh.
And I take the advise to include import org.openqa.selenium.Keys ``as Keys
in the script; however I got the error message “goovy: 15: unable to resolve class org.openqa.selenium.Keys”
Any idea?