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.

V2 - can functions be called to fill parameters in function calls

johnjohn Members
edited November -1 in Sahi - Open Source
Hi,

I wrote a lot of scripts for sahi using pre V2 sahi controller. I just upgrated to V2 and the scripts don't work! I've tracked the issue down to how functions are called/referenced in the test scripts

An example script -
function returnValue()
{
    return 'wibble';
}

var $tmp = returnValue();
_log( $tmp );

_log( returnValue() );
When I execute this the first _log() call succeeds and the second one fails stating that returnValue is not defined. The actual output is:
Starting script
wibble
_log( "wibble" );
_log( returnValue() ); ReferenceError: returnValue is not defined
()@http://www.XX.com/_s_/spr/concat.js:2286
()@http://www.XX.com/_s_/spr/concat.js:2286
@http://www.XX.com/_s_/spr/concat.js:2193
Stopping script
Does anyone have any idea of what is going on? It looks like you can no longer call functions to fill parameters to other function calls.

This used to work before - and I've got about 200 scripts to change if I can't do this any more!!

John

Comments

  • Hi,

    It may have something to do with the <browser></browser> tags that need to be put around your function. But I think they are needed only when the function is doing something on the page's DOM objects. Anyways you can just give it a try.
    Go through this: http://sahi.co.in/w/scripting-changes-in-sahi-v2

    Regards,
    Pankaj.
  • johnjohn Members
    edited October 2008
    Hi,

    It may have something to do with the <browser></browser> tags that need to be put around your function. But I think they are needed only when the function is doing something on the page's DOM objects. Anyways you can just give it a try.
    Go through this: http://sahi.co.in/w/scripting-changes-in-sahi-v2

    Regards,
    Pankaj.
    Thanks Pankaj, I didn't relase that I need to put the browser tags in. However, when I add the <browser> tags into the scripts all hell breaks loose!

    1) the script fails with missing parameter errors
    Starting script
    jserror missing formal parameter (#30)
    Stopping script
    
    2) on the command line for the server, an exception is thrown
    #### Running Script: S:\scripts\scenarios\standard-rate\setup2.sah
    Shutting down ...
    org.mozilla.javascript.EvaluatorException: missing formal parameter (<cmd>#30)
            at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErro
    Reporter.java:95)
            at org.mozilla.javascript.DefaultErrorReporter.error(DefaultErrorReport
    r.java:82)
            at org.mozilla.javascript.Parser.addError(Parser.java:123)
            at org.mozilla.javascript.Parser.reportError(Parser.java:129)
            at org.mozilla.javascript.Parser.mustMatchToken(Parser.java:215)
            at org.mozilla.javascript.Parser.function(Parser.java:505)
            at org.mozilla.javascript.Parser.parse(Parser.java:345)
            at org.mozilla.javascript.Parser.parse(Parser.java:290)
            at org.mozilla.javascript.Context.compileImpl(Context.java:2250)
            at org.mozilla.javascript.Context.compileString(Context.java:1296)
            at org.mozilla.javascript.Context.compileString(Context.java:1285)
            at org.mozilla.javascript.Context.evaluateString(Context.java:1141)
            at net.sf.sahi.rhino.ScriptRunner.run(ScriptRunner.java:52)
            at java.lang.Thread.run(Unknown Source)
    Player.stop: sahi_1223962624906sahix5291x
    
    any ideas?
  • pankaj.nithpankaj.nith Members
    edited October 2008
    Hi,

    Its hard to comment without looking at the code right now. Just check once that you have not missed passing any parameter in some function call or I think you might have called a function with some extra parameter (more than required number). Check out if something is wrong at line number #30 of setup2.sah

    All the best.
    Pankaj.
  • narayannarayan Administrators
    edited October 2008
    John,

    In Sahi V2 the script executes in the proxy, but those steps which use scheduler functions execute on the browser.
    Sahi does this by first parsing your script and then "scheduling" some steps for the browser.

    In your case
    function returnValue()
    {
        return 'wibble';
    }
    
    var $tmp = returnValue();
    _log( $tmp );
    
    _log( returnValue() );
    
    when parsed, becomes
    function returnValue()
    {
    return 'wibble';
    }
    var $tmp = returnValue();
    _sahi.schedule("_sahi._log( "+s_v($tmp)+" );", "D:\\sahi_v2\\bin\\..\\scripts\\_1.sah&n=7");
    _sahi.schedule("_sahi._log( returnValue() );", "D:\\sahi_v2\\bin\\..\\scripts\\_1.sah&n=9");
    
    In the last two steps, notice how the steps in your original script have been modified to become strings and are being scheduled. The browser will pick these strings from the proxy and eval them.

    The line "var $tmp = returnValue();" is executed in the proxy and since returnValue() is defined just above, it works fine.

    The line "_log( returnValue() );" becomes "_sahi.schedule("_sahi._log( returnValue() );", "D:\\sahi_v2\\bin\\..\\scripts\\_1.sah&n=9");", and the string "_sahi._log( returnValue() );" will be evaled on the browser. But the browser does not have "returnValue() " defined. You can make something available to the browser by wrapping them in <browser></browser> tags.

    To make your code work, you have to have another returnValue() function defined within browser tags.
    <browser>
    function returnValue()
    {
        return 'wibble';
    }
    
    </browser>
    function returnValue()
    {
        return 'wibble';
    }
    
    var $tmp = returnValue();
    _log( $tmp );
    
    _log( returnValue() );
    
  • johnjohn Members
    edited October 2008
    Narayn,

    Thanks for the detailed response.

    For the code example I provided the <browser> tags do work. I tried the same approach in my test scripts but I was still getting the same error.

    I did some more digging about and discovered that if the function is defined in a separate file the code fails

    e.g. If we continue to use the trivial code example and extend it over an include file with the function definitions and a main file giving:
    // the include file with function definitions
    <browser>
    
    function returnValue()
    {
        return 'wibble';
    }
    </browser>
    
    function returnValue()
    {
        return 'wibble';
    }
    
    In the main the 'test script' we include this file -
    _include('test-include.sah');
    
    var $tmp = returnValue();
    _log( $tmp );
    
    _log( returnValue() );
    
    the output of this is
    Starting script
    wibble
    _log( "wibble" );
    _log( returnValue() ); ReferenceError: returnValue is not defined
    ()@http://www.XX.com/_s_/spr/concat.js:2286
    ()@http://www.XX.com/_s_/spr/concat.js:2286
    @http://www.XX.com/_s_/spr/concat.js:2193
    Stopping script
    
    To verfiy this was the root cause of the issue I'm seeing I tried copying the helper functions into the same script as the main test - the test executed correctly.

    Do you have any recommendations? It will not be manageable for me to copy all my helper functions into all my test scripts.

    Thanks for your continued support

    John
  • narayannarayan Administrators
    That surely is a bug. Shall make a release with a fix for this before next week.
  • johnjohn Members
    narayan wrote:
    That surely is a bug. Shall make a release with a fix for this before next week.
    Ok thanks.

    I'll test the fix for ya! :)
Sign In or Register to comment.