Action APIs
Sahi Pro Flowchart's Actions simulate user actions on the application screen.
Normally Sahi tries to perform actions by trigerring events on the application. This approach facilitates play back without the application window being in focus.
Mouse Events
Section titled “Mouse Events”Mouse events simulate the user’s mouse based actions.
Combo keys are available with some mouse events to simulate SHIFT click, CTRL click etc.
These are used for multi-selecting on some UIs.
Modes BrowserWindowsJavaAndroidiOSSAP
Click simulates a user’s click on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to click on |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_click(_button("Click Me"));_click(_button("Click Me"), "CTRL"); // clicks with CTRL key pressed_click(_button("Click Me"), "CTRL|SHIFT"); // clicks with CTRL and SHIFT keys pressed
// With _xy_click(_xy(_button("Click Me"),4,5)); // Click at coordinates 4,5 pixels inside the buttonSahi Pro Classic API _click
Double Click
Section titled “Double Click”Modes BrowserWindowsJavaAndroidiOSSAP
Double Click simulates a user’s click on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to double click on |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_doubleClick(_button("DblClick Me"));_doubleClick(_button("DblClick Me"), "CTRL"); // double clicks with CTRL key pressed_doubleClick(_button("DblClick Me"), "CTRL|SHIFT"); // double clicks with CTRL and SHIFT keys pressed
// With _xy_doubleClick(_xy(_button("DblClick Me"),4,5)); // double click at coordinates 4,5 pixels inside the buttonSahi Pro Classic API _doubleClick
Right Click
Section titled “Right Click”Modes BrowserWindowsJavaSAP
Right Click simulates a user’s click on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to right click on |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_rightClick(_button("Right Click Me"));_rightClick(_button("Right Click Me"), "CTRL"); // right clicks with CTRL key pressed_rightClick(_button("Right Click Me"), "CTRL|SHIFT"); // right clicks with CTRL and SHIFT keys pressed
// With _xy_rightClick(_xy(_button("Right Click Me"),4,5)); // right click at coordinates 4,5 pixels inside the buttonSahi Pro Classic API _rightClick
Mouse Down
Section titled “Mouse Down”Modes BrowserWindowsJava
Mouse Down simulates a mousedown on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to perform mouse down on |
$isRight | booleanoptional | Set to true for a left-hand mouse, default false |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_mouseDown(_button("Mouse Down"));_mouseDown(_button("Mouse Down"), true); //mouseDown for a left-hand mouse_mouseDown(_button("Mouse Down"), false, "CTRL"); //mouseDown with CTRL key pressed_mouseDown(_button("Mouse Down"), false, "CTRL|SHIFT"); //mouseDown with CTRL and SHIFT keys pressedSahi Pro Classic API _mouseDown
Mouse Up
Section titled “Mouse Up”Modes BrowserWindowsJava
simulates a mouseup on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to perform mouse up on |
$isRight | booleanoptional | Set to true for a left-hand mouse, default false |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_mouseUp(_button("Mouse Up"));_mouseUp(_button("Mouse Up"), true); // mouseup for a left-hand mouse_mouseUp(_button("Mouse Up"), false, "CTRL"); // mouseup with CTRL key pressed_mouseUp(_button("Mouse Up"), false, "CTRL|SHIFT"); // mouseup with CTRL and SHIFT keys pressedSahi Pro Classic API _mouseUp
Mouse Over
Section titled “Mouse Over”Modes BrowserWindowsJava
Mouse Over simulates mouse hovering on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to perform hover over or move the mouse over. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" This argument is applicable only for Browser mode |
Raw script
_mouseOver(_button("Click Me"));_mouseOver(_button("Click Me"), "CTRL"); // mouseOver with CTRL key pressed_mouseOver(_button("Click Me"), "CTRL|SHIFT"); // mouseOver with CTRL and SHIFT keys pressedSahi Pro Classic API _mouseOver
Modes BrowserWindowsJavaAndroidiOSSAP
Checks or unchecks the given checkbox or checks radio element.
Parameters
| Name | Type | Description |
|---|---|---|
$checkBoxOrRadioElement | HTML DOM element | The check box or Radio element to check or uncheck. |
$checked | booleanoptional | Set to false to uncheck the checkbox. Default value is true. This is available since Sahi Pro v11.0.0.8. |
Sahi Pro Classic API _check
Select Option
Section titled “Select Option”Modes BrowserWindowsJavaSAP
Selects one or more elements in a select dropdown.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Select dropdown |
$optionToSelect | string|integer|array of strings|array of integers | Option's text, id or index; can also pass an array of options for multi-select Only option's text can be passed for SAP mode |
$isMultiple | booleanoptional | set isMultiple true for multiselect This argument is not available for SAP mode |
Raw script
_setSelected(_select("Age"), "28"); // Selects the option visible as "28" in a select box_setSelected(_select("color"), 1); // Selects the second option in a select box_setSelected(_select("color"), "red"); // Selects red in a multiselect box_setSelected(_select("color"), "blue", true); // Selects blue also in the multiselect box_setSelected(_select("color"), ["red", "blue"]); // Unselects previous and selects red and blue in a multiselect box_setSelected(_select("color"), [1,2,3]); // Unselects previous and selects the second, third and fourth options in a multiselect box_setSelected(_select("color"), ["green", "yellow"], true); // Keeps previous options selected and selects "green" and "yellow" alsoSahi Pro Classic API _setSelected
Set Value From List
Section titled “Set Value From List”Modes Browser
Selects one or more elements from the datalist or simulates text entry into input elements for $inputData(Data to filter).
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Input element |
$inputData | string|array of strings | Data to filter (case-insensitive). |
$optionToSelect | string|integer|array of strings|array of integersoptional | Option's text, id or index; The text can be a pure string or a regular expression with an index can also pass an array of options for multi-select. Note- Array of options can only work with email input type element on other browsers than firefox. |
Raw script
_setValueFromList(_textbox("Age"), "28", 0); // Selects the first option visible after writing "28" from the list._setValueFromList(_textbox("Shirt"), "large", "/red/"); // Selects the first option matches with red after writing "large" from the list._setValueFromList(_textbox("Shirt"), "large"); // Populates textbox identified as _textbox("Shirt") with "large".// Below code selects both of the first email ids visible after writing "green" and "yellow" from the list(if browser is other than firefox)._setValueFromList(_email("color"), ["green", "yellow"], [0, 0]);Sahi Pro Classic API _setValueFromList
Expand
Section titled “Expand”Modes WindowsJavaSAP
Expands the tree node.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Windows|Java|SAP element | Tree element to be expanded |
$identifier | stringoptional | Identifier of tree node. - For SAP mode, this identifier must be hierarchical path of the tree node. - Identifier is not optional for SAP mode |
Raw script
// For windows applications_expand(_treeviewitem("First"));
// For Java applications_expand(_jvJTree("First"), "Root"); //If tree is passed as an argument, pass visible text of tree node._expand(_jvJTreeItem(_jvJTree("First"), "Root")); //If tree item passed as an argument, then no need to pass visible text of tree node.
// For SAP mode_expand(_tree("wnd[0]/usr/cntlTREE_CONTROL_CONTAINER/shellcont/shell"), "SAP menu/Accounting/Document Verification");_expand(_tree("wnd[0]/usr/cntlTREE_CONTROL_CONTAINER/shellcont/shell"), "SAP menu/Logistics/Shipment Order"));Sahi Pro Classic API _expand
Collapse
Section titled “Collapse”Modes WindowsJavaSAP
Collapses the tree node.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Windows|Java|SAP element | Tree element to be collapsed |
$identifier | stringoptional | Identifier of tree node. - For SAP mode, this identifier must be hierarchical path of the tree node. - Identifier is not optional for SAP mode |
Raw script
// For windows applications_collapse(_treeviewitem("First"));
// For Java applications_collapse(_jvJTree("First"), "Root"); //If tree is passed as an argument, pass visible text of tree node._collapse(_jvJTreeItem(_jvJTree("First"), "Root")); //If tree item passed as an argument, then no need to pass visible text of tree node.
// For SAP mode_collapse(_tree("wnd[0]/usr/cntlTREE_CONTROL_CONTAINER/shellcont/shell"), "SAP menu/Accounting/Document Verification");_collapse(_tree("wnd[0]/usr/cntlTREE_CONTROL_CONTAINER/shellcont/shell"), "SAP menu/Logistics/Shipment Order"));Sahi Pro Classic API _collapse
Drag and Drop
Section titled “Drag and Drop”Modes BrowserWindowsAndroidiOS
Simulates a drag and drop action
Parameters
| Name | Type | Description |
|---|---|---|
$dragElement | HTML DOM element | Element to be dragged |
$dropElement | HTML DOM element | Element on which the dragged element will be dropped |
$windowName | stringoptional | Target window for dropped element (available only for Windows mode) |
Raw script
// Drag the item and drop it in the shopping cart_dragDrop(_image("item"), _byId("ShoppingCart"));// Drag the item and drop it at coordinates 20,40 pixels inside shopping cart_dragDrop(_image("item"), _xy(_byId("ShoppingCart"), 20, 40));
// Examples for Windows mode
//Drag and Drop actions will be performed on same window._selectWindow("Source Window");_dragDrop(_image("item.jpg"), _listitem("images"));//Drag and Drop actions will be performed on different windows._selectWindow("Source Window");_dragDrop(_image("item.jpg"), _listitem("images"), "Destination Window");Sahi Pro Classic API _dragDrop
Drag and Drop at X, Y
Section titled “Drag and Drop at X, Y”Modes BrowserAndroidiOS
Drag and Drop at X, Y simulates a drag and drop action. It drags the element and drops it at the given x, y coordinate.
Parameters
| Name | Type | Description |
|---|---|---|
$dragElement | HTML DOM element | Element to be dragged |
$x | integer | x coordinate of drop point on the page |
$y | integer | y coordinate of drop point on the page |
$isRelative | booleanoptional | Specifies whether the coordinates are relative to current position of draggable element. |
Raw script
// If _image("item") is located at coordinates (50, 150)
// Drag the item and drop it at the coordinate (100, 300)// The end result is that the element's coordinates will be at (100, 300)_dragDropXY(_image("item"), 100, 300);
// Drag the item and drop it at the coordinate (50+100, 150+300)// The end result is that the element's coordinates will be at (150, 450)_dragDropXY(_image("item"), 100, 300, true);Sahi Pro Classic API _dragDropXY
Drop File
Section titled “Drop File”Modes Browser
Drop File simulates file drop. It drops file on specified element.
Parameters
| Name | Type | Description |
|---|---|---|
$filePath | string | Path of the file to be uploaded. Relative path resolves relative to files folder of the current project. |
$dropElement | HTML DOM element | Element on which file will be dropped. |
Raw script
// drop file in the shopping cart element_dropFile("D:/image.jpg", _byId("ShoppingCart"));// drop file in gallery element_dropFile("D:/sun.jpg", _div("gallery"));Sahi Pro Classic API _dropFile
Modes BrowserWindowsJavaiOS
Mouse Event based Actions like Click, Double Click, Right Click, etc. are normally triggered at 0,0 coordinates of that element (top left). This action can be used to specify other coordinates on element where the event should be fired.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Element within which we want to perform the mouse action |
$x | integer | x co-ordinate in pixels |
$y | integer | y co-ordinate in pixels |
Returns
HTML DOM element |
Raw script
_click(_xy(_button("id"), 10, 20)) // Clicks inside the button, 10px from the left and 20 pixels from the top.
// Negative values can be given to specify offset from right and bottom._click(_xy(_button("id"), -5, -10)) // Clicks inside the button, 5px from the right and 10px from the bottom.Sahi Pro Classic API _xy
Touch Events
Section titled “Touch Events”Modes BrowseriOS
Simulates a tap event(touch event) on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element that needs to be tapped |
$count | integeroptional | number of times the element needs to be tapped. Default value is 1. |
Sahi Pro Classic API _tap
Modes Browser
Simulates a touch event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element on which we need to fire a touch event |
$count | integeroptional | number of times the element needs to be touched. Default value is 1. |
Sahi Pro Classic API _touch
Touch Start
Section titled “Touch Start”Modes Browser
Simulates a touch start event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element on which we need to fire a touch start event |
Sahi Pro Classic API _touchStart
Touch End
Section titled “Touch End”Modes Browser
Simulates a touch end event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element on which we need to fire a touch end event |
Sahi Pro Classic API _touchEnd
Touch Cancel
Section titled “Touch Cancel”Modes Browser
Simulates a touch cancel event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element on which we need to cancel the ongoing touch event |
Sahi Pro Classic API _touchCancel
Touch Move
Section titled “Touch Move”Modes Browser
Simulates a touch move event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element which we need to move/drag. |
$moveX | integer | number of pixels the element needs to move in x-direction |
$moveY | integer | number of pixels the element needs to move in y-direction |
$isRelative | booleanoptional | if true, the element will move relative to the current location of the element. If false, element will move to the position where the horizontal coordinate and vertical coordinate are 'moveX' and 'moveY'. Default value is true. |
Sahi Pro Classic API _touchMove
Modes Browser
Simulates a touch move event on the HTML element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element on which we need to simulate swipe event. |
$moveX | integer | number of pixels the element needs to move in x-direction |
$moveY | integer | number of pixels the element needs to move in y-direction |
$isRelative | boolean | if true, the element will move relative to the current location of the element. If false, element will move to the position where clientX and clientY are moveX and moveY. |
Sahi Pro Classic API _swipe
Keyboard Events
Section titled “Keyboard Events”Set Value
Section titled “Set Value”Modes BrowserWindowsJavaAndroidiOSSAP
Simulates text entry into input elements like textbox, textarea etc.
The events are performed in the following sequence :
- Focus on element
- Enter the value to be set
- Remove focus from the element
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML form element whose value is to be set |
$value | string | Value to be set |
$props | objectoptional | Properties to specify for clearing the textbox, keyboard buttons to hide keyboard (Specific to iOS Automation) and triggering keyboard events (specific to Android Automation). 1. iOSKeyboardButton, string, optional Keyboard button identifier, which is used to dismiss the keyboard. This property is specific to iOS automation. 2. clearTextbox, boolean, optional Default is false. If specified as true, the textbox is cleared by selecting all text and pressing the delete key. This property is specific to iOS automation. 3. triggerKeyEvents, boolean, optional Default is false. If specified as true, keyboard events will be triggerd for the value to be set. This property is specific to Android automation. |
Raw script
// Populates textbox identified as _textbox("user") with Ram_setValue(_textbox("user"), "Ram");
// Below example is specific to iOS Automation.// Clears the texbox first and populates textbox identified as _textbox("user") with Ram and click on the return button of the keyboard._setValue(_textbox("user"), "Ram", {"iOSKeyboardButton":"return", "clearTextbox":true});
// Below example is specific to Android Automation.// Trigger the keyboard events for value to be set_setValue(_textbox("user"), "Ram", {"triggerKeyEvents":true});Sahi Pro Classic API _setValue
Key Down
Section titled “Key Down”Modes BrowserWindowsJava
Simulates pressing down a key on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Any HTML element |
$charInfo | character|integer|pr_KC | charInfo can be: - a character (eg. 'b') - charCode of a character (eg. 98) - an array of [keyCode, charCode] (eg. [13,13] for pressing ENTER key) |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
Raw script
_keyDown(document.body, 'a');_keyDown(document.body, 98);_keyDown(document.body, [13,13]);_keyDown(document.body, 'a', "CTRL");Sahi Pro Classic API _keyDown
Key Up
Section titled “Key Up”Modes BrowserWindowsJava
Simulates releasing a key press on the given element.
NOTE: Key Up is available for Windows and Flex since Sahi Pro: 8.0.0. For old document refer here
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Any HTML element |
$charInfo | character|integer|pr_KC | charInfo can be: - a character (eg. 'b') - charCode of a character (eg. 98) - an array of [keyCode, charCode] (eg. [13,13] for pressing ENTER key) |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
Raw script
_keyUp(document.body, 'a');_keyUp(document.body, 98);_keyUp(document.body, [13,13]);_keyUp(document.body, 'a', "CTRL");Sahi Pro Classic API _keyUp
Key Press
Section titled “Key Press”Modes BrowserWindowsJavaSAP
Simulates pressing a key on the given element. Does keyDown and keyUp together.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Element that supports keyPress action |
$keyInfo | character|integer|array of integers|string | keyInfo can be specified as: - A character (eg. 'b') - A charCode of a character (eg. 98) - An array of [keyCode, charCode] (eg. [13,13] for pressing ENTER key) - As a string (eg. "Ctrl|S") in SAP mode. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" Not applicable for SAP mode |
Raw script
_keyPress(document.body, 'a');_keyPress(document.body, 98);_keyPress(document.body, [13,13]);_keyPress(document.body, 'a', "CTRL");
// Below samples are for SAP mode_keyPress(_window("Create Inquiry: Initial Screen"), "Enter");_keyPress(_window("Create Inquiry: Overview"), "Ctrl|S");_keyPress(_gridView("wnd[0]/usr/cntlBCALVC_TOOLBAR_D100_C1/shellcont/shell"), "F4");Sahi Pro Classic API _keyPress
Modes BrowserWindowsJava
Types the text into the element. Different from _setValue. Type does not bring or remove focus from element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element in which the text is to be typed |
$text | string | text to be typed |
Raw script
_type(_textbox("user"), "am");Sahi Pro Classic API _type
Set Password
Section titled “Set Password”Modes BrowserWindowsJavaAndroidiOSSAP
Types the password into the element. Different from _setValue. To encrypt the password, check the Encrypted checkbox, when saving the step.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element in which the password is to be typed |
$text | string | password to be typed |
Raw script
_setPassword(_password("password"), "MgkKEQBU");Sahi Pro Classic API _setPassword
Focus Events
Section titled “Focus Events”Modes BrowserWindowsJavaSAP
Brings focus to given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Any HTML element to focus on |
Raw script
_focus(_textbox("username"));Sahi Pro Classic API _focus
Remove Focus
Section titled “Remove Focus”Modes BrowserWindowsJava
Similar to clicking somewhere else on the window to remove focus from given element.
Forces onblur events to be called.
Blur happens automatically during Sahi’s script execution.
This action is rarely used explicitly. Same as _blur.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Any HTML element to remove focus from |
Raw script
_removeFocus(_textbox("username"));Sahi Pro Classic API _removeFocus
Modes Browser
Similar to clicking somewhere else on the window to remove focus from given element.
Forces onblur events to be called.
Blur happens automatically during Sahi’s script execution.
This action is rarely used explicitly. Same as _removeFocus.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Any HTML element to remove focus from |
Raw script
_blur(_textbox("username"));Sahi Pro Classic API _blur
Text Selection
Section titled “Text Selection”Selecting text for manipulation (like selecting text in a Rich Text Editor to change font to bold)
Select Range
Section titled “Select Range”Modes BrowserWindowsJavaSAP
Example:
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to select on |
$rangeStart | integer | start position of selection |
$rangeEnd | integer | end position of selection |
Raw script
// If the text was "abcdefghij"_selectRange(_rte(1), 2, 4); // selects text cdvar $text = _getSelectionText(); // $text = "cd"
_selectRange(_rte(1), 2, 2); // places the cursor at 3rd position without any selectionSahi Pro Classic API _selectRange
Select Text Range
Section titled “Select Text Range”Modes BrowserWindowsJava
Example:
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element to select on |
$searchText | string | text to search for and select. The text can be a pure string or a regular expression with an index |
$position | stringoptional | can be "before" or "after". if specified, the selection moves to the start or end of searchText. |
Raw script
//Given a sample string//This bright red apple is sweeter than that dull red apple
_selectTextRange(_rte(1), "red apple"); // selects the first red apple_selectTextRange(_rte(1), "red apple[1]"); // selects the second red apple_selectTextRange(_rte(1), "/ap.le/[1]"); // selects the second applevar $text = _getSelectionText(); // $text = "apple"_selectTextRange(_rte(1), "/ap.le/[1]", "before"); // moves the cursor before the second apple, with no selection_selectTextRange(_rte(1), "/ap.le/[1]", "after"); // moves the cursor after the second apple, with no selectionSahi Pro Classic API _selectTextRange
Page Navigation
Section titled “Page Navigation”Navigate To URL
Section titled “Navigate To URL”Modes Browser
Navigate To URL loads the given url in the top frame of the window. To load in a popup window, first select that window by adding Select Window Action.
Parameters
| Name | Type | Description |
|---|---|---|
$url | string | URL to navigate to. Can be relative to current page. |
$forceReload | booleanoptional | If the current page URL is same as url, Sahi will not reload the page by default. Set to true to force reload when urls are same. |
Raw script
// If current page is not http://sahi.co.in/demo/_navigateTo("http://sahi.co.in/demo/"); // Will navigate to this page.
// If current page is itself "http://sahi.co.in/demo/"_navigateTo("http://sahi.co.in/demo/"); // will do nothing_navigateTo("http://sahi.co.in/demo/", true); // Will force navigation to this page.
_selectWindow("popWin");_navigateTo("http://sahi.co.in/demo/"); // loads in the popup window.Sahi Pro Classic API _navigateTo
Native Events
Section titled “Native Events”In rare cases where Mouse events do not work properly, we switch to native events. These events are created by the underlying Operating System. The application window needs to be in focus for this to work properly.
These APIs only work if the application and Sahi are running on the same system. So these APIs are not available on mobile devices.
Some native events may not function on all platforms. To simulate native events on macOS, Sahi requires Accessibility permissions. Follow these steps to grant permission:
- Open System Settings (or System Preferences on older macOS versions).
- Go to Privacy & Security > Accessibility.
- Locate the terminal application you use to run Sahi.
- Ensure it is checked (enabled).
Type Native
Section titled “Type Native”Modes BrowserWindowsJavaSAP
Types the text using native events
Parameters
| Name | Type | Description |
|---|---|---|
$text | string | Text to type |
$isEncrypted | booleanoptional | Set as true if the text is encrypted |
Raw script
_focus(_textbox("user"));_focusWindow();_typeNative("Ram");_focus(_password("password"));_typeNative("MgkKEQBU", true)Sahi Pro Classic API _typeNative
Key Press Native
Section titled “Key Press Native”Modes BrowserWindowsJavaSAP
Presses the specified keyboard key.
Every keyboard comes with a standard layer, and a shift layer. The standard layer usually contains lowercase letters and numbers, while the shift layer contains capital letters and symbols. Keys on the shift layer can be passed directly to this action. They can also be simulated using a combination key. For example, to press ‘@’, you can directly pass ‘@’ as key to this action. You can also pass ‘2’ as key and ‘SHIFT’ as combo key to press ‘@’ with U.S keyboard layout.
Parameters
| Name | Type | Description |
|---|---|---|
$key | string | Key to be pressed, e.g. "a", "F4", "ENTER", "+", "7". |
$combo | stringoptional | Any combo key such as "CTRL", "SHIFT", "ALT", "WINDOWS" or "META". Can also be two or more keys together like "CTRL|SHIFT", "CTRL|ALT|SHIFT". |
Raw script
_focusWindow();_setValue(_textbox("search"), "coffee");_keyPressNative("ENTER"); // Press enter key
_keyPressNative("F4", "ALT"); // Presses Alt+F4_keyPressNative("t", "CTRL|SHIFT"); // Presses Ctrl+Shift+t_keyPressNative("0", "CTRL"); // Presses Ctrl+0Sahi Pro Classic API _keyPressNative
Type Key Code Native
Section titled “Type Key Code Native”Modes BrowserWindowsJavaSAP
Presses the keyboard key for given keyCode
java.awt.event.KeyEvent defines keyCodes as constants which can be passed to this function.
Users can type even those characters which might not be present on a keyboard’s primary layer (characters which can be typed without using any key combinations) but has an ASCII code associated with them.
The user need to define the character in Sahi/userdata/config/keyCode_combinations.txt file in following format:
“VK_keyCode constant”:“ASCII Code” Example:- “VK_DOLLAR”:“36”
Parameters
| Name | Type | Description |
|---|---|---|
$keyCode | integer | Key Code of keyboard key to be pressed. |
Raw script
_focusWindow();_setValue(_textbox("search"), "coffee");_typeKeyCodeNative(java.awt.event.KeyEvent.VK_ENTER); // Press enter key_typeKeyCodeNative(java.awt.event.KeyEvent.VK_DOLLAR); // Press dollar key defined in keyCode_combinations.txt fileSahi Pro Classic API _typeKeyCodeNative
Focus Window
Section titled “Focus Window”Modes BrowserWindowsJavaSAP
Brings the window above all other windows.
It is generally needed before using other native events or before taking screen shots
For popup windows, add Select Window Action first.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Raw script
_focusWindow(); // bring window into focus._takeScreenShot();
// For popup windows, use _selectWindow first_selectWindow("popWin"); // target further statements at popWin window_focusWindow(); // bring popup window into focus._takeScreenShot();Sahi Pro Classic API _focusWindow
Window Action
Section titled “Window Action”Modes BrowserWindowsJavaSAP
Performs various window level actions.
For popup windows, add Select Window Action first.
Parameters
| Name | Type | Description |
|---|---|---|
$action | string | Action can be one of - "focus" - "maximize" - "minimize" - "restore" - "refresh" - "resize" |
$width | integeroptional | Specifies the width of the browser window while using the 'resize' option. |
$height | integeroptional | Specifies the height of the browser window while using the 'resize' option. |
Raw script
_windowAction("focus"); // focus window. Same as _focusWindw_windowAction("maximize"); // maximize window._windowAction("minimize"); // minimize window._windowAction("restore"); // restore window._windowAction("refresh"); // refresh window._windowAction("resize",500,300); // resize the window with the given width and height.
// maximizing a popup window_selectWindow("popWin"); // first select the popWin window to target further steps on._windowAction("maximize");Sahi Pro Classic API _windowAction
Click Native
Section titled “Click Native”Modes BrowserWindowsJavaAndroidiOSVisual Assist
Simulates a native click event on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Element that has to be clicked. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
$isRight | booleanoptional | If true, the click is a right click |
$isDouble | booleanoptional | If true, the click is a double click |
Raw script
_clickNative(_button("b1"));Sahi Pro Classic API _clickNative
Right Click Native
Section titled “Right Click Native”Modes BrowserWindowsJavaVisual Assist
Simulates a native right click event on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element that has to be clicked. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
Raw script
_rightClickNative(_button("b1"));Sahi Pro Classic API _rightClickNative
Double Click Native
Section titled “Double Click Native”Modes BrowserWindowsJavaAndroidVisual Assist
Simulates a native double click event on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | element that has to be double clicked. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
Raw script
_doubleClickNative(_button("b1"));Sahi Pro Classic API _doubleClickNative
Click at X, Y
Section titled “Click at X, Y”Modes BrowserWindowsJavaAndroidiOSSAP
Simulates a click event at the point (x,y).
Parameters
| Name | Type | Description |
|---|---|---|
$x | integer | x coordinate of the screen position to be clicked |
$y | integer | y coordinate of the screen position to be clicked |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
$isRight | booleanoptional | If true, the click is a right click |
$isDouble | booleanoptional | If true, the click is a double click |
Raw script
_clickNativeXY(100, 200);_clickNativeXY(150,300, "ALT"); // ALT + clickSahi Pro Classic API _clickNativeXY
Mouse Over Native
Section titled “Mouse Over Native”Modes BrowserWindowsVisual Assist
Simulates a native mouse over event on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | Element to perform hover over or move the mouse over. |
$combo | stringoptional | Any combo key: can be "CTRL", "SHIFT", "ALT" or "META"; Can also be two or more keys together like "CTRL|SHIFT" |
Raw script
_mouseOverNative(_button("b1"));Sahi Pro Classic API _mouseOverNative
Drag and Drop using Native Events
Section titled “Drag and Drop using Native Events”Modes BrowserWindowsJavaAndroidiOS
Simulates a dragdrop event from element 1 to element 2 using native events.
Parameters
| Name | Type | Description |
|---|---|---|
$el | HTML DOM element | the draggable element |
$el2 | HTML DOM element | the droppable element |
Raw script
_dragDropNative(_div("draggable"), _div("droppable"));Sahi Pro Classic API _dragDropNative
Drag from X1, Y1 and Drop at X2, Y2 using Native Events
Section titled “Drag from X1, Y1 and Drop at X2, Y2 using Native Events”Modes BrowserWindowsJavaAndroidiOSSAP
Simulates a dragdrop event from element at first set of coordinates to element at second set of coordinates, using native events.
Parameters
| Name | Type | Description |
|---|---|---|
$x1 | integer | x coordinate of the draggable element |
$y1 | integer | y coordinate of the draggable element |
$x2 | integer | x coordinate of the droppable element |
$y2 | integer | y coordinate of the droppable element |
Raw script
_dragDropNativeXY(100,50, 100,150);Sahi Pro Classic API _dragDropNativeXY
File Upload
Section titled “File Upload”File upload can be done using the Set File Action. File upload can also be done using Native Events, provided the browser is in the foreground. However, Set File Action does not require the browser to be in the foreground and hence is more stable than the Native Events.
Also, there may be client-side validations for the HTML File element in your application. So it may require certain javascript events to be triggered during file upload.
Action Set File triggers the necessary javascript events on the HTML File element. It correctly handles client-side validations. Thus Set File Action is recommended for file upload.
Set File
Section titled “Set File”Modes Browser
API Set File performs file upload operation.
Parameters
| Name | Type | Description |
|---|---|---|
$element | HTML DOM element | HTML File Element that triggers the popup window for file upload. There is a special case in which another clickable HTML element needs to be passed instead of file element. This Special Case is described in details.. |
$filePaths | string|array of strings | Path of the files to be uploaded. A file path can be absolute as well as relative. When relative, the file path is resolved relative to the files folder of the current project. |
$actionURL | stringoptional | The form action URL to which the file is submitted. This is an optional parameter. Use it in cases where the form's action URL is changed on setting the file. By default, it uses the form's current action URL. |
$fileElement | HTML DOM elementoptional | HTML File Element. Needed only for a special case when another clickable HTML element is passed as 1st parameter instead of file element. This Special Case is described in details. This is available since Sahi Pro v9.2.1. |
Raw script
_setFile3(_file("id"), "data.csv");_setFile3(_span("upload"), "details.xlsx");_setFile3(_span("upload"), "profile.pdf", null, _file("id"));_setFile3(_file("id"), ["C:/abc/efg.jpg", "C:/abc/abc.png"]);//when the HTML Form's action URL is changed on setting the file_setFile3(_file("id"), "C:/abc/efg.jpg", "formSubmit.jsp");_setFile3(_file("id"), ["C:/abc/efg.jpg", "C:/abc/abc.png"], "formSubmit.jsp");Sahi Pro Classic API _setFile3
Troubleshooting
Section titled “Troubleshooting”- Look to see if there are any exceptions on the Sahi console (the command prompt). If it shows any FileNotFoundException, correct the path of your file.
- Check if the URL to which the form is being submitted is correct. Sometimes the URL changes when the form is submitted. In such a case, specify the third parameter
actionURL. Refer here to identify the Action URL. - File upload with full path in Internet Explorer, find the detailed resolution here.
Identifying Action URL
Section titled “Identifying Action URL”-
Launch a browser from the Sahi Dashboard.
-
Perform the file upload functionality in your app manually.
-
Check the output on the Sahi Pro console. It will look something like this.
[java] FileUpload: Multipart Request Found[java] Action URL for upload:[java] http://localhost/demo/php/fileUpload.php[java] The above action URL may be required as the third parameter while[java] using Set File Action or _setFile3 API.[java] Recording: false[java] For troubleshooting, click on 'Enable Traffic Logs' link on the Sahi Dashboard[java] and perform the upload operation. Upload logs will be created in[java] sahi/userdata/logs/file_upload/ folder.As you can see from the output,
http://localhost/demo/php/fileUpload.phpis the action url in this case. -
Once you have identified the action url, use that as the third parameter
actionURL.
File Download
Section titled “File Download”File download overview
Section titled “File download overview”Sahi Pro detects file downloads, and automatically saves them to sahi/userdata/temp/download directory.
Sahi Pro also adds the sessionId to the file name, so that multiple simultaneous tests do not overwrite each others files.
For example, if you click on a “download” link which downloads a “setup.exe” file, this is how Sahi Pro will handle it.
- Click on “download” link
- Sahi detects that it is a downloadable file, based on its MimeType and Content-Disposition headers.
It then downloads and saves the file into sahi/userdata/temp/download/ after renaming it to
something like: sahi_0384a26207e6104f5f08868032bd170de76d__setup.exe - Once the file is downloaded, the original name of the file (“setup.exe”) can be read using the
Last Downloaded File NameAction - Action
Save Downloaded Asallows the file to be renamed and copied over to a convenient directory for you to work on. - Action
Clear Last Downloaded FileNameclears out the last downloaded file name so that further files can be downloaded and worked upon.
Last Downloaded File Name
Section titled “Last Downloaded File Name”Modes Browser
Returns the last downloaded file name.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
string | Name of the last downloaded file |
Sahi Pro Classic API _lastDownloadedFileName
Save Downloaded As
Section titled “Save Downloaded As”Modes Browser
Renames/copies the file into specified directory for further processing.
Parameters
| Name | Type | Description |
|---|---|---|
$path | string | File path to copy this file into. Needs to be a file path not a directory. If path is relative, it is resolved relative to the files folder of the current project. |
Sahi Pro Classic API _saveDownloadedAs
Clear Last Downloaded FileName
Section titled “Clear Last Downloaded FileName”Modes Browser
Clears out the the last downloaded file name so that further files can be downloaded and worked upon. After using this, the Action Last Downloaded File Name will return empty untill another file is downloaded.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _clearLastDownloadedFileName
File download APIs example
Section titled “File download APIs example”Sample Raw Script for File Download:
_assertNull(_lastDownloadedFileName()); // nothing so far_click(_link("download")); // click on download link// Wait for a while till _lastDownloadedFileName() becomes NOT null. This is when the download is complete.// We wait for a maximum of 90 seconds or till _lastDownloadedFileName() becomes NOT null.// If you find that your file is huge and takes much longer, run the following statement in a loop for 5 or 6 retries.// Do NOT increase 90000 to more than 120000._wait(90000, _lastDownloadedFileName() != null);// Assert the name of the downloaded file._assertEqual("setup.exe", _lastDownloadedFileName()); // check if downloaded
if (_lastDownloadedFileName() != null) { _saveDownloadedAs("c:/myownpath/setup.exe"); // save to another path _clearLastDownloadedFileName(); // clear the variable _assertNull(_lastDownloadedFileName()); // check to see if cleared.}File download troubleshooting
Section titled “File download troubleshooting”If you see a file download dialog during playback, do the following:
-
Check if the download URL follows a specific pattern. If yes, add the pattern of the URL to download_urls.txt Eg., if your download url is
http://mysite.example.com/export_doc.jsp?id=12318812, add.*export_doc[.]jsp.*to
sahi/userdata/config/download_urls.txt -
If there are multiple files that are to be downloaded, and they all are of a particular content-type, add the content type to
sahi/userdata/config/download_contenttypes.txt -
If your file download response always has content-disposition as “attachment”, you can tell sahi to download them by default by setting
download.download_if_contentdisposition_is_attachment=truein
sahi/userdata/config/userdata.properties.
Handling file downloads triggered in a new window
Section titled “Handling file downloads triggered in a new window”Sometimes a webpage may open a new window to show a PDF file. In this case, we need to ask Sahi to send a HTML response back using the Action Send HTML Response After File Download.
Send HTML Response After File Download
Section titled “Send HTML Response After File Download”Modes Browser
Links to download files like PDF, Excel, Doc, etc. may open a new window. These windows may not have HTML content and thus cannot be automated. In such cases, use Send HTML Response After File Download to send back an HTML response after file download.
Add Send HTML Response After File Download with parameter enable as true before the step which triggers the download. To turn it off, add Send HTML Response After File Downloadwith parameter enable as false.
Parameters
| Name | Type | Description |
|---|---|---|
$enable | boolean | true enables html response behaviour, false disables html response |
$headers | objectoptional | Additional headers to be set in the HTML response sent after file download. This is available since Sahi Pro v9.1.1. |
Raw script
_assertNull(_lastDownloadedFileName()); // nothing so far_sendHTMLResponseAfterFileDownload(true);
_click(_link("Show PDF in new Window"));_wait(90000, _lastDownloadedFileName() != null);_assertEqual("test.pdf", _lastDownloadedFileName());
if (_lastDownloadedFileName() != null) { _saveDownloadedAs("c:/test.pdf"); _clearLastDownloadedFileName(); // clear the variable _assertNull(_lastDownloadedFileName()); // check to see if cleared.}// Close the popup window_selectWindow("/File Downloaded/");_click(_link("Close Window"));
// Select back the main window_selectWindow();
_sendHTMLResponseAfterFileDownload(false);Sahi Pro Classic API _sendHTMLResponseAfterFileDownload
File download from a different domain
Section titled “File download from a different domain”When a file is downloaded from a domain different than that of the invoking page, the file will get downloaded to userdata\temp\download folder successfully. But since the download is from a different domain, it is not associated with the current script session. Hence Action Last Downloaded File Name will return a null value. Consequently, Save Downloaded As will fail.
To solve this problem, add Action Add Domain To Session with the parameter domain as applicable before triggering download.
Add Domain To Session
Section titled “Add Domain To Session”Modes Browser
Adds the domain to the current browser session during Playback.
Parameters
| Name | Type | Description |
|---|---|---|
$domain | string | Domain to be associated with the current script session |
Raw script
// Add new domain url to Sahi's playback session_addToSession("https://downloads.mydomain..example.com");// NOTE _addToSession comes before click_click(_link("Download"));Sahi Pro Classic API _addToSession
Window open/close APIs
Section titled “Window open/close APIs”Close Window
Section titled “Close Window”Modes BrowserWindowsJavaSAP
This action closes the current window. If the title argument is specified, this action closes the window having the specified title.
Parameters
| Name | Type | Description |
|---|---|---|
$title | stringoptional | Title of the window to close. A regular expression can also be specified here. This argument is available only for SAP mode and Java mode. |
Raw script
// Close popup_popup("popWin")._closeWindow();
// Close the current window in SAP mode_closeWindow();
// Close a window having specified title in SAP mode or Java mode_closeWindow("Create Inquiry");_closeWindow("/Purchase Order No./");_closeWindow("/Purchase Order No. [0-9]{5}/");Sahi Pro Classic API _closeWindow
Close Browser
Section titled “Close Browser”Modes Browser
Closes the browsers in the current session in the middle of a run.
Parameters
| Name | Type | Description |
|---|---|---|
$releaseCapacity | booleanoptional | Set to true if the browser was launched using _launchNewBrowser(). |
Raw script
_navigateTo("https://www.myapplication1.example.com");_setValue(_textbox("user"), "test");_setValue(_password("password"), "secret");_click(_submit("Login"));_closeBrowser();_openBrowser();_navigateTo("https://www.myapplication2.example.com");_setValue(_textbox("q"), "2");Sahi Pro Classic API _closeBrowser
Open Browser
Section titled “Open Browser”Modes Browser
Opens the browser in the current session in the middle of a run. This can be used to reopen browser after Close Browser Action.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Raw script
_navigateTo("https://www.myapplication1.example.com");_setValue(_textbox("user"), "test");_setValue(_password("password"), "secret");_click(_submit("Login"));_closeBrowser();_openBrowser();_navigateTo("https://www.myapplication2.example.com");_setValue(_textbox("q"), "2");Sahi Pro Classic API _openBrowser
Open Window
Section titled “Open Window”Modes Browser
Opens a new browser window.
Parameters
| Name | Type | Description |
|---|---|---|
$url | stringoptional | URL of the page to open. If no URL is specified, a new window with "about:blank" is opened. |
$name | stringoptional | name of the window. |
$size | array of integersoptional | height and width of the window. |
Raw script
_openWindow("https://sahitest.com/demo/training/login.htm","defaultSahiPopup",[1280,600]);Sahi Pro Classic API _openWindow
Calling Generic Code
Section titled “Calling Generic Code”Modes BrowserAndroid
Call is a generic API to bypass Sahi’s action APIs and directly call javascript on the DOM.
Parameters
| Name | Type | Description |
|---|---|---|
$expression | expression | Any expression which needs to be evaluated on the browser Note that the expression is not quoted and is NOT a string. |
Returns
string | The completion value of evaluating the given code. If the completion value is empty, undefined is returned. |
Raw script
// For example, a date field may bring up the calendar, but selecting the year,month etc. may be time consuming.// For repeated flows, one may want to bypass this by directly setting the value of the field through javascript._call(_textbox("date").value = "25/02/1990");
var $a=_call(2 + 2);_log($a);// returns 4Sahi Pro Classic API _call
Modes BrowserAndroid
Eval, like _call, is a generic API to bypass Sahi’s action APIs and directly call javascript on the DOM.
However, Eval takes a string instead of an expression.
Parameters
| Name | Type | Description |
|---|---|---|
$evalString | string | Any string which needs to be javascript 'eval'ed on the browser |
Returns
string | The completion value of evaluating the given code. If the completion value is empty, undefined is returned. |
Raw script
// For example, a date field may bring up the calendar, but selecting the year,month etc. may be time consuming.// For repeated flows, one may want to bypass this by directly setting the value of the field through javascript._eval("_textbox(\"date\").value = \"25/02/1990\"");
var $a=_eval('2 + 2');_log($a);// returns 4Sahi Pro Classic API _eval
Authentication
Section titled “Authentication”Set Client Certificate
Section titled “Set Client Certificate”Modes Browser
Some applications seek a client certificate for authentication. This action sets the certificate to be used for authentication in the current script session. This action must be called before navigating to the application. Refer Client Certificate Authentication page for details.
Parameters
| Name | Type | Description |
|---|---|---|
$path | string | Client certificate file path. If path is relative, it is resolved relative to userdata directory. |
$password | string | Client certificate password in encrypted format |
$keyStoreType | string | Keystore type can be JCEKS, JKS, DKS, PKCS11 or PKCS12 |
Raw script
_setClientCertificate("c:/myownpath/testuser1.jks","Mg0BChVBGh1XOB0K", "JKS");
// Certificate file path can be specified relative to Sahi userdata directory._setClientCertificate("certgen/certs/testuser2.p12","MgkKEQBU", "PKCS12");_setClientCertificate("certgen/certs/testuser3.pfx","MgkKEQBU", "PKCS12");Sahi Pro Classic API _setClientCertificate
Set Authentication
Section titled “Set Authentication”Modes Browser
Some applications seek HTTP authentication. This action can be used to add credentials for HTTP authentication schemes like Basic, Digest, NTLM, etc. Refer 401 Authentication page for details to handle HTTP authentication.
This action must be added before navigating to the application. After this action gets executed, the credentials will be used for HTTP authentication in the current Path.
Parameters
| Name | Type | Description |
|---|---|---|
$username | string | Username for authentication |
$password | string | Password for authentication |
$scheme | string | HTTP Authentication schemes like Basic, Digest, NTLM, etc. |
$realm | stringoptional | Authentication realm as requested by the authentication server |
Raw script
// Add credentials to authenticate user 'user1' user for authentication realm 'Secure Gateway'._setAuthentication("user1", "JtkMEBE=", "", "Secure Gateway");
// Add credentials to authenticate user 'user2' user for authentication scheme 'basic'._setAuthentication("user2", "JtkMEBE=", "basic");
// To specify credentials for any authentication_setAuthentication("user", "PtkMEGE=", "", "/.*/");Sahi Pro Classic API _setAuthentication

