blob: 44aa1d16fa39e7b1b5914a92d296b0b158555bf6 [file] [log] [blame]
Michael Walshbcc404d2017-04-12 15:54:58 -05001#!/usr/bin/env python
2
3r"""
4This module provides robot keyword execution functions such as run_key..
5"""
6
7import gen_print as gp
8from robot.libraries.BuiltIn import BuiltIn
9
10
Michael Walshbcc404d2017-04-12 15:54:58 -050011def run_key(keyword_buf,
12 quiet=None,
13 test_mode=None,
14 ignore=0):
Michael Walshbcc404d2017-04-12 15:54:58 -050015 r"""
16 Run the given keyword, return the status and the keyword return values.
17
18 The advantage of using this function verses having robot simply run your
19 keyword is the handling of parameters like quiet, test_mode and ignore.
20
21 Description of arguments:
22 keyword_buf The keyword string to be run.
23 quiet Indicates whether this function should run
24 the pissuing function to print 'Issuing:
25 <keyword string>' to stdout.
26 test_mode If test_mode is set, this function will
27 not actually run the command. If quiet is
28 0, it will print a message indicating what
29 it would have run (e.g. "Issuing:
30 (test_mode) your command").
31 ignore Ignore errors from running keyword. If
32 this is 0, this function will fail with
33 whatever error occurred when running the
34 keyword.
35
36 Example usage from a robot script:
37
38 ${status} ${ret_values}= Run Key My Keyword \ Arg1 \ Arg2
39
40 Note that to get robot to pass your command + args as a single string to
41 this function, you must escape extra spaces with a backslash.
42
43 Also note that ret_values is a python list:
44 ret_values:
45 ret_values[0]: value1
46 ret_values[1]: value2
47 """
48
49 # Set these vars to default values if they are None.
50 quiet = int(gp.get_var_value(quiet, 0))
51 test_mode = int(gp.get_var_value(test_mode, 0))
52 ignore = int(ignore)
53
54 # Convert the keyword_buf into a list split wherever 2 or more spaces are
55 # found.
56 keyword_list = keyword_buf.split(' ')
57 # Strip spaces from each argument to make the output look clean and
58 # uniform.
59 keyword_list = [item.strip(' ') for item in keyword_list]
60
61 if not quiet:
62 # Join the list back into keyword_buf for the sake of output.
63 keyword_buf = ' '.join(keyword_list)
64 gp.pissuing(keyword_buf, test_mode)
65
66 if test_mode:
67 return 'PASS', ""
68
Michael Walshdfcbaa82017-04-20 16:48:03 -050069 try:
70 status, ret_values = \
71 BuiltIn().run_keyword_and_ignore_error(*keyword_list)
72 except Exception as my_assertion_error:
73 status = "FAIL"
74 ret_values = my_assertion_error.args[0]
Michael Walshbcc404d2017-04-12 15:54:58 -050075
76 if not (status == 'PASS' or ignore):
77 # Output the error message to stderr.
78 BuiltIn().log_to_console(ret_values, stream='STDERR')
79 # Fail with the given error message.
80 BuiltIn().fail(ret_values)
81
82 return status, ret_values
83
Michael Walshbcc404d2017-04-12 15:54:58 -050084
Michael Walshbcc404d2017-04-12 15:54:58 -050085def run_key_u(keyword_buf,
86 quiet=None,
87 ignore=0):
Michael Walshbcc404d2017-04-12 15:54:58 -050088 r"""
89 Run keyword unconditionally (i.e. without regard to global test_mode
90 setting).
91
92 This function will simply call the run_key function passing on all of the
93 callers parameters except test_mode which will be hard-coded to 0. See
94 run_key (above) for details.
95
96 See the proglog of "run_key" function above for description of arguments.
97 """
98
99 return run_key(keyword_buf, test_mode=0, quiet=quiet, ignore=ignore)