I have created a Python library with a method that returns the object self. I saved this object into a robot variable. But I am unable to access the method present in this object by using syntax ${var}.method_name or ${var.name}
Test
${val}= Init
Log To Console \n${val}
${word}= Run Keyword ${val}.name
Log To Console ${word}
Result:
==============================================================================
Test .
<sing.sing object at 0x7ffff06584e0>
Test | FAIL |
No keyword with name '<sing.sing object at 0x7ffff06584e0>.name' found.
------------------------------------------------------------------------------
Trying to access as ${var.name}
Test
${val}= Init
Log To Console \n${val}
${word}= Run Keyword ${val.name}
Log To Console ${word}
Result:
Test .
<sing.sing object at 0x7ffff0658d30>
Test | FAIL |
Keyword name must be a string.
*** Settings ***
Library sing.py
*** Test Cases ***
Test
${val}= Init
Log To Console \n${val}
${word}= Set Variable ${val.name()}
Log To Console ${word}
I don’t get the same behaviour when I execute the below code. I get Keyword name cannot be empty error. I basically want to execute any method from the obj stored in variable val.
*** Settings ***
Library sing.py
*** Test Cases ***
Test
${val}= Init
Log To Console \n${val}
${word}= ${val.name()}
Log To Console ${word}
Thanks… I have a python file that returns more than 1 object at a time. So I’m finding ways to save the returned object into a variable in robot and then call the methods in that object.
*** Settings ***
Library sing.py
*** Test Cases ***
Test
${val}= Init
Log To Console \n${val}
${word}= ${val.name()}
Log To Console ${word}
So I am trying to execute method - name from the object ${val} so that the variable ${word} gets the value “hi”.
The error that I see when I execute anusha.robot is “Keyword name cannot be empty.”
bash-4.2$ python -m robot anusha.robot
==============================================================================
Anusha
==============================================================================
Test .
<sing.sing object at 0x7ffff0658cc0>
Test | FAIL |
Keyword name cannot be empty.
------------------------------------------------------------------------------
Keyword name cannot be empty. Ah ok, I guess that makes sense, because the keyword hi doesn’t exist there’s no value to assign to to ${word}, In my example from a while back I used the keyword Set Variable to set the value of ${word} to hi.
Robot Framework evaluates ${val.name()} first and then tries to process the line with robot interpreter so this:
${word}= ${val.name()}
is the same to robot framework as this:
${word}= hi
The python function call (val.name()) is processed first → it’s value (hi) is passed into the variable ${val.name()} → then robot framework tries to work out what to do with ${word}= hi, and obviously this is not valid.
The question is what were you wanting it to do? what was the reason to remove Set Variable?
My primary intention is to be able to run the object methods. The methods themselves may or may not return any value. . For example like this:
bash-4.2$ cat sing.py
#!/usr/bin/env python
class sing():
def __init__(self):
pass
def init(self):
return self
def name(self):
print("hi")
bash-4.2$ cat anusha.robot
*** Settings ***
Library sing.py
*** Test Cases ***
Test
${val}= Init
Log To Console \n${val}
${val.name()}
But I get the same error Keyword name cannot be empty.
I understand why this behaves so. Can you please provide me a solution for my code to work ie being able to run keywords from the object created.
I do not want to import it right at the beginning. I want the object to get initialised only on certain conditions and will need multiple instances of the class sing().
Consider the example below where the python file does nothing until one of it’s functions are called. You can do something like this, there’s no need to have an __init__ function in your py file (it’'s optional), but if you have a some initialisation that needs to happen before the other keyword functions, just put it in a normal function and call it as a keyword before the keyword that needs it inside your if statement.
Dave.
singa.py
class singa():
def Sound_Music(self):
return "Doe a Deer"
def Song_ABBA(self):
return "I am the Dancing Queen"
*** Settings ***
Library singa.py
Library singb.py
Library singc.py
*** Test Cases ***
Test
${condion}= Evaluate random.randint(0, 13)
IF ${condion} == 0
${word}= Sound Music
Log To Console ${word}
END
IF ${condion} == 1
${word}= Song Abba
Log To Console ${word}
END
IF ${condion} == 2
${word}= Dancea
Log To Console ${word}
END
IF ${condion} == 3
${word}= Danceb
Log To Console ${word}
END
IF ${condion} == 4
${word}= Dancec
Log To Console ${word}
END
IF ${condion} == 5
${word}= Danced
Log To Console ${word}
END
IF ${condion} == 6
${word}= Dancee
Log To Console ${word}
END
IF ${condion} == 7
${word}= Dancef
Log To Console ${word}
END
IF ${condion} == 8
${word}= Danceg
Log To Console ${word}
END
IF ${condion} == 9
${word}= Danceh
Log To Console ${word}
END
IF ${condion} == 10
${word}= Song Ca
Log To Console ${word}
END
IF ${condion} == 11
${word}= Song Cb
Log To Console ${word}
END
IF ${condion} > 11
${word}= Song Ce
Log To Console ${word}
END