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