在Robot框架中实现侦听器data.resource.variables获得的是空的

2024-10-16 22:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Robot框架中为我的测试套件编写一个侦听器,将我的输出文件移动到基于测试设备ID嵌入其中的唯一ID的目录中。实际上,我受到this answer的启发,决定亲自尝试:

class OutputFilesListener(object):
    ROBOT_LISTENER_API_VERSION = 3

    def __init__(self):
        self.output = ""
        self.log = ""
        self.report = ""
        self.unique_id = ""

    def end_suite(self, data, result):
        # I would like to get data.resource.variables to read global variable I set in one of the test cases based on some output
        print(data.resource.variables) # called properly, prints []
        print(data.resource.keywords) # called properly, prints [<robot.running.model.UserKeyword object at 0x7f88157e7978>, <robot.running.model.UserKeyword object at 0x7f88157d80f0>, <robot.running.model.UserKeyword object at 0x7f88157d8160>] - makes sense


    def output_file(self, path):
        self.output = path

    def log_file(self, path):
        self.log = path

    def report_file(self, path):
        self.report = path

    def close(self):
        print("{} {} {}".format(self.output, self.log, self.report))
        if self.log and self.report and self.output:
            print("All are there!") # This is fine - so in general values are properly passed

所以看起来,传入data.resource.variablesend_suite(...)方法的变量是。。。嗯,空的。我只检查了data.resource.keywords,似乎没有什么-所以有一些东西是正确填充的。另外,我已经检查了我设置的变量是否是可用的,并且它被正确地记录在不同的测试用例中。在

我的侦听器代码有什么问题吗?或者我应该在应用程序的其他部分查找问题?在

仅供参考,这是一行应该设置全局变量,并且它可以正常工作:

^{pr2}$

Tags: pathselfreportlogoutputdatamodelobject
1条回答
网友
1楼 · 发布于 2024-10-16 22:24:36

您似乎在问如何获得变量${uniqueBoardid}的值。不能通过.resource属性来实现。在这种情况下,这个属性是无用的。它引用该套件的*** Variables ***表。在

您可以通过运行内置库中的get_variable_value方法来获得变量的值。在

例如:

from robot.libraries.BuiltIn import BuiltIn
...
class OutputFilesListener(object):
    ROBOT_LISTENER_API_VERSION = 3
    ...
    def end_suite(self, data, result):
        uniqueBoardId = BuiltIn().get_variable_value('${uniqueBoardId}`)
        print("the board id is %s" % uniqueBoardId)

相关问题 更多 >