blob: 91a5f0c1ac8f760f67ba3a6e4f4dd602b8d7f18b [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
Michael Walshefb3d172018-03-29 16:29:12 -050076 if status != 'PASS':
Michael Walshbcc404d2017-04-12 15:54:58 -050077 # Output the error message to stderr.
78 BuiltIn().log_to_console(ret_values, stream='STDERR')
Michael Walshefb3d172018-03-29 16:29:12 -050079 if not ignore:
80 # Fail with the given error message.
81 BuiltIn().fail(ret_values)
Michael Walshbcc404d2017-04-12 15:54:58 -050082
83 return status, ret_values
84
Michael Walshbcc404d2017-04-12 15:54:58 -050085
Michael Walshbcc404d2017-04-12 15:54:58 -050086def run_key_u(keyword_buf,
87 quiet=None,
88 ignore=0):
Michael Walshbcc404d2017-04-12 15:54:58 -050089 r"""
90 Run keyword unconditionally (i.e. without regard to global test_mode
91 setting).
92
93 This function will simply call the run_key function passing on all of the
94 callers parameters except test_mode which will be hard-coded to 0. See
95 run_key (above) for details.
96
97 See the proglog of "run_key" function above for description of arguments.
98 """
99
100 return run_key(keyword_buf, test_mode=0, quiet=quiet, ignore=ignore)