| Share
  1. Explore
    Introduction, screen-shots, features, limitations
  2. Getting started
    Prerequisites, download, install, browser configuration, record, playback, view logs
  3. Sahi Scripting Basics - I
    Statements, variables, functions, conditions and looping, _include
  4. Sahi Scripting Basics - II
  5. Sahi APIs (built-in functions)
    1. Browser Accessor APIs
    2. Browser Action APIs
    3. Miscellaneous APIs
  6. Sahi Scripting - Calling Java
  7. Exception handling using try-catch
  8. Recovering without try-catch using _setRecovery
  9. Data Driven Testing
    _getDB, CSV Files, Excel, Databases
  10. Multithreaded Playback (Parallel execution)
    suites, commandline, ant
  11. Advanced techniques, tips and examples
    1. HTTPS/SSL Sites
    2. Configuring an External proxy
    3. Adding jars to Sahi's classpath
  12. Other language drivers Driving Sahi from Java, Ruby etc.
    1. Java
    2. Ruby

Browser Action APIs ·

Action APIs perform actions on the browser.

Since Sahi V2, Sahi executes Sahi scripts inside a Rhino Javascript engine in the proxy.
Actions which need to be performed on the browser are dispatched to the browser by the proxy.

The remarkable thing about these Browser Action APIs, is that the parameter expressions
passed to these APIs execute on the browser and not on the proxy.

Eg.
_click(document.form1.checkbox1) will work since document.form1.checkbox1 is evaluated on the browser.

Mouse actions

API _click(element)
Notes Clicks the element. Details
API _rightClick(element)
Notes Right clicks on the element. Details
API _doubleClick(element)
Notes Double clicks on the element. Details
API _check(checkBoxOrRadioElement)
Notes Checks the given checkbox or radio element. If already checked, this API does not do anything.
API _uncheck(checkBoxElement)
Notes Unchecks the given checkbox. If already unchecked, this API does not do anything.
API _mouseOver(element)
Notes Simulates a mouse over on the element. Details
API _dragDrop(elementToDrag, elementToDropOn)
Notes Drags elementToDrag and drops on elementToDropOn. Details
API _dragDropXY(elementToDrag, x, y[, isRelative])
Notes Drags elementToDrag and drops it on the given x, y coordinates.
If isRelative is true, drags x,y pixels relative to current position.
If isRelative is false, drags to x,y coordinate on browser.

Focus action

API _focus(element)
Notes Brings focus to element.
API _removeFocus(element)
Notes Removes focus from element.
API _blur(element)
Notes Removes focus from element.

Key actions

In the below APIs, combo can be any of “CTRL”, “ALT” or “META

API _keyPress(element, charCode, combo)
Notes Simulates a keypress event for key with given charCode on the element
API _keyDown(element, charCode, combo)
Notes Simulates a keydown event for key with given charCode on the element
API _keyUp(element, charCode, combo)
Notes Simulates a keyup event for key with given charCode on the element

Data input actions

API _setValue(element, text)
Notes Focuses on the element, types in the text and then removes focus from element.
element can be a textarea or a textbox. Details
API _setSelected(element, option_identifier, isMultiple)
Notes Selects the option with option_identifier in select element.
element can only be a select box.
isMultiple is used to select multiple values in a multi select box.
Details
API _setFile(element, filePath)
Notes Prepares the form to set file at filePath. Details
API _rteWrite(iframe, text)
Notes Writes text into a rich text editor based on content-editable iframe
API _type(element, text)
Notes Types the text into the element. Different from _setValue.
_type does not bring or remove focus from element. Rarely used.

Assertions

API _assertEqual(expected, actual, message)
Notes Asserts that expected and actual are equal, else logs the message. Details
API _assertNotEqual(expected, actual, message)
Notes Asserts that expected and actual are NOT equal, else logs the message. Details
API _assertNotNull(object, message)
Notes Asserts that the object is NOT null, else logs the message. Details
API _assertNull(object, message)
Notes Asserts that the object is null, else logs the message. Details
API _assertTrue(condition, message)
Notes Asserts that the condition is true, else logs the message. Details
API _assert(condition, message)
Notes Same as _assertTrue
API _assertNotTrue(condition, message)
Notes Asserts that the condition is NOT true, else logs the message. Details
API _assertFalse(condition, message)
Notes Asserts that the condition is false, else logs the message. Details
API _assertExists(object, message)
Notes Asserts that the object is not null, else logs the message.
API _assertNotExists(object, message)
Notes Asserts that the object does not exist (is null), else logs the message.
API _assertContainsText(expectedText, object, message)
Notes Asserts that the object contains the expectedText, else logs the message. Details
API _assertEqualArrays(expected, actual, message)
Notes Deprecated: Asserts that expected and actual arrays are equal, else logs the message.
This is internally called by _assertEqual if expected and actual are arrays.

Generic actions

API _simulateEvent(element, event)
Notes Used to simulate an event on element. Buggy (as of 21 May 2009). Will be modified.

Utility actions

API _navigateTo(url, forceReload)
Notes Navigates to the given url.
If the url is same as what is loaded on the browser, the page will be refreshed only if forceReload is true
Details
API _closeWindow()
Notes Closes the current window.
This will work only for popups.
Eg. _popop(“popWin”)._closeWindow();
API _log(message, logLevel)
Notes Logs message into the playback logs.
logLevel can be “info”, “success”, “error”, “failure” and “custom1” through “custom5”
Details
API _wait(timeInMilliseconds, condition)
Notes Waits for timeInMilliseconds ms or till the condition is satisfied on the browser,
which ever is sooner. Details
API _call(javascriptSnippet)
Notes Executes the javascriptSnippet on the browser, instead of in the Rhino engine. Details
API _eval(string)
Notes Evals the string on the browser, instead of in the Rhino engine. Details
API _execute(command[, isSynchronous])
Notes Executes any server side command that needs to be invoked via the command line or shell.
Useful for invoking other programs.
If isSynchronous is true, the function will wait till the command finishes. Details
API _callServer()
Notes Deprecated.
Calls server side code which cannnot be executed by javascript on the browser. Details
With Sahi V2, these actions can directly be performed by calling java classes from javascript through Rhino. Details
API _resetSavedRandom(key)
Notes Resets the random number associated with key, to another random number. Details
Deprecated. From Sahi V2, random numbers can be generated using _random and stored as simple variables.
Related _savedRandom , _random
API _setGlobal(key, value)
Notes Stores the value in key for retreival.
The value persists across tests in a suite, if suite.global_variables is true in sahi.properties.
Eg. _setGlobal(“userid1”, _textbox(“user”).value);
Related _getGlobal

Actions to mock out particular URLs

API _addMock(pattern[, clazz_method])
Notes Forces the proxy to process certain patterns of urls differently.
If clazz_method is not specified, sends back a simple HTML blank page.
Details
API _mockImage(pattern[, clazz_method])
Notes Similar to _addMock, but by default sends back an image. Details
API _removeMock(pattern)
Notes Removes the mock behavior added via _addMock or _mockImage for that pattern. Details

Actions to set expectations for javascript confirms and prompts

API _expectConfirm(message, boolean)
Notes Sets an expectation such that when a javascript confirm with given message appears,
based on the boolean value, OK or Cancel will be processed. Details
API _expectPrompt(message, text)
Notes Sets an expectation such that when a javascript prompt with given message appears,
the given text is populated. Details
API _clearPrintCalled()
Notes resets the value of _printCalled
API _clearLastAlert()
Notes Clears the value returned by _lastAlert().
API _clearLastPrompt()
Notes Clears the value returned by _lastPrompt().
API _clearLastConfirm()
Notes Clears the value returned by _lastConfirm().

Actions used for debugging

API _highlight(element)
Notes Highlights an element with a red border. Used for debugging purposes.
API _alert(message)
Notes Brings up a javascript alert with given message. Use only while debugging
API _prompt(message)
Notes Brings up a javascript prompt with given message. Use only while debugging
API _confirm(message)
Notes Brings up a javascript confirm with given message. Use only while debugging
API _debug(message)
Notes Prints the message on to the console for debugging purposes.
API _debugToErr(message)
Notes Prints the message on to the error stream for debugging purposes.
API _debugToFile(message, filePath)
Notes Prints the message into a file for debugging purposes.

Toggle KeepAlive Actions

API _enableKeepAlive()
Notes Details
API _disableKeepAlive()
Notes Details

Cookie related actions

Also look at _cookie

API _createCookie(name, value, days)
Notes Creates a cookie with name and value which will expire in given days.
Details
API _deleteCookie(name)
Notes Deletes cookie with given name.
Details

Print related actions

API _clearPrintCalled()
Notes resets the value of _printCalled

Download related actions

Sahi handles file downlaods by silently downloading files into sahi/temp/download folder.
The last downloaded file can be manipulated by the following APIs.
Note that a _wait() statement may be required before _saveDownloadedAs in case the file is big and takes a lot of time to download.

API _saveDownloadedAs(filePath)
Notes Saves the file which was downloaded into sahi/temp/folder into the given filePath
API _clearLastDownloadedFileName()
Notes Clears the _lastDownloadedFileName (so that new files downloaded with the same fileName can be asserted)
Related _lastDownloadedFileName

---