从现有的 python 包创建机器人框架库 [英] Creating a robot framework library from existing python package

查看:60
本文介绍了从现有的 python 包创建机器人框架库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:

  • 如何在机器人框架中使用 ConnectHandler?
  • 有什么好的工作流程可以解决创建机器人的问题来自现有 python 包的库?

我希望在机器人框架中使用 netmiko 库.我使用 pip 将该模块导入我的 python env 并确认其可用通过使用机器人文件.

*** 设置 ***图书馆 netmiko

我现在想实例化一个ConnectHandler",我可以从文档中看到它需要一个字典

https://pynet.twb-tech.com/blog/automation/python 命令行中的 netmiko.html:

<预><代码>>>>从 netmiko 导入 ConnectHandler>>>cisco_881 = {... 'device_type': 'cisco_ios',... 'ip': '10.10.10.227',...'用户名':'pyclass',... '密码': '密码',... }

源代码在这里:https://github.com/ktbyers/netmiko

所以我编辑了机器人文件来创建一个包含 key:values 的字典,然后将其作为参数传递给 ConnectHandler.

*** 设置 ***图书馆 netmiko图书馆馆藏*** 测试用例 ***我的测试${device}= 创建字典 device_type cisco_ios... ip 10.10.10.227... 用户名 pyclass...密码密码日志字典 ${device}连接处理器 ${device}

结果是

<块引用>

================================================================================ Testnetmiko

============================================================================== 我的测试
|失败 |KeyError: u'device_type'

我在这里做错了什么?

解决方案

有什么好的工作流程可以解决创建机器人的问题来自现有 python 包的库?

从现有包创建库的最佳方法就是这样做:创建一个库.不要尝试在机器人测试用例中直接调用 ConnectHandler 方法,而是创建一个关键字.

例如,创建一个名为 netmikoKeywords.py 的文件,并将您的代码放在那里.例如,您可能有一个名为 Make Connection 的关键字,它可能如下所示:

# netmikoKeywords.py从 netmiko 导入 ConnectHandlerdef make_connection(类型,IP,用户名,密码):设备 = {'device_type':类型,'ip': ip,'用户名':用户名,'密码':密码,}连接 = ConnectHandler(设备)返回连接

如果您希望在关键字之间保持连接,您可能需要将连接设置为全局变量.或者,将您的库创建为类并使其成为实例变量.

你可以像这样在你的机器人文件中使用它:

*** 设置 ***|图书馆 |netmiko关键字*** 测试用例 ***|例子||${连接}= |建立联系||... |cisco_ios |10.10.10.227 |pyclass |密码

My questions are:

  • How can I use the ConnectHandler in robotframework?
  • What is a good workflow to solve the problem of creating robot libraries from existing python packages?

I wish to use netmiko library in robotframework. I imported the module into my python env using pip and confirmed its available by using a robot file.

*** Settings ***
Library    netmiko

I now wish to instantiate a "ConnectHandler", I can see from the documentation that it takes a dictionary

https://pynet.twb-tech.com/blog/automation/netmiko.html at the python commandline:

>>> 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

So I edited the robot file to create a dictionary containing key:values , and then passed that as an argument to 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

============================================================================== My Test
| FAIL | 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?

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.

For example, create a file called netmikoKeywords.py, and place your code there. For example, you might have a keyword called Make Connection that might look something like this:

# 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

这篇关于从现有的 python 包创建机器人框架库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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