Initializing class instances in RF

4,189 views
Skip to first unread message

Somesh Daga

unread,
Jan 21, 2014, 2:55:25 PM1/21/14
to robotframe...@googlegroups.com
I have imported a user-created python based-class to use as a library into robot framework. However, I cannot find a way to create an instance of the class that I have imported. My class has a constructor that takes in one argument.

e.g.
class Table:

def __init__(self,name)
    self.name=name

def doSomething(self)
    print self.name

@staticmethod
def compare(table1,table2)
    #doessomething

Also, I would further like to create multiple instances of the same class, so that I can utilize them in a static function together. I have tried going through other resources, but have not been able to find anything about creating an instance in the robot framework UI.
Could someone please guide me through it?

Thanks


Pekka Klärck

unread,
Jan 22, 2014, 4:51:40 AM1/22/14
to someshd...@gmail.com, robotframework-users
2014/1/21 Somesh Daga <someshd...@gmail.com>:
> I have imported a user-created python based-class to use as a library into
> robot framework. However, I cannot find a way to create an instance of the
> class that I have imported. My class has a constructor that takes in one
> argument.

If you import a class to Robot Framework as a library, Robot will take
care of instantiating it for you. See the following User Guide
sections for more information about using and creating test libraries.
For details about controlling when new instances are created, see the
Test library scope section.

http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html#using-test-libraries
http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html#creating-test-libraries

> Also, I would further like to create multiple instances of the same class,
> so that I can utilize them in a static function together. I have tried going
> through other resources, but have not been able to find anything about
> creating an instance in the robot framework UI.
> Could someone please guide me through it?

This indicates me that you should create a separate library that
keywords for returning instances of your other class. If using same
instances with all test is fine, you might be able to use variable
files too:

http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html#variable-files

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Pekka Klärck

unread,
Jan 23, 2014, 3:29:51 AM1/23/14
to Somesh Daga, robotframework-users
[Added robotframework-users back to Cc.]

2014/1/23 Somesh Daga <someshd...@gmail.com>:
> Thanks a lot for your explanation and the links were quite helpful.
> I understand now that importing a library multiple times with different
> arguments can create multiple instances of a class. Now my question is how
> do you access those instances? In RIDE, you can store a string as a
> variable, ${str}="blahblahblah" and then use the ${str} variable to keep
> referring to the string "blahblahblah". So considering that I am going to
> have multiple instances of my class, how do I call class functions on a
> specific instance because I don't have a variable names to refer to the
> instances? If we can assign variable names to the instances, how do we do
> it?

Methods implemented by your libraries are available as keywords. If
you import same library multiple times with different arguments, you
need to use the "WITH NAME" syntax to give unique name for each of
them. Assuming the imported libs have same keywords, you need to then
use them so that you prefix the keyword name with the library name
like "MyLib.My Keyword". Both the "WITH NAME" syntax and resolving
conflicts when multiple libraries have same keywords are discussed
more thoroughly in the User Guide.

Notice that I'm not sure is using these classes as libraries the best
approach in general. Depending on your use case, you might want to
create a separate library that has keywords that return instances of
you other class., You can then easily pass those instances to other
keywords if that's needed.

To get a better understanding how libraries work in practice, you may
want to study this demo project:
https://bitbucket.org/robotframework/robotdemo/wiki/Home

Живко Костадинов

unread,
Apr 4, 2017, 11:00:54 AM4/4/17
to robotframework-users

Or, you mus use like this:

class Table:

def __init__(self,name)
    self.name=name

def doSomething(self)
    print self.name

@staticmethod
def compare(table1,table2)
    #doessomething
-----------------------
   Suite Setup   Use table class
 
*** Settings ***
  Library  /path to class Table

  ***Keyword***
Use table class
 ${table_class}=  Get Library Instance Table
 set suite variable   ${tables_class}

 ***Test cases***
Log to console  ${table_calss. doSomething()}
Reply all
Reply to author
Forward
0 new messages