Logging APIs
abstract
Logging APIs allow adding additional information to playback logs.
_log
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_log($message[, $logType[, $imagePath]])
Arguments
$message | string | Message to log |
$logType | string optional | 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 | string optional | Path to image file which will be logged along with the message. Relative paths will be resolved relative to current script. Introduced since Sahi Pro 6.0.0
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 |
Returns
null |
Sahi Pro Flowcharts Action :Log
Details
_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.
_logException
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_logException($exception)
Arguments
$exception | Exception | Exception passed to catch block |
Returns
null |
Sahi Pro Flowcharts Action :Log Exception
Details
_logException 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.
_logException 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.
try{
_click(_link("does not exist"));
}catch($e){
_logException($e); // Logs the exception, but does not fail
}
_logExceptionAsFailure
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
3.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_logExceptionAsFailure($exception)
Arguments
$exception | Exception | Exception passed to catch block |
Returns
null |
Sahi Pro Flowcharts Action :Log Exception As Failure
Details
_logExceptionAsFailure 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.
_logExceptionAsFailure 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.
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.
}
_logImage
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 7.5.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_logImage($imagePath[, $message[, $logType]])
Arguments
$imagePath | string | Path to image file which will be logged along with the message. Relative paths will be resolved relative to current script.
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 | string optional | Message to log |
$logType | string optional | 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" |
Returns
null |
Sahi Pro Flowcharts Action :Log Image
Details
_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 color
_logOutput
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.2.0 | 7.5.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_logOutput($message)
Arguments
$message | string | Message to log |
Returns
null |
Sahi Pro Flowcharts Action :Log Output
Details
_logOutput allows concatenating multiple output messages during a script execution, and display the concatenated string in the script and suite reports.
Sample Script:
For the above script, the following message will be displayed in script report:
_logOutput allows concatenating multiple output messages during a script execution, and display the concatenated string in the script and suite reports.
Sample 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.
For the above script, the following message will be displayed in script report:
_artifact
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.2.0 | 7.5.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_artifact($artifactPath[, $label])
Arguments
$artifactPath | string | File path of the artifact files.
If artifactPath is a relative path, it is evaluated relative to the script that includes it.
Can be folder path. Wildcard * is also supported.
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 | string optional | Label to add to the artifacts. If the label is not specified its default value will be artifactPath. |
Returns
null |
Sahi Pro Flowcharts Action :Artifact
Details
_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.
_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.
_artifact("data.xls"); // Logging artifact: data.xls
_artifact("../myartifacts/*.txt", "all"); // Logging artifact: all
Masking logs
_maskLogs
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_maskLogs([$message])
Arguments
$message | string optional | Message to print when logging this mask statement |
Returns
null |
Sahi Pro Flowcharts Action :Mask Logs
Details
_maskLogs 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.
_maskLogs 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.
_unmaskLogs
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.5 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_unmaskLogs([$message])
Arguments
$message | string optional | Message to print when logging this unmask statement |
Returns
null |
Sahi Pro Flowcharts Action :Unmask Logs
Details
_unmaskLogs tells Sahi to start displaying steps in the playback logs and in the Controller. Used after _maskLogs.
_unmaskLogs tells Sahi to start displaying steps in the playback logs and in the Controller. Used after _maskLogs.
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
_disableInfoLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.5.2 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_disableInfoLogging()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Disable Info Logging
Details
Disables logging of INFO (normal) steps in playback logs. Only SUCCESS, FAILURE and CUSTOM steps are logged.
Disables logging of INFO (normal) steps in playback logs. Only SUCCESS, FAILURE and CUSTOM steps are logged.
_enableInfoLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.5.2 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_enableInfoLogging()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Enable Info Logging
Details
Enables logging of INFO (normal) steps in playback logs which was previously disabled using
Enables logging of INFO (normal) steps in playback logs which was previously disabled using
_disableInfoLogging
.
Disable/Enable error logging
_disableDefaultErrorLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_disableDefaultErrorLogging()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Disable Default Error Logging
Details
Disables automatic logging of errors. Used in exception handling. See Corrective Action and Log the Exception Message Corrective Action, Log and then Fail.
Disables automatic logging of errors. Used in exception handling. See Corrective Action and Log the Exception Message Corrective Action, Log and then Fail.
_enableDefaultErrorLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_enableDefaultErrorLogging()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Enable Default Error Logging
Details
Enables automatic logging of errors.
Enables automatic logging of errors.
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
Sometimes, for commonly called utility functions, one may choose not to log the code folded function. This can be done by addingfnName.noLog = true
after the function definition.
Example.
function getTimeStamp() {
return (new Date()).toString();
}
getTimeStamp.noLog = true;
Demarcating Test Cases
Sections of a script may need to be marked as separate testcases while viewing in logs. For example a single script can exercise login, verifyTotal and logout, and may be considered as 3 testcases in a single script._testcase
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
4.0 | 7.0.0 | 7.5.0 | 9.0.0 | NA |
Available for modes: Browser | Windows | Java | Android | iOS | SAP
_testcase($testcaseId, $description)
Arguments
$testcaseId | string | Unique identifier for testcase. No square brackets should be present in the testcaseId. |
$description | string | Description to be shown in logs |
Returns
null |
Sahi Pro Flowcharts Action :Testcase
Details
Demarcates a testcase (a logical boundary of steps)
The logs of the above script will look like this: Also refer to csv files as suites.
Demarcates a testcase (a logical boundary of steps)
dangerDEPRECATED:
Use Scenario Framework or _runUnitTests instead.
_navigateTo("/demo/training/");
var $t = _testcase("TC_1", "Login");
$t.start();
_setValue(_textbox("user"), "test");
_setValue(_password("password"), "secret");
_click(_submit("Login"));
$t.end();
var $t2 = _testcase("TC_2", "Add books").start(); // can be in one line also
_setValue(_textbox("q"), "2");
_setValue(_textbox("q[1]"), "1");
_setValue(_textbox("q[2]"), "1");
_click(_button("Add"));
$t2.end();
var $t3 = _testcase("TC_8", "Verify total");
$t3.start();
_assertExists(_textbox("total"));
_assert(_isVisible(_textbox("total")));
_assertEqual("1150", _textbox("total").value);
$t3.end();
The logs of the above script will look like this: Also refer to csv files as suites.
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
Addhar_logging.enabled=true
in userdata.properties
Restart Sahi for this to take effect.
Locally enable HAR Logging
Locally enable or disable HAR logging inside a script using_startHarLogging()
and _stopHarLogging()
APIs.
_startHarLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | NA | NA | NA | NA |
Available for modes: Browser
_startHarLogging([$enableResponseContent])
Arguments
$enableResponseContent | boolean optional | By default, response content will not be stored in HAR logs. Pass true to store response content. |
Returns
null |
Sahi Pro Flowcharts Action :Start Har Logging
Details
Tells Sahi to start logging HTTP request/response information for all network calls in this script.
Tells Sahi to start logging HTTP request/response information for all network calls in this script.
_stopHarLogging
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | NA | NA | NA | NA |
Available for modes: Browser
_stopHarLogging()
Arguments
None |
Returns
null |
Sahi Pro Flowcharts Action :Stop Har Logging
Details
Tells Sahi to stop logging HTTP request/response information for all network calls in this script.
Tells Sahi to stop logging HTTP request/response information for all network calls in this script.
_getHarLogs
Since: | Sahi Pro | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
9.0.0 | NA | NA | NA | NA |
Available for modes: Browser
_getHarLogs()
Arguments
None |
Returns
array of objects | An array of HAR logs |
Sahi Pro Flowcharts Action :Get Har Logs
Details
It returns an array of HAR logs accumulated while executing the steps between
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
enableResponseContent
passed to_startHarLogging
API is true - time : returns the total duration from the start of the request to receiving the entire response
// 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;
}
}
info
- Case 1: If
_getHarLogs
is invoked after _startHarLogging
and _stopHarLogging
, then it gets the log accumulated while executing steps between _startHarLogging
and _stopHarLogging
.- Case 2: If
_getHarLogs
is invoked between _startHarLogging
and _stopHarLogging
, then it gets the log accumulated while executing steps between _startHarLogging
and _getHarLogs
.- Case 3: If there are multiple
_startHarLogging
and _stopHarLogging
pairs, it gets the HAR logs corresponding to the recent _startHarLogging
and _stopHarLogging
APIs.- Case 4: If
_startHarLogging
API is not there in the script while HAR logging is enabled from userdata properties, then it gets the HAR logs accumulated while executing steps between the first step and the step for _getHarLogs
.- Case 5: If
_startHarLogging
API is not there in the script and also HAR logging is disabled in the userdata properties file, the it returns empty string.warning
If the domain in a request URL is different than the domain of the invoking page, then the HAR log for this request will not be captured. To solve this problem, add this domain to the current script session using _addToSession API before this request gets sent.