storing python class object in robot variable

2,097 views
Skip to first unread message

Madhavan.S.S

unread,
Dec 21, 2016, 12:00:26 PM12/21/16
to robotframework-users
Experts,

am trying to export my python methods as remote library. when my python method returns class object to robot framework, it is getting saved as string.
when i use the same python libraries as local library, its saved getting saved as class object.

am printing the variable type received by my python function

with Remote library
==============
<msg timestamp="20161221 21:45:29.543" level="INFO">This is variable type: &lt;type 'str'&gt;</msg>


with local library
============
<msg timestamp="20161221 22:09:27.964" level="INFO">This is variable type: &lt;class 'restlib.RestConnect'&gt;</msg>


My Robot file:
==========
*** Settings ***
Documentation    Suite description

Library    Remote    http://127.0.0.1:8270
Library    cpt.testcaseLib.ExampleLib.Example_keyword_Lib

*** Variables ***

${testplan}    ""
${nsx_mgr}     ""

*** Test Cases ***
Python Demo Test
    [tags]  Python
    Set nsx api connection    ${EXEC_DIR}/mseg_Scale.yml
    Disable ARP for Spoofgaurd ip detect
    check ARP status in Spoofgaurd ip detect

*** Keywords ***
Set nsx api connection
    [Documentation]    Initiate a Rest API connection NSX Manager
    [Arguments]    ${setup_yml_file}
    ${yml_dict} =    read contents from setup file    mseg_Scale.yml
    set suite variable    ${testplan}    ${yml_dict}
    ${nsx_con} =    get connection to nsxm    ${yml_dict}   => here i get the class object from one python method.
    set suite variable    ${nsx_mgr}    ${nsx_con}

Disable ARP for Spoofgaurd ip detect
    [Documentation]    Keyword to set the Spoof Guard IP detect settings
    ${status} =  set snoop setting    ${nsx_mgr}  true  false   => here i pass the class object to another python method.
    should be true  ${status}


check ARP status in Spoofgaurd ip detect
    [Documentation]    Keyword to check the Spoof Guard IP detect settings
    ${dhcp}  ${arp}    get snoop setting    ${nsx_mgr}
    log to console    "The state of DHCP is ${dhcp}"${\n}
    log to console    "The state of ARP is ${arp}"${\n}


please help.

Madhavan.S.S

Tatu Aalto

unread,
Dec 21, 2016, 2:38:10 PM12/21/16
to madhav...@gmail.com, robotframework-users
Ugh

This is expected behaviour and is explained in the user guide [1]. From user guide: "Other types are converted to strings". Please read the full chapter, because it explains many other things too. 

In practice if you want to return complex objects, like class instance, you need serialise the object in the remote side, example to a dictionary and then send it to Robot Framework side. Other option is to save the instance in the remote library and not to pass the instance to the Robot Framework side at all.

Which way to go, depends on what you did to plan to with the instance. If you did plan to return class instance and use the methods from the instance (like a selenium Webdriver instance and use it methods to find a element) then that is not possible by using the remote library. Then the best way is to store the instance in the remote side and access the methods by using the keywords you build.

But if your intanse contains only attributes and not methods then it is possible convert the attributes as dictionary and send the dictionary from remote side to the Robot Framework side. Then in the Robot Framework side you can use the dictionary as you please.

The big question is which way you should go and to that question I am not able to answer. You need to decide which way is the best for you. Think your requirements and needs, experiment  both ways and then decide which is best for you. Happy coding and holidays.

-Tatu
Send from my mobile

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Madhavan.S.S

unread,
Dec 22, 2016, 5:44:39 AM12/22/16
to robotframework-users, madhav...@gmail.com
Hi Tatu,

Thanks for the response. i have to go with storing the class object in the remote library itself.

Experts,

i have another very basic question. can i import java libraries like .class or .jar in robot and execute the robot script using pybot?

My requirement is such that one set of libraries are developed in python and other set of set of libraries developed in JAVA. we are looking for some common framework from where we can leverage both python and java libraries to create our tests.

i saw robot as a framework which has supports for both. now the task here is to import python and java libraries to the same robot script and write keywords for both.

when i tried to pybot, its not recognizing java libraries. when i tried jybot, my python libraries are not getting loaded with "Importing test library 'ExampleLib.Example_keyword_Lib' failed: Method code too large!" error. challenge with remote libraries is i can use only one class as remote library.

Any advice on how to proceed here?

Madhavan.
To post to this group, send email to robotframe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages