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.

[sahi v3] using functions within _setGlobal fails

zawistolzawistol Members
edited November -1 in Sahi - Open Source
hallo,
I am migratingfrom V1 to V3 and I have found following problem

***************** DOESNT WORK IN V3, WORKS IN V1 ***********
_setGlobal("GlobalContractName","C25 " + UniqueNumber());

function UniqueNumber(){
var UniqueNumberLocal = Math.floor(Math.random()*9999999);
return UniqueNumberLocal;
}

************** ERROR MESSAGE ***************
setGlobal("GlobalContractName","C25 " + UniqueNumber());
TypeError: Object expected
No trace available
Click for browser script at May 21, 2010 10:50:04 AM



********************* WORKAROUND IN V3 ******************
_setGlobal("GlobalContractName","C25 " + Math.floor(Math.random()*9999999));

Any ideas ?

Comments

  • Hi,
    I have found another problem, that is related to this. I am using rel. 20100321.

    The following construction doesnt work
    _setSelected(_select("locType1"), DecodeGeoType($Hashcode); 
    function DecodeGeoType($Hashcode){
    if ($Hashcode== "N"){
        return "Nation";
      }
    if ($Hashcode== "C"){
        return "City";
      }
    }
    


    To make it work, you need to add following line before
    var $var1= DecodeGeoType($Hashcode);
    
    and use $var1 instead of direct function call


    Lech
  • narayannarayan Administrators
    Hi Lech,

    There were quite a few changes from Sahi V1 to V3. One of the main things was moving execution away from the browser and into the proxy. This helped solve a lot of issues with loops, ifs etc. but made the older scripts incompatible with the newer Sahi.


    To explain the behaviour:

    Your code:
    function UniqueNumber(){
        var UniqueNumberLocal = Math.floor(Math.random()*9999999);
        return UniqueNumberLocal;
    }
    _setGlobal("GlobalContractName","C25 " + UniqueNumber());
    


    function UniqueNumber is defined in Rhino's runtime running inside the proxy. _setGlobal("GlobalContractName","C25 " + UniqueNumber()); is converted to a string
    '_setGlobal("GlobalContractName","C25 " + UniqueNumber());' and 'eval'ed on the browser (This happens for all Browser Action APIs). So UniqueNumber would not be found on the browser, and hence fail. Math.random is inbuilt in JS, so is available both on the proxy and the browser.


    _setGlobal is no longer required in V3. You can just use regular variables.
    Eg.
    function UniqueNumber(){
        var UniqueNumberLocal = Math.floor(Math.random()*9999999);
        return UniqueNumberLocal;
    }
    $GlobalContractName = "C25 " + UniqueNumber();
    



    The same thing happens with
    _setSelected(_select("locType1"), DecodeGeoType($Hashcode)); 
    function DecodeGeoType($Hashcode){
    if ($Hashcode== "N"){
        return "Nation";
      }
    if ($Hashcode== "C"){
        return "City";
      }
    }
    

    String '_setSelected(_select("locType1"), DecodeGeoType($Hashcode);' will be 'eval'ed on the browser and DecodeGeoType will not be found. There are two solutions to this.

    do
    var $var1= DecodeGeoType($Hashcode);
    _setSelected(_select("locType1"), $var1); 
    

    or define the function within < browser > tags, in which case, the function will be made available on the browser rather than in the proxy.
    <browser>
    function DecodeGeoType($Hashcode){
    if ($Hashcode== "N"){
        return "Nation";
      }
    if ($Hashcode== "C"){
        return "City";
      }
    }
    </browser>
    
    _setSelected(_select("locType1"), DecodeGeoType($Hashcode));
    


    Prefixing a $ basically makes values of proxy side variables available to the browser. so

    $x = "abc";
    _alert($x);

    will become

    evalOnBrowser("_alert(" + $x + ")"); (with the required quotes around $x)

    You can use $ as a start of a function also, for example, you could do
    _setSelected(_select("locType1"), $DecodeGeoType($Hashcode)); 
    function $DecodeGeoType($Hashcode){
    if ($Hashcode== "N"){
        return "Nation";
      }
    if ($Hashcode== "C"){
        return "City";
      }
    }
    

    This will become
    evalOnBrowser('_setSelected(_select("locType1"), ' + $DecodeGeoType($Hashcode) + ');')


    Hope that helps.
    Regards,
    Narayan
  • Narayan,
    Using $myfunction($param) instead of myfunction($param),
    both for function declaration and invocation
    works perfectly for me in SAHI V3
    Great thanks!
    Lech
This discussion has been closed.