18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
Calling a funtion in another sahi script file
Hello Sahi Users,
I have a litte doubt in writing down few script.
Question backgroud:
I have a .sah driver script in one parent folder. I have rest of the functions in another .sah script inside a sub folder under the parent folder mentioned above. These funtion in the sub folder are specific function for Application under test.
Now, I read an excel file (which contain the function flow of a test case) from the driver. And I need to execute functions one by one from the excel.
I read the excel file using _getDB API and stored the data in record set named rs.
How should I call the functions which are stored in rs?
/*Driver Script*/
var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=X:\\XXXX\\XX\\Test_Data\\FromFMRM_TestCases.xls;readOnly=false", "", "");
var $rs = [];
$rs[0] = [];
$rs = db.select("select * from [TestFlow$]");
_alert($rs.length);
//For reading or traversing through row for all test cases
for(var $i=0; $i<$rs.length; $i++)
{
//For reading or traversing through columns for the test flow of a test case, number is set to 256 as office 2003 support so much of columncolumn as max
for(var $j=2; $j<256; $j++)
{
//Read till the cell value is blank, if blank
if ($rs[$i][$j]=="null")
{
//Breaking when keyword is not present or blank
//End of test case execution
_alert("Test case "+$rs[$i][0]+" Execution completed");
break;
}
else
{
_include ("TestFlow_Functions\TestFlow.sah");
//Here I need to call a function in TestFlow_Functions\TestFlow.sah script?
//And the script name is in variable value $rs[$i][$j]
//
}
}
}
/*Sample TestFlow.sah*/
//Function to Login into the site
function LoginWebPage()
{
_alert("This is LoginWebPage function at TestFlow script");
}
//Function to Navigate to particular page
function NavigatePage()
{
_alert("NavigatePage");
}
//Function to Book the required ticket
function BookTicket()
{
_alert("BookTicket");
}
I have a litte doubt in writing down few script.
Question backgroud:
I have a .sah driver script in one parent folder. I have rest of the functions in another .sah script inside a sub folder under the parent folder mentioned above. These funtion in the sub folder are specific function for Application under test.
Now, I read an excel file (which contain the function flow of a test case) from the driver. And I need to execute functions one by one from the excel.
I read the excel file using _getDB API and stored the data in record set named rs.
How should I call the functions which are stored in rs?
/*Driver Script*/
var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=X:\\XXXX\\XX\\Test_Data\\FromFMRM_TestCases.xls;readOnly=false", "", "");
var $rs = [];
$rs[0] = [];
$rs = db.select("select * from [TestFlow$]");
_alert($rs.length);
//For reading or traversing through row for all test cases
for(var $i=0; $i<$rs.length; $i++)
{
//For reading or traversing through columns for the test flow of a test case, number is set to 256 as office 2003 support so much of columncolumn as max
for(var $j=2; $j<256; $j++)
{
//Read till the cell value is blank, if blank
if ($rs[$i][$j]=="null")
{
//Breaking when keyword is not present or blank
//End of test case execution
_alert("Test case "+$rs[$i][0]+" Execution completed");
break;
}
else
{
_include ("TestFlow_Functions\TestFlow.sah");
//Here I need to call a function in TestFlow_Functions\TestFlow.sah script?
//And the script name is in variable value $rs[$i][$j]
//
}
}
}
/*Sample TestFlow.sah*/
//Function to Login into the site
function LoginWebPage()
{
_alert("This is LoginWebPage function at TestFlow script");
}
//Function to Navigate to particular page
function NavigatePage()
{
_alert("NavigatePage");
}
//Function to Book the required ticket
function BookTicket()
{
_alert("BookTicket");
}
This discussion has been closed.
Comments
It is not very apparent what the problem is, but here are a couple of pointers:
1) _include does a static include during parsing. It cannot be done dynamically. So you need to include all the functions and call the necessary ones when needed.
2) To dynamically execute a javascript string, you can use eval("js string")
3) If you are trying to do eval("_click(_link(0))"), you may need to do eval("_sahi._click(_sahi._link(0))") since Sahi's pre processing may not work here.
Hope that helps.
Regards,
Narayan
I understand sahi does a static parsing so the "_include" pull the all content of the included file onto the called file and places in the called position. I understood this when I looked at the parsed script from the controller.
I should have been little more detailed on my question. Here is the problem
I need to call the a sahi function available in another .sah file dynamically. (The script calling is the driver script and called script contains the functions of the application). So basically I need call a function available in "TestFlow.sah" file from "Driver Script". Now the catch is, the input or which function to execute is coming from excel reading.
Below is the example. My excel file will have the keyword to be called (actually sahi funtion available in TestFlow.sah). I read this excel file from the driver script.
Example:
| Keyword 1 | Keyword 2 |
TC1 | LoginWebPage | NavigatePage |
TC2 | NavigatePage | BookTicket |
TC3 | xyz | |
TC4 | yze | def |
so the test case will flow through the keyword flow. And if any keyword is null the test case is completed Else sahi driver script has to call the function returned by "$rs[$i][$j]" in the else clause.
if ($rs[$i][$j]=="null")
{
//Breaking when keyword is not present or blank
//End of test case execution
_alert("Test case "+$rs[$i][0]+" Execution completed");
break;
}
else
{
_include ("TestFlow_Functions\TestFlow.sah");
//Here I need to call a function in TestFlow_Functions\TestFlow.sah script?
//And the script name is in variable value $rs[$i][$j]
$rs[$i][$j] + "()";
/*Point: How to call the function here; lets say by example value "LoginWebPage" is the $rs[$i][$j], then I need to call "LoginWebPage" which is available in TestFlow.sah. Next iteration will bring "NavigatePage" as value of $rs[$i][$j], then I need to call "NavigatePage" function from TestFlow.sah.*/
/*Also I need to pass some parameters from driver script to testflow script*/
}
eval($rs[$i][$j] + "()");
eval($rs[$i][$j] + "()");
unlike the actual API calls like
_sahi.schedule("_sahi._alert("+s_v($rs[$i][$j])+");", "X:\\XXXX\\XX\\DriverTestFlow.sah&n=37");
As I read from the start-up document for sahi, "the Evals the string on the browser, instead of in the Rhino engine." So it is considering function as external javascript as you said I believe.
It works with my current function code, let me try by adding few sahi script in the called function and try.
Thank you for your responseagain, will get back to you in case any problem. Also if my next test works I will post my finding in the post so that you can close the post.
I used the eval($rs[$i][$j] + "()"); as you stated, now the call is getting estiablished, can I also pass varialble or values using eval itself?
eval($rs[$i][$j]+"("+$rs[$i][0]+")");
Here I want to pass the test case Id to the next function so I use as the above statement, when I tried the above statement, its not working.
can you please assist
Use:
Now I am able to call other functions, however as I posted in other post "Driving script through the excel input - Need help"
I am getting stuck when I do a select query " $AppURLValue = db1.select("select AppURL from [TestData$] where TC ='"+$CaseID+"'");"
the error says,
_alert("recursive_access");
[auto] at Dec 2, 2010 10:10:37 AM
Logging exception:
_navigateTo("recursive_access", true);
TypeError: url.replace is not a function
http://www.google.co.in/_s_/spr/concat.js:1435
http://www.google.co.in/_s_/spr/concat.js:2700
http://www.google.co.in/_s_/spr/concat.js:2700
http://www.google.co.in/_s_/spr/concat.js:2523
As you guided "recursive_access" issue has been resolved by adding [0][0] because, the db.select always returns 2D array.
Thank you,
Bala