我是机器人框架的新手。我试图从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
我收到以下错误:
测试库'sample'需要1到2个参数,得0。
请帮助。感谢。
答案 0 :(得分: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
答案 1 :(得分:0)
此答案将继续我对您earlier question的回答中提供的示例
使用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
答案 2 :(得分:0)
所以这就是我所做的。 :)
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