Ejemplo n.º 1
0
    def report(self) -> Reporter:
        """Enables access to the TestProject reporting actions from the driver object

        Returns:
            Reporter: object giving access to reporting methods
        """
        return Reporter(self.command_executor)
Ejemplo n.º 2
0
    def execute(self,
                action: ActionProxy,
                by: By = None,
                by_value: str = None) -> ActionProxy:
        # Set the locator properties
        action.proxydescriptor.by = by
        action.proxydescriptor.by_value = by_value

        # Set the list of parameters for the action
        for param in action.__dict__:
            # Skip the _proxydescriptor attribute itself
            if param not in ["_proxydescriptor"]:
                action.proxydescriptor.parameters[param] = action.__dict__[
                    param]

        # Objects for handling any StepSettings
        settings = self._command_executor.settings
        step_helper = self._command_executor.step_helper

        # Handling driver timeout
        step_helper.handle_timeout(settings.timeout)
        # Handling sleep before execution
        step_helper.handle_sleep(sleep_timing_type=settings.sleep_timing_type,
                                 sleep_time=settings.sleep_time)

        # Execute the action
        response: AddonExecutionResponse = self._agent_client.execute_proxy(
            action)

        # Handling sleep after execution
        step_helper.handle_sleep(sleep_timing_type=settings.sleep_timing_type,
                                 sleep_time=settings.sleep_time,
                                 step_executed=True)

        if response.execution_result_type is not ExecutionResultType.Passed and not settings.invert_result:
            raise SdkException(
                f"Error occurred during addon action execution: {response.message}"
            )

        # Update attributes value from response
        for field in response.fields:

            # skip non-output fields
            if not field.is_output:
                continue

            # check if action has an attribute with the name of the field
            if not hasattr(action, field.name):
                logging.warning(
                    f"Action '{action.proxydescriptor.guid}' does not have a field named '{field.name}'"
                )
                continue

            # update the attribute value with the value from the response
            setattr(action, field.name, field.value)

        # Extract result from response result.
        result = True if response.execution_result_type is ExecutionResultType.Passed else False
        result, step_message = step_helper.handle_step_result(
            step_result=result,
            base_msg=response.message,
            invert_result=settings.invert_result,
            always_pass=settings.always_pass)

        # Handle screenshot condition
        screenshot = step_helper.take_screenshot(settings.screenshot_condition,
                                                 result)

        # Getting the addon name from its proxy descriptor class name.
        # For example:
        #   action.proxydescriptor.classname = io.testproject.something.i.dont.care.TypeRandomPhoneNumber
        #   description is 'Execute TypeRandomPhoneNumber'.
        description = f'Execute \'{action.proxydescriptor.classname.split(".")[-1]}\''

        element = None
        # If proxy descriptor has the by property and the by property is implemented by TestProject's FindByType...
        if action.proxydescriptor.by and FindByType.has_value(
                action.proxydescriptor.by):
            element = ElementSearchCriteria(
                find_by_type=FindByType(action.proxydescriptor.by),
                by_value=action.proxydescriptor.by_value,
                index=-1)
        # Creating input/output fields
        input_fields = {
            f.name: f.value
            for f in response.fields if not f.is_output
        }
        output_fields = {
            f.name: f.value
            for f in response.fields if f.is_output
        }
        # Manually reporting the addon step with all the information.
        Reporter(command_executor=self._command_executor).step(
            description=description,
            message=f'{step_message}{os.linesep}',
            element=element,
            inputs=input_fields,
            outputs=output_fields,
            passed=result,
            screenshot=screenshot)
        return action