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

Add KeywordTest Parameter Count and dynamic execution via script

If you're running a keyword test via script, especially in a data driven environment where the data dictates which test to execute, it would be useful to be able to get a count of the parameters on a keyword test.  Currently, for any keyword test, you have access to the Parameters object for the test and then just each parameter individually.  There is not a way to iterate through those parameters and assign values prior to execution nor is there a way to simply get a count of the parameters so you can build your parameter list to pass to the Run method.

 

It would be nice if, on a per test basis, you have access to something like KeywordTests.MyTest.Parameters.Count and even some way of iterating through the collection to set the values prior to test execution... something like going through all the parameters, setting the values, and then just executing "Run"... 

 

The idea behind this, again, is to make implementing keyword tests in a data driven framework easier. With the above changes, you can add parameter values to a keyword test in a more generic way.  A framework developer could then (if the KeywordTest by name feature gets implemented), find a keyword test simply by name, assign parameter values to the keyword test straight from data without foreknowledge of the test ahead of time, and execute the keyword test.  A much more generic and dynamic automation framework could be created then to integrate keyword tests more fully into a data driven structure.

4 Comments

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    If I'm understanding your use case correctly, what you need is a way to pass arguments to an arbitrary keyword test dynamically during runtime. Something like:

    args = ["param1", 15, true]; // params are read from a file
    RunKeywordTest("TestName", args); 

    This can be done in JavaScript/ECMAScript 6 and in Python.

     

    JavaScript example:

    let testName = "MyTest1";
    let args = ["param1", 15, true]; // params are read from a file
    
    KeywordTests[testName].Run(...args);
    // This is the same as
    // KeywordTests.MyTest1.Run("param1", 15, true);

    The three dots operator "..." (aka spread operator) expands the "args" array into a regular list of arguments that are then passed to the .Run() method.

     

    This way you can have a generic function to run a keyword test by name:

    RunKeywordTest("TestWithNoParams");
    RunKeywordTest("TestWithParams", ["param1", 15, true]);
    RunKeywordTest("TestWithMoreParams", ["foo", "bar", "baz", "qux", "quux"]); function RunKeywordTest(TestName, Args = []) { KeywordTests[TestName].Run(...Args); }

     

    Python has a similar feature, array unpacking:

    testName = "MyTest1"
    args = ["param1", 15, True]
    
    KeywordTests.__getprop__(testName).Run(*args)
    # Same as:
    # KeywordTests.TestName.Run("param1", 15, true)

     

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    And that just shows that, in some ways, I'm still a n00b with JavaScript.

    That is EXACTLY what I need... now... if i can only do that in a Script Extension
    ;)
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    OK... that JScript vs. JavaScript thing is really starting to bug me...

    Your code works fine so long as the array AND the RunKeywordTests method are both JavaScript...

    ...but my Array is coming from a Script Extension which is a JScript array... which doesn't have all that fancy stuff available to me... and so, it doesn't work.  So, I have to do some conversion work to turn my parameter list into an array that works with JavaScript.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    OK... what I ended up doing is that, in the JScript extension, I constructed my objects as normal and then called JSON.stringify to convert the complex objects to JSON notation. That's the value then that I pass on to my JavaScript code where, the first thing it does is convert the JSON to JavaScript objects using JSON.parse.  Now my arrays are working correctly and can do that "magic" explode thingie...