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