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.
Converting string to function name
Hi all,
I have searched the posts related to my problem and found one but it didn't work for me, below code works perfect in javascript,
function callme() {
var x="alert";
eval(x)('Hello'); }
but when i try the same code in sahi as below, doesn't work.
var x="_alert";
eval(x)('Hello');
I actually want to covert strings like "_div" to function names so i can use them as below,
_assertContainsText("x", [function name] ("xy"));
and also I tried the below line which didn't work for me,
var $f="_div";
var $fname=eval($f);
_assertContainsText("x", $fname ("xy"));
could you please give me some hint what might be the problem ? am I missing something ?
Thanks in advance,
Ratankumar.
I have searched the posts related to my problem and found one but it didn't work for me, below code works perfect in javascript,
function callme() {
var x="alert";
eval(x)('Hello'); }
but when i try the same code in sahi as below, doesn't work.
var x="_alert";
eval(x)('Hello');
I actually want to covert strings like "_div" to function names so i can use them as below,
_assertContainsText("x", [function name] ("xy"));
and also I tried the below line which didn't work for me,
var $f="_div";
var $fname=eval($f);
_assertContainsText("x", $fname ("xy"));
could you please give me some hint what might be the problem ? am I missing something ?
Thanks in advance,
Ratankumar.
Comments
But I don't know if it is right or if you can use APIs like "_div" or "_alert" in eval, why don't you use them by the way Sahi already does?
I just don't see the point in using:
var x="_alert";
eval(x)('Hello');
If you can just use _alert("Hello");
But try using:
var x="_alert(\"Hello\")";
eval(x);
or:
var x="_alert";
x = x + "(\"Hello\")";
eval(x);
(I don't remember how to use quotes inside quotes
Well my idea is to have a one function for assertions for test , for example
function check(element)
{
var $f=element;
var $fname=eval($f);
_assertContainsText("x", $fname ("xy"));
}
the above function would assert and verify the element I send for text. If I'm not wrong it'd save a lot time and line of script. I'd simply send _div to assert division etc. correct me if I'm wrong.
Thanks,
Ratan.