How to use ReturnValueResolver method in Robotframework

Best Python code snippet using robotframework

assigner.py

Source:assigner.py Github

copy

Full Screen

...82 return HandlerExecutionFailed(ErrorDetails(exc_info))83 def assign(self, return_value):84 context = self._context85 context.trace(lambda: 'Return: %s' % prepr(return_value))86 resolver = ReturnValueResolver(self._assignment)87 for name, value in resolver.resolve(return_value):88 if not self._extended_assign(name, value, context.variables):89 value = self._normal_assign(name, value, context.variables)90 context.info(format_assign_message(name, value))91 def _extended_assign(self, name, value, variables):92 if name[0] != '$' or '.' not in name or name in variables:93 return False94 base, attr = self._split_extended_assign(name)95 try:96 var = variables[base]97 except DataError:98 return False99 if not (self._variable_supports_extended_assign(var) and100 self._is_valid_extended_attribute(attr)):101 return False102 try:103 setattr(var, attr, value)104 except:105 raise VariableError("Setting attribute '%s' to variable '%s' "106 "failed: %s" % (attr, base, get_error_message()))107 return True108 def _split_extended_assign(self, name):109 base, attr = name.rsplit('.', 1)110 return base.strip() + '}', attr[:-1].strip()111 def _variable_supports_extended_assign(self, var):112 return not (is_string(var) or is_number(var))113 def _is_valid_extended_attribute(self, attr):114 return self._valid_extended_attr.match(attr) is not None115 def _normal_assign(self, name, value, variables):116 variables[name] = value117 # Always return the actually assigned value.118 return value if name[0] == '$' else variables[name]119def ReturnValueResolver(assignment):120 if not assignment:121 return NoReturnValueResolver()122 if len(assignment) == 1:123 return OneReturnValueResolver(assignment[0])124 if any(a[0] == '@' for a in assignment):125 return ScalarsAndListReturnValueResolver(assignment)126 return ScalarsOnlyReturnValueResolver(assignment)127class NoReturnValueResolver(object):128 def resolve(self, return_value):129 return []130class OneReturnValueResolver(object):131 def __init__(self, variable):132 self._variable = variable133 def resolve(self, return_value):134 if return_value is None:135 identifier = self._variable[0]136 return_value = {'$': None, '@': [], '&': {}}[identifier]137 return [(self._variable, return_value)]138class _MultiReturnValueResolver(object):139 def __init__(self, variables):140 self._variables = variables141 self._min_count = len(variables)142 def resolve(self, return_value):143 return_value = self._convert_to_list(return_value)144 self._validate(len(return_value))145 return self._resolve(return_value)146 def _convert_to_list(self, return_value):147 if return_value is None:148 return [None] * self._min_count149 if is_string(return_value):150 self._raise_expected_list(return_value)151 try:152 return list(return_value)153 except TypeError:154 self._raise_expected_list(return_value)155 def _raise_expected_list(self, ret):156 self._raise('Expected list-like value, got %s.' % type_name(ret))157 def _raise(self, error):158 raise VariableError('Cannot set variables: %s' % error)159 def _validate(self, return_count):160 raise NotImplementedError161 def _resolve(self, return_value):162 raise NotImplementedError163class ScalarsOnlyReturnValueResolver(_MultiReturnValueResolver):164 def _validate(self, return_count):165 if return_count != self._min_count:166 self._raise('Expected %d return values, got %d.'167 % (self._min_count, return_count))168 def _resolve(self, return_value):169 return list(zip(self._variables, return_value))170class ScalarsAndListReturnValueResolver(_MultiReturnValueResolver):171 def __init__(self, variables):172 _MultiReturnValueResolver.__init__(self, variables)173 self._min_count -= 1174 def _validate(self, return_count):175 if return_count < self._min_count:176 self._raise('Expected %d or more return values, got %d.'177 % (self._min_count, return_count))178 def _resolve(self, return_value):179 before_vars, list_var, after_vars \180 = self._split_variables(self._variables)181 before_items, list_items, after_items \182 = self._split_return(return_value, before_vars, after_vars)183 before = list(zip(before_vars, before_items))184 after = list(zip(after_vars, after_items))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Robotframework automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful