我正在 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
属性来执行此操作。该属性在这种情况下毫无用处。它引用套件的***变量***
表。
您可以通过运行内置库中的 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)
关于python - 在机器人框架中实现监听器 - 获取的 data.resource.variables 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46912690/