RE: creating new library using other libraries for Robot Framework

773 views
Skip to first unread message

Taylor, Martin

unread,
May 8, 2013, 8:14:08 AM5/8/13
to martin...@gmail.com, robotframe...@googlegroups.com

Why don’t you make the class B in module B derive from Class A in module A?  That way  B would expose all of the KW methods in A, plus you could add your new KW methods for B that use methods from A.  The result would be a single import from RF that should work fine.  (I offer this solution ‘cause I’ve done it myself.)

 

Cheers,

Martin

 

From: robotframe...@googlegroups.com [mailto:robotframe...@googlegroups.com] On Behalf Of Martin Cosyns
Sent: Wednesday, May 08, 2013 3:10 AM
To: robotframe...@googlegroups.com
Subject: creating new library using other libraries for Robot Framework

 

Dear all,

 

currently I’m writing a new library (module B) for Robot Framework in Python. This library uses keywords from a second library (module A). So I imported module A into module B. Maybe I have to mention that the constructor of the class A has a parameter. So my solution was to add a parameter to class B’s constructor too and load an instance of class A within the constructor of class B (check attached code snippet). So far so good. This works fine both in Python environment and in the Robot Framework when I use the library standalone (means I use just the new library and its keywords).

 

I want to extend the tests by using different keywords from library A. So I have to import the library A into Robot Framework as well to access those keywords. Here an excerpt from the Robot Framework script:

 

*** Settings ***

Library  | Library_A | ${Argument_A}

Library  | Library_B | ${Argument_A}

 

*** Variables ***

${Argument_A} | aValue

 

*** Test Cases ***

Test new library

                Keyword_from_Library_A

Keyword_from_Library_B

 

With this the Keyword_from_Library_A  works while Keyword_from_Library_B doesn’t anymore (“AttributeError: Class_A instance has no attribute ‘private_attribute_C’”). If I change the import order of the libraries, the Keyword_from_Library_A doesn’t work (“AttributeError: Class_A instance has no attribute ‘private_attribute_D’”), but Keyword_from_Library_B works now.

 

The long text leads to the question, how to get out from this vicious circle?

--
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-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Pekka Klärck

unread,
May 9, 2013, 3:19:42 AM5/9/13
to martin...@gmail.com, robotframe...@googlegroups.com
2013/5/8 Martin Cosyns <martin...@gmail.com>:
>
> currently I’m writing a new library (module B) for Robot Framework in
> Python. This library uses keywords from a second library (module A). So I
> imported module A into module B. Maybe I have to mention that the
> constructor of the class A has a parameter. So my solution was to add a
> parameter to class B’s constructor too and load an instance of class A
> within the constructor of class B (check attached code snippet). So far so
> good. This works fine both in Python environment and in the Robot Framework
> when I use the library standalone (means I use just the new library and its
> keywords).
>
> I want to extend the tests by using different keywords from library A. So I
> have to import the library A into Robot Framework as well to access those
> keywords.

I don't see why this wouldn't work. Creating your library B so that it
extends A, like Martin Taylor recommended, might be a simpler
solution, though.

> Here an excerpt from the Robot Framework script:
[snip]
>
> With this the Keyword_from_Library_A works while Keyword_from_Library_B
> doesn’t anymore (“AttributeError: Class_A instance has no attribute
> ‘private_attribute_C’”). If I change the import order of the libraries, the
> Keyword_from_Library_A doesn’t work (“AttributeError: Class_A instance has
> no attribute ‘private_attribute_D’”), but Keyword_from_Library_B works now.

I have no idea what's going on there. The example you had included and
the library you had attached clearly didn't contain all the related
code because they didn't access private_attribute_C or
private_attribute_D. If you can create simpler example demonstrating
the same problem, preferably on that also others could execute, then
it would be easier to understand what is happening.

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

immo

unread,
May 9, 2013, 5:43:17 AM5/9/13
to robotframe...@googlegroups.com
Hi folks,

Let me give you some more background info.
Library B is not an child or Inherit class from the Object oriented programming point. It's just going to use some functionality of this library. Furthermore is a inherit class of another one. Therefore this approach will not work. Of course even python supports multiple inheritance but this would even be just an very bad workaround.
So my proposal for a workaround would be to create a ressource in robot which loads both libraries.
First we use keyword A.KW from library A to get the data and afterwards handover the result to the Keyword from Lib B ( B.KW). This would bypass the problem but not really solve it.

The main problem is to understand how robot&python loads and hold the libraries and in which context. Probably that is a way to solve the issue.

Immo

Kevin O.

unread,
May 9, 2013, 8:54:30 AM5/9/13
to robotframe...@googlegroups.com, martin...@gmail.com
I agree with Pekka. In an effort to make your post & attachment shorter and readable, you have removed the code that is causing the problem, making it hard for us to help.
Your structure seems okay to me. I would start by finding where these attributes are created and find a code path that would prevent them from being created.
If you are not seeing any warnings about the same keyword being found in more than one library, I can think of some possibilities:
You are creating an attribute in a try/except block and the exception is being suppressed. For this to happen, a change in global state in one library must affect the other.
You are creating an attribute in a conditional code block that is not being executed. For this to happen, a change in global state in one library must affect the other.
Your libraries are messing with the system path and you have some other copies of the libraries lying around (Python will load a .pyc file even if you deleted the .py file).

Martin Cosyns

unread,
May 13, 2013, 8:30:14 AM5/13/13
to robotframe...@googlegroups.com
Hello to all,
 
We found the reason for this behaviour in the constructor of Library_A. The constructor controls the number of its calls. This means, I have to use the BuiltIn.get_library_instance() instead of creating a new instance of Library_A.
 
Thanks for your help.

Am Mittwoch, 8. Mai 2013 10:09:46 UTC+2 schrieb Martin Cosyns:

Dear all,

 

currently I’m writing a new library (module B) for Robot Framework in Python. This library uses keywords from a second library (module A). So I imported module A into module B. Maybe I have to mention that the constructor of the class A has a parameter. So my solution was to add a parameter to class B’s constructor too and load an instance of class A within the constructor of class B (check attached code snippet). So far so good. This works fine both in Python environment and in the Robot Framework when I use the library standalone (means I use just the new library and its keywords).

 

I want to extend the tests by using different keywords from library A. So I have to import the library A into Robot Framework as well to access those keywords. Here an excerpt from the Robot Framework script:

 

*** Settings ***

Library  | Library_A | ${Argument_A}

Library  | Library_B | ${Argument_A}

 

*** Variables ***

${Argument_A} | aValue

 

*** Test Cases ***

Test new library

                Keyword_from_Library_A

Keyword_from_Library_B

 

With this the Keyword_from_Library_A  works while Keyword_from_Library_B doesn’t anymore (“AttributeError: Class_A instance has no attribute ‘private_attribute_C’”). If I change the import order of the libraries, the Keyword_from_Library_A doesn’t work (“AttributeError: Class_A instance has no attribute ‘private_attribute_D’”), but Keyword_from_Library_B works now.

 

The long text leads to the question, how to get out from this vicious circle?
Reply all
Reply to author
Forward
0 new messages