WoodChopper
WoodChopper

Reputation: 4375

Robot SSHLibrary pass ssh connection to my library class

Using SSHLibrary currently I execute ssh commands in test suite file or keyword file. Is it possible to do that in my current library file? Meaning my library function just forms the string now. I wanted to executed that in ssh connection in library file.

test/testsuit.robot

*** Settings ***
Library         SSHLibrary
Library         ${EXEC_DIR}/lib/mylib.py  WITH NAME  llib
Suite Setup     open_connection_and_login
Suite Teardown  Close All Connections

*** Variables ***
${HOST}      10.x.x.x.x
${USERNAME}  myuser
${PASSWORD}  mypassword

*** Test Cases ***
example test
    ${sshstring}= llib.form_the_sshstring   ls
    ${output}=  Execute Command     {sshstring}

*** Keywords ***
open_connection_and_login
    Open Connection  ${HOST}
    Login  ${USERNAME}  ${PASSWORD}

lib/mylib.py

def form_the_sshstring(input):
    sshstring = "{}".format(ls)
    return sshstring

Expecting something like..

*** Test Cases ***
example test
    ${output}= llib.run_the_sshstring   ls

/lib/mylib.py

import SSHLibrary
    def run_the_sshstring(input):
        sshstring = "{}".format(ls)
        out = SSHLibrary.SSHCleint(sshstring)
        return out

Upvotes: 1

Views: 1730

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

From within your library file you can get a reference to the imported SSHLibrary -- and thus, it's keywords -- with the built-in keyword Get Library Instance. With that, you can call the SSHLibrary keyword Execute Command

Example:

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

def run_the_sshstring(input):
    sshlib = BuiltIn().get_library_instance("SSHLibrary")
    result = sshlib.execute_command(input)
    return result

Upvotes: 2

Related Questions