18 January 2026:


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

ratan.angadiratan.angadi Members
edited November -1 in Sahi - Open Source
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.

Comments

  • JaoBrJaoBr Members
    Here what worked for me: http://sahi.co.in/forums/viewtopic.php?id=1454

    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 :D).
  • Thanks for the response JaoBr,

    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 for all responses, I tried using sahi api's in eval function many ways and I don't think Sahi would allow its API's to be used in eval function. I feel as eval is more related to javascript Sahi would not allow its API's to be used in eval function. If some one had success with it please share !

    Thanks,
    Ratan.
Sign In or Register to comment.