使用robotframework api从python执行robot关键字 [英] execute robot keyword from python using robotframework api

查看:40
本文介绍了使用robotframework api从python执行robot关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用机器人语言编写复杂的机器人关键字有时非常耗时,因为机器人语言不是真正的编程语言.我想用python编写我的关键字,并且只用robotframework语言公开简单的html表.问题是我们已经有很多用机器人语言编写的低级机器人关键字(在 .robot 和 .txt 文件中).是否可以使用 robotsframework python api 从 python 代码中执行这些关键字?

解决方案

是的,这是可能的.在您的 Python 代码中,您可以获得对 BuiltIn 库的引用,然后使用 Run Keyword 关键字来运行您想要的任何关键字.

例如,您可以编写一个 python 关键字,将另一个关键字作为参数并运行它.以下可能是您在 python 中的做法:

# MyLibrary.py从robot.libraries.BuiltIn 导入BuiltIndef call_keyword(keyword):返回 BuiltIn().run_keyword(keyword)

然后您可以告诉此关键字调用任何其他关键字.这是一个示例套件,其中有一个用机器人编写的关键字,然后让 python 代码执行它:

*** 设置 ***|图书馆 |我的图书馆.py*** 关键词 ***|示例关键字||日志 |你好,世界*** 测试用例 ***|调用一个调用robot 关键字的python 关键字的示例||呼叫关键字 |示例关键字

注意测试用例如何告诉 call_keyword 方法运行关键字 Example Keyword.当然,您不必必须传入关键字.关键点是获取对 BuiltIn 库的引用,然后您就可以调用该库中的任何方法.

这在机器人框架用户指南中记录在标题为使用机器人框架的内部模块.更具体地说,请参阅使用内置库部分.

请注意,文档指出,如果您的关键字调用了 run_keyword 方法,则您需要调用 register_run_keyword.我不会在这里复制文档.您可以通过查看 BuiltIn 模块本身来获取文档,或者在交互式 Python 会话中运行以下代码:

<预><代码>>>>导入robot.libraries.BuiltIn>>>帮助(robot.libraries.BuiltIn.register_run_keyword)

Writing complex robot keywords in robot language is sometimes very time consuming because robot language is not a real programming language. I would like to write my keywords in python and only expose simple html tables in robotframework language. The problem is that we already have a lot of low level robot keywords written in robot language (in .robot and .txt files). Is it possible to execute those keywords from the python code using the robotframework python api ?

解决方案

Yes, it is possible. In your python code you can get a reference to the BuiltIn library, and then use the Run Keyword keyword to run any keyword you want.

For example, you could write a python keyword that takes another keyword as an argument and runs it. The following might be how you do it in python:

# MyLibrary.py
from robot.libraries.BuiltIn import BuiltIn

def call_keyword(keyword):
    return BuiltIn().run_keyword(keyword)

You can then tell this keyword to call any other keyword. Here's an example suite that has a keyword written in robot,, and then has the python code execute it:

*** Settings ***
| Library | MyLibrary.py

*** Keywords ***
| Example keyword
| | log | hello, world

*** Test Cases ***
| Example of calling a python keyword that calls a robot keyword
| | Call keyword | Example keyword

Notice how the test case tells the call_keyword method to run the keyword Example Keyword. Of course, you don't have to pass in a keyword. The key point is to get a reference to the BuiltIn library, which then allows you to call any method in that library.

This is documented in the robot framework user guide, in the section titled Using Robot Framework's Internal Modules. More specifically, see the section Using BuiltIn Library.

Note that the documentation states you need to call register_run_keyword if your keyword calls the run_keyword method. I won't reproduce the documentation here. You can get the documentation by looking in the BuiltIn module itself, or run the following code in an interactive python session:

>>> import robot.libraries.BuiltIn
>>> help(robot.libraries.BuiltIn.register_run_keyword)

这篇关于使用robotframework api从python执行robot关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆