简体   繁体   English

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

[英]Implementing listener in Robot Framework - data.resource.variables aquired are empty

I am writing a listener for my test suite in Robot Framework to move my output files to directory with unique ID embedded into it based on the tested device ID.我正在为我在 Robot Framework 中的测试套件编写一个监听器,以根据测试的设备 ID 将我的输出文件移动到嵌入了唯一 ID 的目录中。 I was actually inspired by this answer and decided to try it myself:我实际上受到了这个答案的启发,并决定自己尝试一下:

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

So it seems, that variables passed in data.resource.variables to end_suite(...) method are... well, empty.因此,在data.resource.variables传递给end_suite(...)方法的变量似乎是......好吧,是空的。 I have only checked data.resource.keywords and it seems that there are few - so something is properly filled.我只检查了data.resource.keywords并且似乎很少 - 所以有些东西被正确填写。 Also, I have already checked if the variable I set is available suite-wise, and it was properly logged in different test case.此外,我已经检查了我设置的变量是否适用于套件,并且它已正确记录在不同的测试用例中。

Is there anything wrong with my listener code or maybe I should look for a problem in other part of my application?我的侦听器代码是否有问题,或者我应该在应用程序的其他部分查找问题?

FYI, this is a line that should set global variable and it works properly:仅供参考,这是一条应该设置全局变量的行,它可以正常工作:

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

You seem to be asking how to get the value of the variable ${uniqueBoardid} .您似乎在问如何获取变量${uniqueBoardid} You don't do that through the .resource attribute.你不是通过.resource属性来做到这一点的。 That attribute is useless in this context.该属性在这种情况下是无用的。 It refers to the *** Variables *** table of the suite.它指的是套件的*** Variables ***表。

You can get the value of a variable by running the get_variable_value method from the BuiltIn library.您可以通过运行内置库中的get_variable_value方法来获取变量的值。

For example:例如:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM