javascriptjavascriptingrhinosap-gui

SAP GUI for Java 7.30 rev 2 - OS-X - Any way to send a spacebar keypress to a combobox using Javascript


Using JavaScript (Mozilla Rhino JavaScript engine) to script SAPGUI (for Java 7.30 rev 2 on OS X 10.10.5). One thing I'm stuck on is figuring out how to send a spacebar keypress after focusing on a combobox element (with .setFocus() ). I need the spacebar keypress sent in order to clear the combobox value. After using userarea.findById to resolve the SAP object and save it to a var. I tried passing an empty string to set the key directly:

comboBox.key = "";

I've also tried passing a space character to the key. These approaches don't appear to change the element at all. Keys which aren't empty (comboBox.key = "CB") will work to set other values in the box so I know I have the right element in focus.

I've tried to record the UI interaction in the "Scripting..." window but the keypress doesn't appear to be picked up. Also, when I change the value, during the recording, it just records an empty string as listed above. I've also tried to use the Java bridging capability of the JS engine to do the following:

  1. Pass in an empty Java string
  2. Automate keypress using the keyPress method of Packages.java.awt.Robot but I get an error:

    org.mozilla.javascript.JavaScriptException: java.security.AccessControlException: access denied ("java.awt.AWTPermission" "createRobot")

The only working option I have right now is using AppleScript to send a spacebar keystroke through "System Events". I would like to stay away from AppleScript if I can as I have quite a bit put in on the JS script.


Solution

  • After weeks of digging, I was able to resolve the issue by doing the following:

    comboBox.mScriptObject.setSelectedItem("");
    

    To figure out the solution, I dumped the fields and methods of the "comboBox" element in the scripting console:

    var methods = "";
    var fields = "";
    var comboBox;
    
    comboBox = userarea.findById("<relative id here>");
    
    objInfo = comboBox.getClass()
    
    for(var i = 0; i < objInfo.getFields().length; i++){
        fields += objInfo.getFields()[i] + "\r\n";
    }
    
    for(var i = 0; i < objInfo.getDeclaredMethods().length; i++){
        methods += objInfo.getMethods()[i] + "\r\n";
    }
    
    "Fields:\r\n\r\n" + fields + "\r\n\r\nMethods\r\n\r\n" + methods;
    

    I then noticed this:

    public java.lang.Object com.sap.guiservices.scripting.base.GuiScriptWrapper.mScriptObject
    

    Not knowing exactly what "mScriptObject" was, I performed the same field and method dump for "comboBox.mScriptObject". A few methods of immediate interest were:

    public java.lang.String com.sap.platin.r3.control.GuiComboBox.getValue()
    public java.lang.String com.sap.platin.r3.control.GuiComboBox.getKey()
    public void com.sap.platin.r3.control.GuiComboBox.setSelectedItem(java.lang.Object)
    

    I then tried calling each of the methods and checked the results. After calling "setSelectedItem", the "comboBox" element, in the SAPGUI window, reflected the string I passed in.