Logging APIs
Logging APIs allow adding additional information to playback logs.
Modes BrowserWindowsJavaAndroidiOSSAP
Parameters
| Name | Type | Description |
|---|---|---|
$message | string | Message to log |
$logType | stringoptional | Specifies the type of log statement. FAILURE will mark the step as failed (in red) and also fail the script. The other modes display the step in different colors. Can be one of "INFO", "SUCCESS", "FAILURE", "CUSTOM", "CUSTOM1", "CUSTOM2", "CUSTOM3", "CUSTOM4", "CUSTOM5" Defaults to "INFO" |
$imagePath | stringoptional | Path to image file which will be logged along with the message. Relative paths will be resolved relative to files folder of the current project. Introduced since Sahi Pro 6.0.0 |
Raw script
_log("Current user is " + $user); // Will log as plain text
_log("Current user is " + $user, "CUSTOM1"); // Will log in a different color
_log("Current user is " + $user, "INFO", "tiger.png"); // Will also add tiger.png in logs.Sahi Pro Classic API _log
Log Exception
Section titled “Log Exception”Modes BrowserWindowsJavaAndroidiOSSAP
Log Exception is used within a catch block to log the Exception in playback logs. The logged line will be in plain text and the script status will not be affected by this.
Parameters
| Name | Type | Description |
|---|---|---|
$exception | Exception | Exception passed to catch block |
Raw script
try{ _click(_link("does not exist"));}catch($e){ _logException($e); // Logs the exception, but does not fail}Sahi Pro Classic API _logException
Log Exception As Failure
Section titled “Log Exception As Failure”Modes BrowserWindowsJavaAndroidiOSSAP
Log Exception As Failure is used within a catch block to log the Exception in playback logs. The logged line will be in red and the script will be marked failed because of this.
Parameters
| Name | Type | Description |
|---|---|---|
$exception | Exception | Exception passed to catch block |
Raw script
try{ _click(_link("does not exist"));}catch($e){ _logExceptionAsFailure($e); // Logs the exception, and fails, // and in the logs, points to the original line as source of failure.}Sahi Pro Classic API _logExceptionAsFailure
Log Image
Section titled “Log Image”Modes BrowserWindowsJavaAndroidiOSSAP
Parameters
| Name | Type | Description |
|---|---|---|
$imagePath | string | Path to image file which will be logged along with the message. Relative paths will be resolved relative to files folder of the current project. |
$message | stringoptional | Message to log |
$logType | stringoptional | Changes the color in which log statement is displayed. Can be one of "INFO", "SUCCESS", "FAILURE", "CUSTOM", "CUSTOM1", "CUSTOM2", "CUSTOM3", "CUSTOM4", "CUSTOM5" Defaults to "INFO" |
Raw script
_logImage("../tiger.png"); // Will add the image to logs
_logImage("C:/tiger.png", "This is a tiger"); // Will log image along with message
_logImage("C:/tiger.png", "This is a tiger in custom log message", "CUSTOM1");// Will log image with message in a different colorSahi Pro Classic API _logImage
Log Output
Section titled “Log Output”Modes BrowserWindowsJavaAndroidiOSSAP
Log Output allows concatenating multiple output messages during a script execution, and display the concatenated string in the script and suite reports.
Sample Script:
Parameters
| Name | Type | Description |
|---|---|---|
$message | string | Message to log |
Raw script
_logOutput("Script started."); // Logging output: Script started._setValue(_textbox("user"), "test");_setValue(_password("password"), "secret");_click(_submit("Login"));_logOutput("Login passed."); // Logging output: Login passed._click(_button("Logout"));_logOutput("Script stopped."); // Logging output: Script stopped.Sahi Pro Classic API _logOutput
Artifact
Section titled “Artifact”Modes BrowserWindowsJavaAndroidiOSSAP
Artifact is used to save a file as part of the logs. For example, one may want to store a report from your application along with the script report. Each artifact is uniquely named and associated with a script report, so that multiple runs do not over write the same artifact file.
A link to the artifact will be visible on the script report. All artifacts in a suite can be downloaded as a zip from the suite report page.
Parameters
| Name | Type | Description |
|---|---|---|
$artifactPath | string | File path of the artifact files. If artifactPath is a relative path, it is evaluated relative to the files folder of the current project. Can be folder path. Wildcard * is also supported. |
$label | stringoptional | Label to add to the artifacts. If the label is not specified its default value will be artifactPath. |
Raw script
_artifact("data.xls"); // Logging artifact: data.xls
_artifact("../myartifacts/*.txt", "all"); // Logging artifact: allSahi Pro Classic API _artifact
Masking logs
Section titled “Masking logs”Mask Logs
Section titled “Mask Logs”Modes BrowserWindowsJavaAndroidiOSSAP
Mask Logs tells Sahi not to display further steps in the playback logs and in the Controller. This is used when sensitive information like password may be entered via script, but we do not want it stored/displayed anywhere Toggled by _unmaskLogs.
Parameters
| Name | Type | Description |
|---|---|---|
$message | stringoptional | Message to print when logging this mask statement |
Sahi Pro Classic API _maskLogs
Unmask Logs
Section titled “Unmask Logs”Modes BrowserWindowsJavaAndroidiOSSAP
Unmask Logs tells Sahi to start displaying steps in the playback logs and in the Controller. Used after _maskLogs.
Parameters
| Name | Type | Description |
|---|---|---|
$message | stringoptional | Message to print when logging this unmask statement |
Sahi Pro Classic API _unmaskLogs
Example of _maskLogs _unmaskLogs
Section titled “Example of _maskLogs _unmaskLogs”_setValue(_textbox("user"), "test");_maskLogs("Password Information start");_setValue(_password("password"), "secret");_unmaskLogs("Password Information end");_click(_submit("Login"));will show this in logs:
Playback Logs

Controller Playback Tab

Disable/Enable INFO logging
Section titled “Disable/Enable INFO logging”Disable Info Logging
Section titled “Disable Info Logging”Modes BrowserWindowsJavaAndroidiOSSAP
Disables logging of INFO (normal) steps in playback logs. Only SUCCESS, FAILURE and CUSTOM steps are logged.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _disableInfoLogging
Enable Info Logging
Section titled “Enable Info Logging”Modes BrowserWindowsJavaAndroidiOSSAP
Enables logging of INFO (normal) steps in playback logs which was previously disabled using _disableInfoLogging.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _enableInfoLogging
Disable/Enable error logging
Section titled “Disable/Enable error logging”Disable Default Error Logging
Section titled “Disable Default Error Logging”Modes BrowserWindowsJavaAndroidiOSSAP
Disables automatic logging of errors. Used in exception handling. See Corrective Action and Log the Exception Message Corrective Action, Log and then Fail.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _disableDefaultErrorLogging
Enable Default Error Logging
Section titled “Enable Default Error Logging”Modes BrowserWindowsJavaAndroidiOSSAP
Enables automatic logging of errors.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _enableDefaultErrorLogging
Code Folding
Section titled “Code Folding”When a function is called in a Sahi script, the logs for that function call are automatically “folded” in the reports.

Disable Code Folding
Section titled “Disable Code Folding”Sometimes, for commonly called utility functions, one may choose not to log the code folded function. This can be done by adding
fnName.noLog = trueafter the function definition.
Example.
function getTimeStamp() { return (new Date()).toString();}getTimeStamp.noLog = true;HAR Logging
Section titled “HAR Logging”HTTP request response information summaries can be captured during playback. (HAR refers to HTTP ARchive format of storing request response information)
If enabled, a log step may look like this:

It can be enabled globally or locally.
Globally enable HAR Logging
Section titled “Globally enable HAR Logging”Add
har_logging.enabled=truein userdata.properties Restart Sahi for this to take effect.
Locally enable HAR Logging
Section titled “Locally enable HAR Logging”Locally enable or disable HAR logging inside a script using _startHarLogging() and _stopHarLogging() APIs.
Start Har Logging
Section titled “Start Har Logging”Modes Browser
Tells Sahi to start logging HTTP request/response information for all network calls in this script.
Parameters
| Name | Type | Description |
|---|---|---|
$enableResponseContent | booleanoptional | By default, response content will not be stored in HAR logs. Pass true to store response content. |
Sahi Pro Classic API _startHarLogging
Stop Har Logging
Section titled “Stop Har Logging”Modes Browser
Tells Sahi to stop logging HTTP request/response information for all network calls in this script.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _stopHarLogging
Get Har Logs
Section titled “Get Har Logs”Modes Browser
It returns an array of HAR logs accumulated while executing the steps between _startHarLogging and _stopHarLogging. Every HAR log object has following attributes:
- requestMethod : returns the HTTP request method
- requestUrl : returns the requested url
- requestQueryString : returns an array of query parameters in the requested url
- requestHeaders : returns the request headers as an array
- requestData : returns the request body as an array
- responseStatus : returns the HTTP response status code
- responseStatusText : returns the HTTP response status message
- responseHeaders : returns the response headers as an array
- responseBodySize : returns the size of response body in bytes
- responseContent : returns the response body if argument
enableResponseContentpassed to_startHarLoggingAPI is true - time : returns the total duration from the start of the request to receiving the entire response
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
array of objects | An array of HAR logs |
Raw script
// Example 1 - Verify request to third party application_startHarLogging();_addToSession("https://www.google-analytics.com");_navigateTo("https://www.example.com/demo");_setValue(_textbox("Name"), "test");_click(_submit("Continue"));_stopHarLogging();var $harLogs = _getHarLogs();for (var $i=0; $i<$harLogs.length; $i++) { var $harLog = $harLogs[$i]; _log( "Requested url : " + $harLog.requestUrl ); if( $harLog.requestUrl.indexOf("https://www.google-analytics.com/collect") != -1 ) { _log( "HTTP request method : " + $harLog.requestMethod ); var $parameters = $harLog.requestQueryString; for( var $j=0; $j < $parameters.length; $j++) { _log( "Query parameter " + $j + " : name=" + $parameters[$j].name + ", value=" + $parameters[$j].value); }
_log( "Response status code : " + $harLog.responseStatus ); _log( "Response status message : " + $harLog.responseStatusText ); _log( "Size of response body in bytes : " + $harLog.responseBodySize ); _log( "Total duration : " + $harLog.time ); break; }}
// Example 2 - Verify response content_navigateTo("https://www.example.com");_startHarLogging(true); // Enable reponse content in HAR logs_check(_checkbox("Order XYZ434"));_click(_submit("Approve")); // Approve Order XYZ434_wait(2000, _isVisible(_div("Success")));_stopHarLogging();var $harLogs = _getHarLogs();for (var $i=0; $i<$harLogs.length; $i++) { var $harLog = $harLogs[$i]; if( $harLog.requestUrl.indexOf("https://www.example.com/approve") != -1 ) { _log( "Response status code : " + $harLog.responseStatus ); _log( "Response status message : " + $harLog.responseStatusText );
var $responseContent = $harLog.responseContent; // Server has sent a JSON containing the receipt number. _log( "Receipt number : " + JSON.parse($responseContent).receiptNo); break; }}Sahi Pro Classic API _getHarLogs

