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.
Related Content
- 4 years ago
- 11 years ago