[英]Creating a robot framework library from existing python package
My questions are: 我的问题是:
I wish to use netmiko library in robotframework. 我希望在机器人框架中使用netmiko库。 I imported the module into my python env using pip and confirmed its available by using a robot file.
我使用pip将模块导入我的python env,并使用机器人文件确认其可用。
*** Settings ***
Library netmiko
I now wish to instantiate a "ConnectHandler", I can see from the documentation that it takes a dictionary 我现在希望实例化一个“ConnectHandler”,我可以从文档中看到它需要一本字典
https://pynet.twb-tech.com/blog/automation/netmiko.html at the python commandline: https://pynet.twb-tech.com/blog/automation/netmiko.html在python命令行:
>>> from netmiko import ConnectHandler
>>> cisco_881 = {
... 'device_type': 'cisco_ios',
... 'ip': '10.10.10.227',
... 'username': 'pyclass',
... 'password': 'password',
... }
Source code is here: https://github.com/ktbyers/netmiko 源代码在这里: https : //github.com/ktbyers/netmiko
So I edited the robot file to create a dictionary containing key:values , and then passed that as an argument to ConnectHandler. 因此,我编辑了机器人文件以创建包含键:值的字典,然后将其作为参数传递给ConnectHandler。
*** Settings ***
Library netmiko
Library Collections
*** Test Cases ***
My Test
${device}= Create Dictionary device_type cisco_ios
... ip 10.10.10.227
... username pyclass
... password password
Log Dictionary ${device}
ConnectHandler ${device}
The result was 结果是
============================================================================== Testnetmiko
================================================== ============================ Testnetmiko
============================================================================== My Test
================================================== ============================我的测试
|| FAIL |
失败| KeyError: u'device_type'
KeyError:u'device_type'
What am I doing wrong here? 我在这做错了什么?
What is a good workflow to solve the problem of creating robot libraries from existing python packages?
什么是解决从现有python包创建机器人库的问题的良好工作流程?
The best way to create a library from an existing package is to do exactly that: create a library. 从现有包创建库的最佳方法是执行以下操作:创建库。 Instead of trying to call the
ConnectHandler
method directly in your robot test case, create a keyword. 不要试图直接在机器人测试用例中调用
ConnectHandler
方法,而是创建一个关键字。
For example, create a file called netmikoKeywords.py, and place your code there. 例如,创建一个名为netmikoKeywords.py的文件,并将代码放在那里。 For example, you might have a keyword called
Make Connection
that might look something like this: 例如,您可能有一个名为
Make Connection
的关键字,可能如下所示:
# netmikoKeywords.py
from netmiko import ConnectHandler
def make_connection(type, ip, username, password):
device = {
'device_type': type,
'ip': ip,
'username': username,
'password': password,
}
connection = ConnectHandler(device)
return connection
If you want the connection to persist between keywords, you might want to set the connection as a global variable. 如果要在关键字之间保持连接,则可能需要将连接设置为全局变量。 Or, create your library as a class and make it an instance variable.
或者,将您的库创建为一个类,并使其成为一个实例变量。
You can this use this in your robot file like so: 您可以在机器人文件中使用它,如下所示:
*** Settings ***
| Library | netmikoKeywords
*** Test cases ***
| Example
| | ${connection}= | Make connection
| | ... | cisco_ios | 10.10.10.227 | pyclass | password
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.