简体   繁体   中英

Creating instances of python class from Robotframework

I am very new to robot framework. I am trying to create object of a python class from Robot framework. The following is the python code:

class sample():
  def __init__(self,dict1,connect=True):
     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,password:self.password}
     self.is_connect=False
     self.is_config_mode=False
     if connect:
        self.connects_to()
  def connect_to(self):
     print('stuff')

I need to make an object of the class sample in Robot file and use it to call other subsequent methods. What I did is:

*** Settings ***
Documentation     Testing connection
Library Collections
Library RequestsLibrary
Library      sample.sample  ${dict1}    WITH NAME    obj

Variables

*** Keywords ***
Test_Connection ${name}  
   [Documentation]    Testing connection
   ${a}=    obj.connect_ssh  

I am getting the following error:

Test Library 'sample' expected 1 to 2 arguments, got 0.

Kindly help. Thanks.

I am not sure what values are being passed to ${dict1} , i had some minor modification to your code and this is able to import library 'sample.py'

Both my .py and .robot file are in same folder

*** Settings ***
Documentation     Testing connection
Library    OperatingSystem
Library    String
Library    Collections
Library    RequestsLibrary
Library    DateTime
Library    sample.sample    ${dict1}    WITH NAME    obj
***Variables***
${dict1}    Test
*** Test Cases ***
Test_Connection_String
    Test_Connection    name

*** Keywords ***
Test_Connection
   [Arguments]    ${name}
   [Documentation]    Testing connection
   obj.connect_to

This answer continues with the example provided in my answer to your earlier question Loading the same library mutiple times is indeed possible using the Library xxx WITH NAME xxxyyy format.

Then you can access all of it's objects properties using the same syntax as the original. In the below example you'll find an example where the IP attribute holds a different value in each object:

*** Settings ***
Library  one
Library  one    WITH NAME    two
Library  Collections

*** Test Cases ***
Test Case
    ${dict1}=  Create Dictionary  device=auto1  ip=192.38.19.21  secret=${EMPTY}  uname=Adrija  password=Hello port=22
    ${dict2}=  Create Dictionary  device=auto2  ip=192.38.19.22  secret=${EMPTY}  uname=Adrija  password=Hello port=22

    one.connects to  ${dict1}  connect=${True}
    two.connects to  ${dict2}  connect=${True}

    ${one}    Get Library Instance    one
    ${two}    Get Library Instance    two

    Should Be Equal As Strings   ${one.ip}     192.38.19.21
    Should Be Equal As Strings   ${two.ip}     192.38.19.22

So this is what I did. :)

Library LibFiles/sample.py  WITH NAME    obj_1
Library LibFiles/sample.py  WITH NAME    obj_2

*** Keywords ***
Test_Connection ${Juniper_mse}  
  [Documentation]    Testing connection

  obj_1.connect_ssh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM