18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Unable to simulate "ENTER/GO" on the ipad using _keyPress()

kagkag Members
edited August 2013 in Sahi Pro
This snippet of code is not working trying to hit enter after entering a search query:
_setValue(_textbox(0), "");
_setValue(_textbox(0), $searchQuery);
_focus(_textbox(0));
_keyPress(_textbox(0), [13,13]);

I also tried without _focus and using _keyPress(_textbox(0), 13), but nothing has worked. Am I using the correct api or am I missing something?

Thanks in advance.

Answers

  • Pratyush_TytoPratyush_Tyto Members
    edited August 2013
    Hi kag,
    Simulating ENTER keypress using _keypress doesn't seem to work.
    Although, please have a look at an alternate way for ENTER keypress event.
    Please have a look at the following link.
    http://sahi.co.in/w/_setfile
    
    It should sovle your problem.
    If you still have doubts though, please let me know.

    Regards.
  • kagkag Members
    Hi Pratyush_Tyto,

    I looked at _setfile(2), and am not sure how to use it as I am not uploading a file. Here is what I would like to do:
    1. Enter a search term in a search box (has no search button)
    2. Hit enter to perform the search.
  • Hi kag,
    I think there has been a misunderstanding. I will make it more clear for you.
    The following line of code in the script on the above link is what is needed to simulate the ENTER keypress event.
    _sahi._typeKeyCodeNative(java.awt.event.KeyEvent.VK_ENTER);
    

    However this function isn't defined in Sahi OS so you'll need to add the following functions as well in your script:
    _sahi.robot = new java.awt.Robot();  
    Sahi.prototype._typeNative = function(str){
    	for (var i=0; i<str.length; i++){
    		var c = str.charAt(i);
    		var keyCode = str.charCodeAt(i);
    		var shiftKey = false;
    		switch (c){
    			case ":":{
    				shiftKey = true;
    				keyCode = java.awt.event.KeyEvent.VK_SEMICOLON;
    				break;
    			}
    			case "\\":{
    				keyCode = java.awt.event.KeyEvent.VK_BACK_SLASH;
    				break;
    			}
    			case "_":{
    				shiftKey = true;
    				keyCode = java.awt.event.KeyEvent.VK_MINUS;
    				break;
    			}
    		}
    		if ((keyCode >= 97 && keyCode <= 122) || (keyCode >= 65 && keyCode <= 90)) {
    			shiftKey = (keyCode >= 65 && keyCode <= 90); 
    			keyCode = eval("java.awt.event.KeyEvent.VK_" + (""+c).toUpperCase());
    		}
    		if (shiftKey) this.robot.keyPress(java.awt.event.KeyEvent.VK_SHIFT);
    		try{
    			this._typeNativeKeyCode(keyCode);
    		}catch(e){
    			print (c + " " + e);
    		}finally{
    			if (shiftKey) this.robot.keyRelease(java.awt.event.KeyEvent.VK_SHIFT);
    		}
    	}
    };
    Sahi.prototype._typeNativeKeyCode = function(keyCode){
    	this.robot.keyPress(keyCode);
    	this.robot.keyRelease(keyCode);
    };
    

    This should make it work for you.
    Please let me know if it doesn't.
    Regards.
  • kagkag Members
    I have Sahi Pro, and I look apis.sah, but all it has for _typeNativeKeyCode is an empty function. I tried to run the command, but nothing happened. I then tried to copy and paste the above and it did nothing when I ran the script. There was no error in the logs that I could see.

    I'm emulating the ipad on FF as I can't get anything to run on the ipad (see my other thread).
  • Hi kag,
    Sorry for the confusion. Your other post was in the Sahi OS forum and not the Pro one, so I assumed that you required it to work with Sahi OS.
    For Sahi Pro,
    Use the following code statement to simulate a ENTER keypress event:
    _sahi._typeKeyCodeNative(java.awt.event.KeyEvent.VK_SPACE);
    

    Let me know if it doesn't work for you.

    Regards.
  • kagkag Members
    Hi - I tried the above (although replaced VK_SPACE with VK_ENTER) and it still did not work. Here is my snippet:
    _setValue(_searchbox(0), $searchQuery); //this works

    _focus(_searchbox(0));
    _sahi._typeKeyCodeNative(java.awt.event.KeyEvent.VK_ENTER);
  • kagkag Members
    I am wondering if this is more than just an enter. Manually when I enter the query string the keyboard pops up to enter my text. To execute the search the enter/go/return key now says "Search". Recently the devs changed the search query field from a textbox to a searchbox, so wondering if that somehow makes the ENTER not work?
  • Hi kag,
    Please have a look at the following code:
    _setValue(_searchbox(0), $searchQuery); //this works
    _focusWindow();
    _focus(_searchbox(0));
    _typeKeyCodeNative(java.awt.event.KeyEvent.VK_ENTER);
    

    I have added _focusWindow before you focus the searchbox element and also removed "_sahi." from the _typeKeyCodeNative API.

    I have tested it taking textbox as the element and it works as required. However it should work for searchbox too.

    Please let me know.

    Regards.
  • Sorry for the long delay. I just tried your snippet and the enter still did not happen. I may have a workaround.

    Thanks anyway.
  • Hi kag,
    Please post the workaround here so that others who are facing a similar issue can also get it working for them.

    Regards.
  • kagkag Members
    The workaround was that I constructed a URL with the search query to get the search result page.
This discussion has been closed.