how could i add argument to robot lin command or how to create my own robot command

525 views
Skip to first unread message

hicham kharro

unread,
Dec 22, 2016, 2:09:45 PM12/22/16
to robotframework-users

Goodmornning ,

Well my problem is that, i’m looking for a way to add an author argument to robot line command and to model it to my project. An example of what I want to do :

Robot –u  unitary_test  --include tag1 tag2 tag3 –exclude tag4 tag7  --file_log=c:\file_log    / functionality  /subfonctionality

Bryan Oakley

unread,
Dec 22, 2016, 3:26:08 PM12/22/16
to robotframework-users
Your example is a bit unclear. What do you mean by an "author argument"? Have you considered simply setting variables or metadata from the command line with the --variable and --metadata arguments?

That being said, it's pretty easy to write your own custom test runner script that wraps robot/pybot. To avoid clashing with robot arguments, you might want to prefix all of your custom arguments with "+" or some other character. The argparse module makes this pretty easy.  Then, build up a new argument list based on what you parsed and didn't parse, and then pass it to robot.

Here's a really basic example that lets you specify "+f" or "++firefox" to run with firefox (or more accurately, it sets the ${BROWSER} variable to "firefox"):

import argparse
from robot import run_cli

parser = argparse.ArgumentParser(prefix_chars="+")
parser.add_argument("+f", "++firefox", action="store_true",
                    help="use firefox")

args, remaining_args = parser.parse_known_args()

# hard-code some arguments:
new_args = ["--outputdir", "./results"]

# translate custom arguments into something pybot understands
if args.firefox:
    new_args.extend(["--variable", "BROWSER:firefox"])
else:
    new_args.extend(]"--variable", "BROWSER:chrome"])

# run with our new arguments and all the remaining arguments
run_cli(new_args + remaining_args)



--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Pekka Klärck

unread,
Dec 22, 2016, 3:54:18 PM12/22/16
to Oakley, Bryan, robotframework-users
Hi,

Didn't know argparse supports parsing only some of the arguments. Very
nice, thanks for sharing Bryan!

One note to those trying Bryan's example: `run_cli` exits Python
interpreter with `sys.exit(rc)`, so you need to catch the resulting
`SystemExit` if you want to do any actions after test execution. The
general pattern is like this:

try:
run_cli(args)
except SystemExit as exit:
# cleanup code here
sys.exit(exit.code) # exit with robot's original return code


Cheers,
.peke
>> email to robotframework-u...@googlegroups.com.
>> To post to this group, send email to
>> robotframe...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/robotframework-users.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-u...@googlegroups.com.
> To post to this group, send email to robotframe...@googlegroups.com.
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org
Reply all
Reply to author
Forward
0 new messages