Reputation: 458
I would like to have a setup like shown below in my Library file in python for robot framework.
Class Main(foo):
def common_functions():
pass
Class Child1(Main):
def something_unique_child1():
pass
Class Child2(Main):
def something_unique_child2():
pass
However , to use these classes , I would have to import them in the .robot file individually like so :-
Library python_filename . Child1
Library python_filename . Child2
Now. The problem is , If I need to use the "Common_functions()" keyword , Robot throws an error stating that there are "Multiple Keywords with the same name"
I am guessing that since that function is accessible by both the subclasses , its getting confused.
How does one achieve this functionality in RF?
Additional Info: The reason I would like to have inheritance(as it has been pointed out to me that is not the best way to write libraries) is because there is a feature which has multiple functionalities under it , think of it like a menu feature
( File-->close , File--> save , File --> save as)
Now , This "Save" feature in itself has a lot of sub features" so opening the application and going to "File-->save" is a repetitive step which I have to do again and again for all the sub features.
I was thinking of putting this and a few other repetitive steps in a base class and simply inherit the function onto the sub features as to avoid code repetition.
Upvotes: 1
Views: 2103
Reputation: 386210
There's no need to import all three classes. Since Child2
inherits from Main
, and Child1
inherits from Main
, all you have to do is import Child1
and Child2
to get all of the keywords from all three classes.
However, if you need all of the keywords, you can have Child2
inherit from Child1
, and then you can just import Child2
to get all of the keywords.
If you insist on wanting to import all three, there are two things you can do.
First, you can fully qualify the keyword so that robot knows which library to use. For example, Child1.common_functions
.
The other thing you can do is to use the Set library search order built-in keyword to tell robot which library to use when there are conflicts.
Upvotes: 3
Reputation: 20067
Have you tried with qualified keyword name? Specifying the library which should provide the keyword, when there's a name conflict (like in your case):
python_filename.Main.Common Functions
And if typing out the full library.class is too cumbersome, you could import them with an alias - WITH NAME
, and use it:
Library python_filename.Child2 WITH NAME c2
....
c2.Common Functions
Upvotes: 1