Skip to main content
 首页 » 编程设计

python之在 Robot Framework 中实现监听器之获取的 data.resource.variables 为空

2024年10月01日8myhome

我正在 Robot Framework 中为我的测试套件编写一个监听器,以根据测试的设备 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.variables 中传递给 end_suite(...) 方法的变量是...好吧,是空的。我只检查了 data.resource.keywords 似乎很少 - 所以有些东西被正确填充了。此外,我已经检查了我设置的变量是否可用套件,并且它已正确记录在不同的测试用例中。

我的监听器代码是否有任何问题,或者我是否应该在我的应用程序的其他部分寻找问题?

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

Run Keyword If      ${validationResult} == True   Set Global Variable     ${uniqueBoardId}     ${deviceId[1]} 

请您参考如下方法:

您似乎在问如何获取变量${uniqueBoardid} 的值。您不会通过 .resource 属性来做到这一点。该属性在这种情况下是无用的。它指的是套件的 *** Variables *** 表。

您可以通过运行 BuiltIn 库中的 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)