我一直在使用 Robotframework 和 Python 编写代码: 我使用 get_model() 从 .robot 文件中获取模型。然后使用基于 AST(抽象语法树)基本思想的 ModelTransformer() 修改模型。
但是在完成修改后,当我尝试使用 .save() 函数将修改后的模型保存到新的 .robot 文件中时,这完全改变了新机器人文件的格式。
# Code to save new robot file
model.save("New.robot")
谁能告诉我,如何解决这个问题?
编辑:
from robot.api import TestData
from robot.api.parsing import ModelTransformer
# read the original model from the .robot file
test_data = TestData(source='original.robot')
# apply your modifications using the ModelTransformer
transformer = ModelTransformer()
transformed_data = transformer.visit(test_data)
# write the modified model to a new .robot file
transformed_data.save(filename='new.robot')
# write the modified model to a new .robot file
with open('new.robot', 'w') as file:
transformed_data.save(file)