1

I am trying to get variable value from report.xml file using publicAPI. This is how report looks like:

report.xml

It has a variable Var to which a Test value is assigned.

I have created simple REST API to run tests:

from flask import Flask
from flask import request

from robot import run
from robot import run_cli

app = Flask(__name__)

@app.route('/')
def runRobot():
 robotName = request.args.get('robot')
 robotName = robotName + '.robot'
 rc = run(robotName)
 return str(rc)

I would like to return value of Var variable instead of execution code. I can't find a matching function in documentation that meets my requirements

Is it even possible to do?

2
  • There are certainly ways to achieve this, but before that, what is your purpose? You want a custom return value for the test execution? Commented Aug 24, 2020 at 7:16
  • Yes, that is what I want to achieve. Commented Aug 29, 2020 at 7:30

1 Answer 1

2

What I could imagine for this situation is a custom metadata that you could set to whatever value you want using the Set Suite Metadata keyword.

Then you can create a small ResultVisitor to retrieve the needed metadata. I am using Robot Framework 3.1.2 for the example below.

Example (SO.robot):

*** Test Cases ***
Test
    Set Suite Metadata    My Return Value    My VALUE    append=True    top=True

The Python file calling Robot CLI and the ResultVisitor:

from robot import run_cli
from robot.api import ExecutionResult, ResultVisitor

class ReturnValueFetcher(ResultVisitor):

    def __init__(self):
        self.custom_rc = None

    def visit_suite(self, suite):
        try:
            self.custom_rc = suite.metadata["My Return Value"]
        except KeyError:
            self.custom_rc = 'Undefined' # error, False, exception, log error, etc.
        
        # Only visit the top suite
        return False

# Run robot, exit=False is needed so the script won't be terminated here
rc = run_cli(['SO.robot'], exit=False)

# Instantiate result visitor
retval = ReturnValueFetcher()

# Parse execution result using robot API
# assuming the output.xml is in the same folder as the Python script
result = ExecutionResult("./output.xml")

# Visit the top level suite to retrive needed metadata
result.visit(retval)

# Print return values
print(f"normal rc:{rc} - custom_rc:{retval.custom_rc}")

Your custom return value will be clearly visible in your log.html and report.html files as well.

enter image description here

This is the console output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.