簡體   English   中英

從Robotframework創建python類的實例

[英]Creating instances of python class from Robotframework

我是機器人框架的新手。 我正在嘗試從Robot框架創建python類的對象。 以下是python代碼:

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

我需要在Robot文件中創建類樣本的對象,並使用它來調用其他后續方法。 我所做的是:

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

變數

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

我收到以下錯誤:

測試庫“樣本”的預期參數為1至2,為0。

請幫助。 謝謝。

我不確定將什么值傳遞給$ {dict1},我對您的代碼進行了一些小的修改,因此可以導入庫“ sample.py”

我的.py和.robot文件都在同一文件夾中

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

該答案以我對您先前的問題的答案中提供的示例繼續進行。使用Library xxx WITH NAME xxxyyy格式確實可以多次加載相同的庫。

然后,您可以使用與原始語法相同的語法來訪問其所有對象屬性。 在下面的示例中,您將找到一個示例,其中IP屬性在每個對象中持有不同的值:

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

這就是我所做的。 :)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM