3

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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, good answer, the only other thing I should mention is that I was not passing input as a dictionary , so the error | FAIL | KeyError: u'device_type' was due to using connection = ConnectHandler(device) rather than connection = ConnectHandler(**device). Its explained here stackoverflow.com/questions/36901/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.