从机器人文件调用python类中的方法 [英] Calling a method which is inside a class in python from a robot file
问题描述
我有一个python类,文件名是one.py
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']
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
if connect:
self.connects_to()
def connects_to(self):
netconn=ConnectionHandler(self.dict1)
print "stuff"
我需要从机器人文件中调用函数connects_to.
I need to call the function connects_to from the robot file.
*** Settings ***
Library LibFiles/one.py
Library OperatingSystem
Library String
Library Collections
*** Keywords ***
Test_1 ${equip1}
${dict1}= Create Drictionary device=auto1 ip:192.38.19.20 secret=${EMPTY} uname=Adrija password=Hello port=22
${a}= connects_to ${dict1} connect=${True}
但我得到的错误是方法connects_to() 不存在.请帮忙.
But the error that I am getting is the method connects_to() doesnt exists. Please help.
谢谢.
推荐答案
上述机器人脚本缺少用于分隔参数的双空格.我假设这是一个格式问题,而不是整个问题的一部分.我还将 ip:192..
更改为 ip=192..
并将关键字调用 connects_to
更改为 Connects To代码>,
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
,
在 Python 库中定义了两种方法:init
和 connect_to
.在 Robot Framework 中,这两个方法映射到以下事件:库 Library/LibFiles/one.py
的加载和关键字 connect 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
.
主要问题是,当您加载库时,您没有指定所需的变量.在下面的机器人示例中,我在 variables
部分指定变量,然后在 settings
部分使用这些变量.
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
如果您不想在加载时建立该连接,而是在连接时建立该连接,则需要在 Connects To
关键字中指定代码和变量.下面是修改后的代码.
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
class one(object):
ROBOT_LIBRARY_VERSION = 1.0
def __init__(self):
pass
def connects_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"
机器人脚本
*** 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}
需要注意的是,一些编辑器在为关键字发现预加载库时可能会出现问题.它们通常加载库而不将任何变量传递给 init
方法,从而导致错误.只需允许默认值并检查这些值就可以解决该问题.
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.
添加了第二个示例,用于将值直接关联到 Python 对象变量.
Added a second example for directly associating values to Python Object variables.
这篇关于从机器人文件调用python类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!