Forum Discussion
Hi,
I know we have option to create Keyword test variables at runtime. refer here
But I'm not sure how we can use keyword test name in dynamic way.
When we have more control over the Keyword Test variables we may reduce the usage of variables prior to running the test.
- shankar_r7 years agoCommunity Hero
As a workaround you can do like below,
Keywordtests.<generic_keyword_test_name>.variables.<variable_name> = "local_variable_value" To handle this, var str_keywordtestname = "KeywordTest1"; var objKeyword = eval("Keywordtests." + str_keywordtestname); Now you will have you keyword test object then you use this object to create dynamic variable. objKeyword.variable.VariableByName("MsgVar");
I haven't worked in Keyword test but I'm guess this will have some help for you to handle dynamic
- Marsha_R7 years agoChampion Level 3
Can I ask why you don't want to just define the local variables in the keyword test itself?
- tristaanogre7 years agoEsteemed Contributor
I've considered using a Code Snippet to add a variable dynamically within a keyword test. Like Project variables and ProjectSuite variables, using code, you can add and/or delete variables on the fly (KeywordTests.MyKWT.Variables.AddVariable is a viable function). However, building a keyword test around it will be difficult because, until the variable is added, you can't utilize it in any keyword test operations. You would have to resort to Code Expressions anytime you want to use the dynamically declared variable using KeywordTests.MyKWT.Variables.VariableByName(). Also, note that the variable would persist at the end of the keyword test meaning you would have to make sure you dispose of it after you're finished with it or add logic to detect whether or not it exists before you add it. It just seems that this over-complicates things that can be achieved simply by declaring the variables at design time of the KWT.
The way I think of Keyword tests is that each one is a function declaration, much like declaring a function in JavaScript:
function Test() { var myVar; myVar = 'My Test'; Log.Message(myVar); }
Variables within functions are declared when the function is declared. Even in JavaScript, while a variable declaration can happen any time within the code, they are "hoisted" to the top and effectively declared up front (unless you're running in strict mode but I digress). Anything added dynamically are properties added to objects, elements added to arrays, rows added to tables, etc.
As Marsha_R mentioned, it would be interesting to know why you want to do things this way. What problem are you attempting to solve or what effect are you attempting to achieve? We might be able to find an alternative. For that matter, if it's a matter of having common global variables to be used across all your keyword tests, it might just be best to declare them as Project or ProjectSuite variables up front or build them into read/write properties in script extension runtime objects.
Related Content
Recent Discussions
- 7 hours agoSvetaG