Reputation: 1
Im trying to get a keyword to run other keywords without having to call it so much.
keyword_rp {variable} keyword_2 ${arg1} ${arg2} ${arg3}
keyword_rp {variable} ${return_data}= keyword_3
keyword_rp {variable} keyword_4 {arg11}
etc.....
you can see how this clutters your robot tests instead I want another key word that you call that takes in the entire test as arguments. Then calls the keyword_rp via python for each line.
keyword_rp_once {variable}
... keyword_2 ${arg1} ${arg2} ${arg3}
... ${return_data}= keyword_3
... keyword_4 {arg11}
python would look like this
def keyword_rp_once(self, variable, *argv):
#parse arguments and use robot built in to run each keyword
Or maybe there is a better way to do this?
Upvotes: 0
Views: 2642
Reputation: 1
It is possible to do it by putting the keywords variables between quotes. For example:
*** Test Cases ***
Example
I execute "ls"
Sleep for "2" seconds
I print "ls" with "lah" with file "File"
*** Keywords ***
I execute "${cmd}"
log to console ${cmd}
I print "${cmd}" with "${opts}" with file "${input}"
LOG TO CONSOLE ${cmd}
Sleep for "1" seconds
LOG TO CONSOLE ${opts}
Sleep for "10" seconds
LOG TO CONSOLE ${input}
Sleep for "${input1}" seconds
sleep ${input1}
In your case it would be as follows:
keyword_rp "{variable}" keyword_2 "${arg1}" "${arg2}" "${arg3}"
You can check this page: https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#embedding-arguments-into-keyword-name
Upvotes: 0
Reputation: 386342
The problem is, robot doesn't see three lines, it sees one long list of arguments by the time it actually has to run the keyword. When keyword_rp_once
is called, what the keyword sees is exactly the same as if you had typed this:
keyword_rp_once {variable} keyword2 ${arg1} ${arg2} ${arg3} ${return_data}= keyword_3 keyword_4 {arg11}
In short, it's impossible for you to do what you want directly in a keyword because robot throws away critical information - the newlines -- so it's impossible for your custom keyword to know where one group of arguments ends and the second group begins.
Note that with the new parser in 3.2, line breaks are available internally, but you would have to do a lot of work to get the information out of the parser and into your keyword via a listener and/or pre-run modifier.
The much simpler choice is to do exactly what robot's own Run Keywords
does: it forces you to add a marker to tell it where one keyword ends and the next begins (specifically in that keyword, the special argument AND
). Then, your keyword can split the single long list of arguments into multiple lists of arguments by looking for that marker.
For example, I once did what you're trying to do by using :
to separate one set of arguments from another. It looked like this:
keyword_rp_once {variable}
... : keyword2 ${arg1} ${arg2} ${arg3}
... : ${return_data}= keyword_3
... : keyword_4 {arg11}
It's marginally easier to read, but at the expense of having to write a complicated keyword, and people who know robot but don't know much about your keyword will find the syntax confusing.
This solution will require a surprisingly large amount of work, especially since you have a variable assignment in the middle. Your keyword would have to have a bunch of code to iterate through the arguments, and then add special handling for any argument that looked like :
or matched a regular expression like ${.*?|=?
or something like that.
Upvotes: 1