tristaanogre's avatar
tristaanogre
Esteemed Contributor
8 years ago
Status:
New Idea

Allow use of Script Function default parameters in Keyword Tests

We have a number of JavaScript script function libraries that we are using in our automated test scenarios.  Our primary vehicle for creating those scenarios is through keyword tests.

 

One thing we've run into is that, occasionally, we need to modify one of the script functions to have another parameter.  What I do is add the parameter and mark it to have a default value (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) so that, if that parameter is not passed, it uses the default value.

 

This works great for functions that are called from other script functions.  However, if I call a function within a keyword test and add a new parameter to the function, the keyword test will fail with an exception indicating that the parameter count is incorrect.

 

What would be great is if the Keyword engine, when reading a function for execution, can recognize when such default parameter values are defined and be able to execute the script without needing to update the keyword test.  Currently, if we add a parameter to a function, we have to go back and update all of the keyword tests where it is called to have the new set of parameters, even if we're just going to use the default.

2 Comments

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    I wish we could just have the parameter lists updated automatically instead of having to run into an Exception to find the ones we forgot.  

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    After I posted this, I did some playing around and found the following.  Now, this is all using JavaScript so I can't vouch for other code languages.

     

    Let's say I have the following function: 

     

    function doSomething(a) {
        Log.Message('This is what I did: ' + a);
    }

    and I use that in several test cases in keyword tests.  Pretty much some basic stuff.  But let's say I want to add an optional parameter.  THIS does not work:

     

    function doSomething(a, b) {
        if (b === undefined) {
            Log.Message('This is what I did: ' + a);        
        }
        else {
             Log.Message('This is what I did: ' + a + ' and I also did this: ' + b);
        }
    }

    In strict JavaScript code only, this works fine and there are no conflicts about parameter count mismatch, etc.  JavaScript is good with this.  Keyword tests, however, will get an exception about a parameter count problem.

     

    I tried, initially, to do something like this:

     

    function doSomething(a, b = undefined) {
        if (b === undefined) {
            Log.Message('This is what I did: ' + a);        
        }
        else {
             Log.Message('This is what I did: ' + a + ' and I also did this: ' + b);
        }
    }

    And this generated the same problem.  What I found is that, if you want the "optional parameter" stuff to work, you need to actually set it to an actual value.  So, in my tests, I put in a ridiculous number 

     

    function doSomething(a, b = 999) {
        if ((b === undefined) || (b === 999)) {
            Log.Message('This is what I did: ' + a);        
        }
        else {
             Log.Message('This is what I did: ' + a + ' and I also did this: ' + b);
        }
    }

    and this worked.  The keyword tests no longer generated an exception and my code function worked as I wanted it to.

    So, generally speaking, this feature request is kind of a do-nothing if you do your coding in a particular way.  However, it would be nice to not have to do these code checks.  If I call a keyword test from another keyword test and there is a parameter count mismatch, as long as the keyword test has parameters marked as "optional", they are ignored in the parameter check.  So, it would be nice that, if a JavaScript function comes up with a parameter mismatch, to default the missing parameters as "undefined" so that my second example above would work.  This is how www.w3schools.com/js documents "default parameters" so it would be nice to keep a similar style.