Python 机器人框架-获取变量

Python 机器人框架-获取变量,python,robotframework,Python,Robotframework,我正在尝试使用publicAPI从report.xml文件中获取变量值。以下是报告的外观: 它有一个变量Var,该变量分配了一个Test值 我创建了简单的REST API来运行测试: from flask import Flask from flask import request from robot import run from robot import run_cli app = Flask(__name__) @app.route('/') def runRobot(): r

我正在尝试使用publicAPI从report.xml文件中获取变量值。以下是报告的外观:

它有一个变量Var,该变量分配了一个Test

我创建了简单的REST API来运行测试:

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)
我想返回Var变量的值,而不是执行代码。我在中找不到符合我的要求的匹配函数


甚至可以这样做吗?

在这种情况下,我可以想象的是一个自定义元数据,您可以使用关键字将其设置为您想要的任何值

然后,您可以创建一个小型数据库来检索所需的元数据。我在下面的示例中使用Robot Framework 3.1.2

示例(SO.robot):

调用Robot CLI和ResultVisitor的Python文件:

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}")
自定义返回值也将在log.html和report.html文件中清晰可见

这是控制台输出:


实现这一点肯定有办法,但在此之前,你的目的是什么?您想要为测试执行定制返回值吗?是的,这就是我想要实现的。
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}")