如何在机器人框架中导入基于python的库后创建2个对象?

问题描述 投票:0回答:2

我正在导入一个python库,想创建两个具有不同参数的对象,并在类中定义调用方法。

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 name加上两个值,然后像下面一样使用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个导入语句。

如果有谁能提供任何关于最佳解决方案的意见,我可以将这个步骤定义为机器人函数关键字,它将把这些参数作为构造函数,并返回对象句柄,这样我们就可以使用不同的对象调用类方法。

python-3.x python-2.7 robotframework robotframework-sshlibrary
2个回答
0
投票

机器人框架并不是真的设计成这样创建对象的。当你使用 import library 关键字 Library setting),robot期望的是一个关键字库,而不是一个标准的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

0
投票

你可以做以下的修改来使它工作

  1. 用文件名重命名你的python类。

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. 在设置部分导入该类

  2. 使用[模板]功能,让你的KW数据驱动。

演示.机器人

    *** 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 |
------------------------------------------------------------------------------

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

© www.soinside.com 2019 - 2024. All rights reserved.