Skip to content Skip to sidebar Skip to footer

Calling A Method Which Is Inside A Class In Python From A Robot File

I have a python class and the name of the file is one.py class one: def __init__(self,dict1,connect=False): self.device=dict1['device'] self.ip=dict1['ip']

Solution 1:

The above Robot Script lacks the double spaces to divide the arguments. I'm assuming that this is a formatting issue, and not part of the overall problem. I've also changed ip:192.. to ip=192.. and changed keyword call connects_to to Connects To,

In the Python Library there are two methods defined: init and connect_to. In Robot Framework these two methods map to the following events: loading of the library Library /LibFiles/one.py and calling of the keyword connect to.

The main problem is that when you load the library you're not specifing the required variables. In the below Robot Example I'm specifying the variables in the variables section and then using those in the settings section.

*** Settings ***
Library  one    ${dict1}    connect=${True}

*** Variables ***
&{dict1}    device=auto1  ip=192.38.19.20  secret=${EMPTY}uname=Adrija  password=Hello port=22

If you do not want to make that connection at load time, but when connecting, then the code and variables need to be specified in the Connects To keyword. Below is the modified code.

one.py

classone(object):

    ROBOT_LIBRARY_VERSION = 1.0def__init__(self):
       passdefconnects_to(self, dict1=False, connect=False):
       self.device=dict1['device']
       self.ip=dict1['ip']
       self.uname=dict1['uname']
       self.password=dict1['password']
       self.dict1={'device':self.device, 'ip':self.ip,'uname':self.uname,self.password:self.password}
       self.is_connect=False
       self.is_config_mode=False# netconn=ConnectionHandler(self.dict1)print"stuff"

Robot Script

*** Settings ***
Library  one
Library  Collections

*** Test Cases ***
Test_1
    ${dict1}=  Create Dictionary  device=auto1  ip=192.38.19.20  secret=${EMPTY}uname=Adrija  password=Hello port=22
    ${a}=  Connects To  ${dict1}  connect=${True}

Test_2
    ${one}    Get Library Instance    one
    ${one.ip}    Set Variable    123.123.123.123
    ${test}    Set Variable    ${one.ip}
    Log To Console    ${test}

It should be noted that some editors may have issues when pre loading libraries for keyword discovery. They typically load the library without passing any variables to the init method and thus cause an error. Simply allowing for default values and checking for those would solve that issue.

Edit: Added a second example for directly associating values to Python Object variables.

Post a Comment for "Calling A Method Which Is Inside A Class In Python From A Robot File"