在 Robot Framework 中导入基于 python 的库后如何创建 2 个对象

How to create 2 Objects after importing python based libraries in Robot Framework

我正在导入一个 python 库,并希望创建两个具有不同参数和调用方法的对象,这些对象在 class 中定义。

demo.py

class Sample:

    def __init__(self,path,device):
            self.device=device
            self.path = path
            print(self.device)
            print(self.path)

    def getting_path(self):
            print(self.path)
            print(self.device)
            return self.path

demo.robot
===============
*** Settings ***
*** Variables ***
${path}    c:
${device}    samsung
${path1}    D:
${device1}    samsung11
*** Test Cases ***
Test
    Test_python_class
*** Keywords ***

Test_python_class
     Import Library      demo.Sample    ${path1}    ${device1}    
     ${result} =     demo.sample.getting_path
     log     ${result}
     Import Library      demo.Sample    ${path}    ${device}
     ${result1} =     demo.sample.getting_path
     log     ${result1}

它没有创建第二个对象。 ${result} 和 {result1} 打印相同的值。

我可以通过使用带有两个值的 WITH 名称的以下语法来实现此目的。 并使用 WITH NAME

像下面这样调用
Import Library      demo.Sample    ${path1}    ${device1}    With Name     c1
     ${result} =     c1.getting_path
     log     ${result}
     Import Library      demo.Sample    ${path}    ${device}   With Name     c2
     ${result1} =     c2.getting_path
     log     ${result1}

但这个解决方案并不是最优的。如果我需要创建 10 个不同值的对象,我需要在这里使用 10 个 import 语句。

如果有人可以提供有关最佳解决方案的任何输入,我将不胜感激,我可以将此步骤定义为机器人 function/keyword,它将为构造函数获取这些参数,并 return 我将对象句柄,以便我们可以使用不同的对象调用 class 方法。

您可以进行以下更改以使其正常工作

  1. 用文件名重命名 python class。

demo.py

class demo:

def __init__(self,path,device):
        self.device=device
        self.path = path
        print(self.device)
        print(self.path)

def getting_path(self,path2,device2):
    self.path=path2
    self.device=device2
    print(self.path)
    print(self.device)
    return self.path
  1. 在设置部分导入 class

  2. 使用 [Template] 功能使您的 KW 数据驱动

demo.robot

    *** Settings ***
Library     demo.py     path=${path}   device=${device}        WITH NAME    mylib
*** Variables ***
${path}    f:
${device}    samsung

*** Test Cases ***
Test
    [Template]  Test_python_class
    c:  samsung
    d:  HTC
    e:  Yahoo
    f:  Sony

*** Keywords ***
Test_python_class
    [Arguments]  ${arg1}  ${arg2}
     ${result} =     mylib.getting path  ${arg1}    ${arg2}
     log to console     ${result}

输出

==============================================================================
Test                                                                  c:
.d:
.e:
.f:
Test                                                                  | PASS |
------------------------------------------------------------------------------

您可以继续增加参数的数量,如测试用例所示。

机器人框架并不是真正为以这种方式创建对象而设计的。当您使用 import library 关键字(或 Library 设置)时,机器人需要一个关键字库,而不是标准的 python 模块。

相反,我建议创建一个合适的关键字库,其中包含用于创建对象的关键字。

例如,从一个如下所示的 SampleLibrary.py 文件开始:

# SampleLibrary.py
from demo import Sample

class SampleLibrary:
    def get_sample(self, path, device):
        return Sample(path, device)

然后,在你的测试中你会做这样的事情:

*** Settings ***
Library  SampleLibrary

*** Test Cases ***
Example
    ${sample1}=  get sample  ${path}   ${device}
    Call method  ${sample1}  getting_path

    ${sample2}=  get sample  ${path1}  ${device1}
    Call method  ${sample2}  getting_path