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