简体   繁体   English

从现有的python包创建机器人框架库

[英]Creating a robot framework library from existing python package

My questions are: 我的问题是:

  • How can I use the ConnectHandler in robotframework? 如何在机器人框架中使用ConnectHandler?
  • What is a good workflow to solve the problem of creating robot libraries from existing python packages? 什么是解决从现有python包创建机器人库的问题的良好工作流程?

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.

相关问题 机器人框架:从python库导入变量 - Robot framework: Import Variables from python library 将现有的Webdriver对象传递给Robot Framework的自定义Python库 - Pass existing Webdriver object to custom Python library for Robot Framework 机器人框架中的Python库文件 - Python library files in robot framework Robot Framework:是否可以从Python库代码访问Robot的全局变量? - Robot Framework: access Robot's global variables from Python library code? Python 3.5.1无法下载Robot Framework库 - Python 3.5.1 Cannot download Robot Framework library 在Python库中注册Robot Framework侦听器 - Register Robot Framework listener within Python library 从 Robot Framework 调用 Python - Call Python from Robot Framework 创建 python package 后,无法从另一个目录访问一个目录中的关键字 in.robot 文件 - Not able to access the keywords in .robot file in one directory from other directory after creating python package 机器人框架-如何使用相同的名称功能,但来自我在“关键字”部分中创建的不同python库 - robot framework - how to use the same name functions but come from different python library I created in Keywords section 如何将图像嵌入到用 python 编写的自定义库中的机器人框架日志文件中? - how to embed a image into robot framework log file from Custom Library written in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM