1

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.

Helio
  • 3,322
  • 1
  • 14
  • 23

2 Answers2

0

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.

pankaj mishra
  • 2,555
  • 2
  • 17
  • 31
  • Hi Pankaj, Thnak you very much for your reply. As I mentioned in last section of description with "WITH NAME " It is working fine. But In my script If i have to to create 10 different objects, I need to use 10 import statement with ( WITH NAME say lib1, lib2,lib3....etc. Will there not be any optimum solution here . If we can make it this import statmenet in function and call with diff arguments or It is not possible in Robot Framework – Manish Verma Apr 27 '20 at 11:18
  • @ManishVerma My apologies for missing that part, please see edited answer – pankaj mishra Apr 27 '20 at 18:22
0

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
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685