1
votes

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?

1
There are certainly ways to achieve this, but before that, what is your purpose? You want a custom return value for the test execution? - Bence Kaulics
Yes, that is what I want to achieve. - KonradDos

1 Answers

2
votes

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