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

雨燕双飞 提交于 2020-04-30 06:30:46

问题


I am importing a python libraries and wanted to create two objects with different arguments and call method defined in the 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}

It is not creating second object. ${result} and {result1} printing the same value.

I can achieve this by using below syntax using WITH name with two value. and calling like below using 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}

But this solution is not optimal. If I need to create 10 objects with different value , I need to use 10 import statements here.

Appreciate if anybody can provide any inputs on optimum solution, where I can define this steps as robot function/keyword which will take these arguments for constructor and return me the object handle so that we can call class method using different object.


回答1:


Robot framework isn't really designed to create objects this way. When you use the import library keyword (or the Library setting), robot expects a keyword library, not a standard python module.

Instead, I recommend creating a proper keyword library which has a keyword for creating your objects.

For example, start with a SampleLibrary.py file that looks like this:

# SampleLibrary.py
from demo import Sample

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

Then, in your test you would do something like this:

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



回答2:


You can make following changes to make it work

  1. Rename your python class with file name.

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. Import the class in settings section

  2. Use [Template] Functionality to make your KW data driven

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}

Output

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

You can keep increasing the number of parameter as shown in the test case.



来源:https://stackoverflow.com/questions/61448984/how-to-create-2-objects-after-importing-python-based-libraries-in-robot-framewor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!