如何在机器人框架中将对象传递给关键字? [英] How to pass objects to keywords in robot framework?
问题描述
我在文件MyClass.py
中编写了一个python类MyClass
:
I have a python class MyClass
written in file MyClass.py
:
class MyClass(object):
def __init__(self):
self.myvar = list()
def setvar(self, val):
self.myvar = val
def mymethod(self):
return self.myvar
我已按如下所示在Robot Framework中导入
I have imported in Robot Framework as below:
Library MyClass WITH NAME my_component
我还有一个关键字,它调用传递给它的对象的方法:
I also have a keyword which call a method of objects which are passed to it:
testing component
[Arguments] ${cmp}
log to console ************* ${cmp}
${result} = ${cmp}.mymethod
我有多个从类MyClass
实例化的对象,每个对象都有不同的属性.我想使用testing component
关键字获取它们的属性,而不管对象本身是什么.
I have multiple objects instantiated from class MyClass
and every object has different properties. I want to get their attributes using testing component
keyword regardless of the object itself.
当我通过my_component
呼叫testing component
时:
Test Method Call
testing component my_component
我得到:
No keyword with name '${cmp}.mymethod' found.
如果我用关键字testing component
这样称呼它:
If I call it this way in keyword testing component
:
${result} = call method ${cmp} mymethod
我得到:
Object 'my_component' does not have method 'mymethod'.
我也尝试了call method my_component mymethod
进行测试,然后又得到了Object 'my_component' does not have method 'mymethod'.
.
I also tried call method my_component mymethod
for test and I again got Object 'my_component' does not have method 'mymethod'.
.
但是当我使用${result} = my_component.mymethod
时,一切正常.
But when I use ${result} = my_component.mymethod
, everything works fine.
推荐答案
关于如何调用python对象方法的问题的字面上答案是,您可以使用扩展变量语法(例如:${cmp.mymethod()}
,也可以使用call method
关键字(例如call method ${cmp} mymethod
).
The literal answer to your question of how to call a method of a python object is that you can use extended variable syntax (eg: ${cmp.mymethod()}
or you can use the call method
keyword (eg: call method ${cmp} mymethod
).
例如,robot中的普通标量变量是python字符串对象.因此,我们可以在它们上调用诸如lower()
和upper()
之类的方法.以下测试案例展示了如何使用我前面提到的两种机制在字符串上调用这些方法:
For example, normal scalar variables in robot are python string objects. Thus, we can call methods on them such as lower()
and upper()
. The following test case shows how you can call these methods on a string, using the two mechanisms I mentioned earlier:
*** Variables ***
${message} Hello, world # a python string object
*** Test cases ***
Example
# Example using "Call method" keyword
${lower}= call method ${message} lower
should be equal ${lower} hello, world
# Example using extended variable syntax
${upper}= set variable ${message.upper()}
should be equal ${upper} HELLO, WORLD
您的代码存在的问题是您对my_component
和${cmp}
代表什么有误解.它们不是python对象.相反,它是机器人框架库的 name .即使在后台,它可能作为对库中定义的类的实例的引用而存在,但从测试my_component
内来看,只是库的 name .
The problem with your code is your misunderstanding of what my_component
and ${cmp}
represents. They are not python objects. Rather, it is the name of a robot framework library. Even though under the hood it may exist as a reference to an instance of a class defined in the library, from within the test my_component
is simply the name of the library.
这就是my_component.my_method
起作用的原因-my_component
是库的名称,而my_method
是该库内的关键字的名称.这是标准的机器人框架语法.请参阅《机器人框架用户指南》中的明确指定关键字
This is why my_component.my_method
works - my_component
is the name of a library, and my_method
is the name of a keyword within that library. This is standard robot framework syntax. See Specifying a keyword explicitly in the robot framework user guide.
如果希望像对象一样传递my_component
,则可以使用run keyword
运行在该库中实现的关键字.
If you want to be able to pass my_component
around as if it were an object, you can use run keyword
to run the keywords implemented in that library.
例如,首先使用以下代码创建MyClass.py
:
For example, start by creating MyClass.py
with the following code:
class MyClass(object):
def mymethod(self):
return "Hello, world"
如果将call method
替换为run keyword
,则您的代码可以使用:
Your code can work if you replace call method
with run keyword
:
*** Keywords ***
testing component
[Arguments] ${cmp}
log to console ************* ${cmp}
${result} = run keyword ${cmp}.mymethod
最后,如果您真的想传递实际的库 object ,则可以使用内置关键字调用方法
Finally, if you really want to pass the actual library object around, you can use the built-in keyword Get Library Instance to get the actual library object, which you can then use with Call Method
*** Keywords ***
testing component
[Arguments] ${cmp_name}
${cmp}= Get library instance ${cmp_name}
log to console ************* ${cmp}
${result} = call method ${cmp} mymethod
[return] ${result}
奇怪的是,在这种情况下扩展变量语法不起作用.我不知道为什么
Oddly, the extended variable syntax doesn't work in this case. I don't know why.
这篇关于如何在机器人框架中将对象传递给关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!