简体   繁体   中英

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. 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. I have only checked data.resource.keywords and it seems that there are few - so something is properly filled. 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} . You don't do that through the .resource attribute. That attribute is useless in this context. It refers to the *** Variables *** table of the suite.

You can get the value of a variable by running the get_variable_value method from the BuiltIn library.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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