0

我有我的 python 模块名称 message.py

 #!/usr/bin/env python3
class TestClass1(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

    def print_args1(self):
        print ("Inside print_args1")
        print (self.arg1, self.arg2)

class TestClass2(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

    def print_args2(self):
        print ("Inside print_args2")
        #print (arg1, arg2)
        print (self.arg1, self.arg2)

如何在我的机器人脚本中导入此消息。以及如何创建这两个类的对象并在机器人框架中调用它们各自的实例方法。我的模块名称(message.py)与模块内定义的类名称不匹配。

我可以在另一个 python 文件中导入和使用。

#!/usr/bin/env python3
import message

obj1 = message.TestClass1(10,20)
obj2 = message.TestClass2(30,40)

obj1.print_args()
obj2.print_args2()

无法在我的机器人脚本中导入和创建对象。在设置表下的机器人脚本中

*** Settings ***
Library          message.py

我们如何在机器人脚本中创建 TestClass1 和 TestClass2 的对象。并使用这些对象调用他们的方法。

我能够在返回对象句柄的 python 文件中定义 python 函数。能够在机器人中调用这个 Python 函数,并且使用这个对象可以调用他们的方法。

下面是代码

ef newFun(arg1,arg2):
   return TestClass1(arg1,arg2)
def newFun2(arg1,arg2):
   return TestClass2(arg1,arg2)
=================
 The above two functions    newFun and newFun2 I created in python file and calling these function from robot script. This will return me TestClass1 and TestClass2 handle .
Then we can call their method print_args1 and print_args2 from robot using these objects.


Robot code  is as below.
======================>>>>hello.robot
*** Settings ***
Library          message.py
*** Variables ***
*** Keywords *** 
Create TestClass1 Object
        ${object1} =    newFun    10    20
        [Return]    ${object1}
*** Test Cases ***
First TestCase with TestClass1 object

    LOG    Instantiate TestClass1 Object    console=yes
    ${instance}    Create TestClass1 Object
    LOG    ${instance}
    LOG    Calling print_args1 function using TestClass1 Object    console=yes
    Call Method    ${instance}    print_args1

现在不是使用 python 函数(在我们的例如 newFun 和 newFun2 中)创建这些对象,有什么方法可以直接在机器人脚本中动态创建对象。

4

0 回答 0