Scripting
Scripting offers developers the chance to influence the screen using JavaScript. You can create scripts that are automatically performed by the system.
You use scripts in SAP Screen Personas to automate or manipulate a screen action. Scripts are small pieces of JavaScript that are stored along with a flavor. Each flavor can have many scripts. You can attach scripts to the events of UI elements.
The names of the UI elements within scripting consist of:
- configuration key to identify the component they belong to
- view name
- name of the UI element within the view
- (optional) one or more indices if the UI element is embedded within a multiplier element. You can build loops through multiplied elements when you take the index from a loop variable.
It is possible to make changes similar to the editor within scripting, but there are some small differences:
- The change is only applied once. As soon as the application overwrites the property, the change is gone.
- The change is applied to only one control. If you have a multiplied UI element, the change within the editor is applied to all instances. With scripting, it is only applied to exactly the one control addressed.
- During regular interaction with a WDA application, properties of UI elements are not directly modified. An event is fired, which in turn is processed by the server, a change event is fired and after processing of the server the input field property value is updated. This ensures that only proper values are stored in the input field and the application is only invoked if valid data has been entered by the user.
- The same mechanism applies to the scripting interaction but the events are not visible; a change to an input field would be written as InputField.value = new value;. Internally, the same processing logic as for a regular user interaction is applied.
- More complex events are wrapped into helper methods for the UI elements. These methods can be seen in the script inspector. For example, the invocation of a button is triggered by calling the press() method of that button.
Scripting for WDA
The object session is the central access to the scripting API. The object session has the main properties: info and utils and the main methods: findByID and idExists. The following lists the main entities for WDA. Further entities can be found on the SAP Community Network (SCN).
Session
Properties
| Name | Type | Access | Description |
|---|---|---|---|
| info | SessionInfo | read | General information about the current session state. |
| utils | Utils | read | Returns a utility object. |
Methods
| Signature | Return Type | Description |
|---|---|---|
| findById(String objectId) | Object | Returns object with specified ID or undefined if client does not have such component. |
| idExists(String objectId) | Boolean | Returns true if an object with the specified ID exists on the currently loaded screen. |
Session.Info
Properties| Name | Type | Access | Description |
|---|---|---|---|
| applicationServer | String | read | The host name of the SAP application server the application is currently connected with. |
| client | String | read | The current logon client. |
| codepage | Number | read | The current SAP codepage number. (WDA by default UTF-8 on unicode systems) |
| flavorId | String | read | The ID (GUID) of the current flavor. Null if no flavor is active. |
| flavorName | String | read | The name of the current flavor. Null if no flavor is active. |
| language | String | read | The current logon language. |
| user | String | read | The current logon user. |
| application | String | read | Name of the current application. |
| applicationConfigID | String | read | Name of the current application configuration ID. |
| systemName | String | read | System ID of the current system where the application is running. |
Session.Utils
Methods
| Signature | Return Type | Description |
|---|---|---|
| alert(String message) | void | This method opens a modal dialog box displaying the specified message. |
| confirm(String message) | Boolean | This method opens a modal dialog box displaying the specified message asking the user either to confirm by pressing the displayed OK button or to reject by pressing the Cancel button. The return value is 'true' if confimed and 'false' if rejected. |
| executeScript(String scriptId) | void | Executes another script from within a script. Parameter scriptId is used to identify the script to be executed. Please note that the session object of the calling script is passed to the called script (variable name: session). |
| get(String key) | String | Returns a value from the session store. Returns null if there is no entry with the specified key in the session store. |
| getScriptById(String scriptId) | String | This method returns the script source of the specified script. |
| log(String message) | void | Logs a message to the scripting log console. |
| prompt(String message, String defaultValue) | String | This method opens a modal dialog box displaying the specified message asking the user to enter a text value. The method returns the user-entered value. It returns null if the user cancels the popup. |
| put(String key, String value) | void | Puts a value into the session store. The key parameter must be a non-empty string. If an entry with that key exists then this entry will be overwritten or replaced by the currently specified value. Setting the value with null will remove the respective key from the session store (if it exists). |
| LogLevel | String | Enumeration for different log levels: DEBUG, ERROR, FATAL, INFO, NONE, TRACING, WARNING. |
| getFormattedDate(date) | Date | Returns the date as formatted string according to user preferences. |
| parseFormattedDate(dateString) | void | Returns date object out of a date string, conversion done based on user preferences. |
| getFormattedNumber(number) | String | Returns the number as formatted string according to user preferences. |
| parseFormattedNumber(numberString) | Number | Returns number out of formatted string; conversion done based on user preferences. |