Multiple initialization of a Library (SeleniumLibrary)

1412 views
Skip to first unread message

AndreasEK

unread,
Apr 14, 2010, 5:10:17 AM4/14/10
to robotframework-users
Hi,

I'd like to refactor some of our tests. We have an interesting
phenomenon that some tests don't run, when the whole suite (directory)
is executed (at some point the browser window does not close, despite
a call to "Close All Browsers"). Since the all use the
SeleniumLibrary, but import that in different ways, I suspect that
inconsisten and/or multiple initialization could be the problem.

From the user guide, I took:

a) It is possible to import test libraries in test case files,
resource files and test suite initialization files. In all these
cases, all the keywords in the imported library are available in that
file. With resource files, those keywords are also available in other
files using them.

b) The library name is got normally from the module or class name
implementing it, but there are some situations where changing it is
desirable: There is a need to import the same library several times
with different arguments. This is not possible otherwise.

My first attempt was to import SeleniumLibrary in an __init__.txt:
Include the Library with the correct settings, and call Teardown
Selenium on Test Teardown (custom keyword that takes screenshot when
test fails, and closes window). And also call "Close All Browsers" on
Suite Teardown. I also removed all other imports from other resources
and test case files. Well, that didn't work, because of a).

On the otherhand, I have not had problems when SeleniumLibrary was
imported in a resource file AND also in a test case file. But I am
wondering what is happening then with the parameters, that you pass to
the SeleniumLibrary. Do they overwrite each other? Who is winning?

What would your strategy be to deal with this? Sorry that this problem
report is still a little fuzzy, I'm still in the process of
understanding the problem :)

Andreas

AndreasEK

unread,
Apr 14, 2010, 3:29:16 PM4/14/10
to robotframework-users
I have a follow up question. The User Guide states:

c) The user keywords and variables defined in a resource file are
available in the file that takes that resource file into use.
Similarly available are also all keywords and variables from the
libraries, resource files and variable files imported by the said
resource file.

If I understand that correctly, I can have a test case, that uses
resource file R-A. Resource file R-A uses resource file R-B and
library L-B. All keywords from R-B and L-B are available in the test
case, right? Are you sure this is the case?

Andreas

AndreasEK

unread,
Apr 14, 2010, 4:20:54 PM4/14/10
to robotframework-users
Hi,

On Apr 14, 9:29 pm, AndreasEK <Andreas.Ebb...@gmx.de> wrote:

> case, right? Are you sure this is the case?

Nevermind. Typo in the test case :(

Andreas

Janne Härkönen

unread,
Apr 15, 2010, 1:14:55 AM4/15/10
to Andreas...@gmx.de, robotframework-users
On Wed, Apr 14, 2010 at 12:10 PM, AndreasEK <Andreas...@gmx.de> wrote:
> Hi,

>
> On the otherhand, I have not had problems when SeleniumLibrary was
> imported in a resource file AND also in a test case file. But I am
> wondering what is happening then with the parameters, that you pass to
> the SeleniumLibrary. Do they overwrite each other? Who is winning?
>
Hello,

There are two related concepts here:

First is the higher level suite setup, which you already discovered,
and which has to be used in test case directory's __init__ -file.

The other is sharing the imports. This can only be done via resource
files. So, if you want to guarantee that you use the same library in
all suites, the library should be taken into use in a resource file,
and that resource file is imported by the __init__ -file and the
actual test case files.

There's also a third bit of information. described in [1], which
combined with the fact that SeleniumLibrary's scope is global, means
that even if you take SeleniumLibrary into use many times with same
arguments, the actual instance will always be same.

[1] http://robotframework.googlecode.com/svn/tags/robotframework-2.1.3/doc/userguide/RobotFrameworkUserGuide.html#test-library-scope


hth,
__janne

AndreasEK

unread,
Apr 15, 2010, 3:37:39 AM4/15/10
to robotframework-users
Hi Janne,


On Apr 15, 7:14 am, Janne Härkönen <janne.t.harko...@gmail.com> wrote:

> First is the higher level suite setup, which you already discovered,
> and which has to be used in test case directory's  __init__ -file.
>
> The other is sharing the imports. This can only be done via resource
> files. So, if you want to guarantee that you use the same library in
> all suites, the library should be taken into use in a resource file,
> and that resource file is imported by the __init__ -file and the
> actual test case files.

Hm, interesting. You say, that the __init__ file AND the test case
file should import the resource file. For my understanding, this is
* so that the __init__ file defines which parameters should be used to
initialize the library
* so that the keywords are available in the test case, if the
parameters deviate from the ones used in the __init__ file, I assume
there will be an error message?

> There's also a third bit of information. described in [1], which
> combined with the fact that SeleniumLibrary's scope is global,  means
> that even if you take SeleniumLibrary into use many times with same
> arguments, the actual instance will always be same.

I knew about the scope, but I could not find it in the documentation
of the Selenium Library. I think that's a useful bit of information
that should be in there. (And if it's in there, it should be more
prominent :)

Looking at the source for the SeleniumLibrary I found the
ROBOT_LIBRARY_SCOPE = 'GLOBAL'.

Thanks,
Andreas

Pekka Klärck

unread,
Apr 15, 2010, 4:51:54 AM4/15/10
to Andreas...@gmx.de, robotframework-users
2010/4/15 AndreasEK <Andreas...@gmx.de>:

> On Apr 15, 7:14 am, Janne Härkönen <janne.t.harko...@gmail.com> wrote:
>
>> First is the higher level suite setup, which you already discovered,
>> and which has to be used in test case directory's  __init__ -file.
>>
>> The other is sharing the imports. This can only be done via resource
>> files. So, if you want to guarantee that you use the same library in
>> all suites, the library should be taken into use in a resource file,
>> and that resource file is imported by the __init__ -file and the
>> actual test case files.
>
> Hm, interesting. You say, that the __init__ file AND the test case
> file should import the resource file. For my understanding, this is
> * so that the __init__ file defines which parameters should be used to
> initialize the library

Are you talking about __init__.py in SeleniumLibrary or __init__.txt
(or __init__.html or __init__.tsv) used by Robot Framework? The
__init__.py file has SeleniumLibrary class and its __init__ method
defines parameters that the library accepts. RF's __init__.txt files
are used to set settings such as Documention and Suite Setup for test
suites created from directories. No keyword or variables imported or
otherwise created in __init__.txt files are automatically available to
lower level suites.

> * so that the keywords are available in the test case, if the
> parameters deviate from the ones used in the __init__ file, I assume
> there will be an error message?

Yes, taking a library into use with wrong parameters fails.

>> There's also a third bit of information. described in [1], which
>> combined with the fact that SeleniumLibrary's scope is global,  means
>> that even if you take SeleniumLibrary into use many times with same
>> arguments, the actual instance will always be same.

Notice also that if you take the library into use with different
parameters on a different suite, you will get different instance there
regardless the scope.

> I knew about the scope, but I could not find it in the documentation
> of the Selenium Library. I think that's a useful bit of information
> that should be in there. (And if it's in there, it should be more
> prominent :)

True, libdoc.py should write this information to the documentation it
generates. Please submit an enhancement request about it.

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

AndreasEK

unread,
Apr 15, 2010, 5:48:10 AM4/15/10
to robotframework-users
Good Morning,

On Apr 15, 10:51 am, Pekka Klärck <pekka.kla...@gmail.com> wrote:
> 2010/4/15 AndreasEK <Andreas.Ebb...@gmx.de>:


>
> > On Apr 15, 7:14 am, Janne Härkönen <janne.t.harko...@gmail.com> wrote:
>
> >> First is the higher level suite setup, which you already discovered,
> >> and which has to be used in test case directory's  __init__ -file.
>
> >> The other is sharing the imports. This can only be done via resource
> >> files. So, if you want to guarantee that you use the same library in
> >> all suites, the library should be taken into use in a resource file,
> >> and that resource file is imported by the __init__ -file and the
> >> actual test case files.
>
> > Hm, interesting. You say, that the __init__ file AND the test case
> > file should import the resource file. For my understanding, this is
> > * so that the __init__ file defines which parameters should be used to
> > initialize the library
>
> Are you talking about __init__.py in SeleniumLibrary or __init__.txt
> (or __init__.html or __init__.tsv) used by Robot Framework?

The latter.

> RF's __init__.txt files
> are used to set settings such as Documention and Suite Setup for test
> suites created from directories. No keyword or variables imported or
> otherwise created in __init__.txt files are automatically available to
> lower level suites.

not automatically means that there's a way to achieve that?

The way I understood Janne is that I should import the Library in a
Resource file, which then has to be imported in the __init__.txt and
all test cases.

> > * so that the keywords are available in the test case, if the
> > parameters deviate from the ones used in the __init__ file, I assume
> > there will be an error message?
>
> Yes, taking a library into use with wrong parameters fails.

Well, what does wrong parameters mean? For example, I could do:

in __init__.txt

Library SeleniumLibrary 1m 192.168.30.30 3333

And in a test case (in the directory that is initialized with
__init__.txt above)

Library SeleniumLibrary 2s localhost 4444

I could just try it, but now I'm asking. Will that generate an error
at runtime?

> Notice also that if you take the library into use with different
> parameters on a different suite, you will get different instance there
> regardless the scope.

Things are getting complicated :)

> True, libdoc.py should write this information to the documentation it
> generates. Please submit an enhancement request about it.

http://code.google.com/p/robotframework/issues/detail?id=533

Andreas

Pekka Klärck

unread,
Apr 15, 2010, 6:23:53 AM4/15/10
to Andreas...@gmx.de, robotframework-users
2010/4/15 AndreasEK <Andreas...@gmx.de>:

> On Apr 15, 10:51 am, Pekka Klärck <pekka.kla...@gmail.com> wrote:
>
>> RF's __init__.txt files
>> are used to set settings such as Documention and Suite Setup for test
>> suites created from directories. No keyword or variables imported or
>> otherwise created in __init__.txt files are automatically available to
>> lower level suites.
>
> not automatically means that there's a way to achieve that?
>
> The way I understood Janne is that I should import the Library in a
> Resource file, which then has to be imported in the __init__.txt and
> all test cases.

This is the only way to have imports in one place.

>> > * so that the keywords are available in the test case, if the
>> > parameters deviate from the ones used in the __init__ file, I assume
>> > there will be an error message?
>>
>> Yes, taking a library into use with wrong parameters fails.
>
> Well, what does wrong parameters mean? For example, I could do:
>
> in __init__.txt
>
> Library  SeleniumLibrary  1m  192.168.30.30  3333
>
> And in a test case (in the directory that is initialized with
> __init__.txt above)
>
> Library  SeleniumLibrary 2s  localhost  4444
>
> I could just try it, but now I'm asking. Will that generate an error
> at runtime?

There shouldn't be errors in this case. You can use different
arguments when you take the library into use. What I meant with wrong
arguments was doing something linke `Library SeleniumLibrary invalid
args here`.

>> Notice also that if you take the library into use with different
>> parameters on a different suite, you will get different instance there
>> regardless the scope.
>
> Things are getting complicated :)

This is actually the exact same case you illustrated above.

>> True, libdoc.py should write this information to the documentation it
>> generates. Please submit an enhancement request about it.
>
> http://code.google.com/p/robotframework/issues/detail?id=533

Thanks. I can probably implement that already today. I have some
libdoc.py style enhancements to do also.

AndreasEK

unread,
Apr 15, 2010, 7:16:54 AM4/15/10
to robotframework-users
Hi,

On Apr 15, 12:23 pm, Pekka Klärck <pekka.kla...@gmail.com> wrote:
> 2010/4/15 AndreasEK <Andreas.Ebb...@gmx.de>:
>

> > On Apr 15, 10:51 am, Pekka Klärck <pekka.kla...@gmail.com> wrote:
>
> >> > * so that the keywords are available in the test case, if the
> >> > parameters deviate from the ones used in the __init__ file, I assume
> >> > there will be an error message?
>
> >> Yes, taking a library into use with wrong parameters fails.
>
> > Well, what does wrong parameters mean? For example, I could do:
>
> > in __init__.txt
>
> > Library  SeleniumLibrary  1m  192.168.30.30  3333
>
> > And in a test case (in the directory that is initialized with
> > __init__.txt above)
>
> > Library  SeleniumLibrary 2s  localhost  4444
>

[...]


>
> >> Notice also that if you take the library into use with different
> >> parameters on a different suite, you will get different instance there
> >> regardless the scope.
>
> > Things are getting complicated :)
>
> This is actually the exact same case you illustrated above.

So if I don't pay attention to the parameters, with which a Library is
initialized, I might end up with two instances of a library in a
single test case, even if the Library is scoped GLOBAL. Correct?

Andreas

Pekka Klärck

unread,
Apr 15, 2010, 8:48:58 AM4/15/10
to Andreas...@gmx.de, robotframework-users
2010/4/15 AndreasEK <Andreas...@gmx.de>:

>
> So if I don't pay attention to the parameters, with which a Library is
> initialized, I might end up with two instances of a library in a
> single test case, even if the Library is scoped GLOBAL. Correct?

That's correct. It would be pretty hard to use the same instance when
the library is initialized with different arguments.

AndreasEK

unread,
Apr 15, 2010, 10:29:12 AM4/15/10
to robotframework-users
Hi,

On Apr 15, 2:48 pm, Pekka Klärck <pekka.kla...@gmail.com> wrote:

> > So if I don't pay attention to the parameters, with which a Library is
> > initialized, I might end up with two instances of a library in a
> > single test case, even if the Library is scoped GLOBAL. Correct?
>
> That's correct. It would be pretty hard to use the same instance when
> the library is initialized with different arguments.

Thanks for your patience and help!

Andreas

Idikoro Eradiri

unread,
Apr 24, 2015, 4:50:17 PM4/24/15
to robotframe...@googlegroups.com
I'm not sure if there's anyone still monitoring this thread, but I am new to robot and am still very confused  about how to use the directory __init__.txt file to import the Selenium library.  I'll try to explain what I mean with an example: 

Lets say I have a test directory with a structure like this:

/TestDir
 -/AdaptersDir
  ----SeleniumAdapter.txt
 -/TestSuitesDir
  ----__init__.txt
  ----TestCase.robot


Inside my SeleniumAdapter.txt all I would have is this:
*** Settings ***
Documentation  selenium adapter
Library  Selenium2Library


Inside my __init__.txt file I would have to following to make sure I'm using the same instance of the Selenium2Library:
*** Settings ***
Documentation  init file for test suite
Resource  Adapters/SeleniumAdapter.txt


Would I also have the following in my TestCase.robot file? :
*** Settings ***
Documentatino  test case for test suite
Resource  Adapters/SeleniumAdapter.txt

My understanding is that I would now have a central place,SeleniumAdapter.txt, to change the Selenium2Library parameters. This also ensures that I'm using the same Selenium instance across my test suite.
Am I correct?


Thanks,
Idikoro

Tatu Aalto

unread,
Apr 26, 2015, 8:00:39 AM4/26/15
to iera...@gmail.com, robotframe...@googlegroups.com
Ugh

Although your assumption is correct, it is not totally correct from the
reason you assume/explain.

> My understanding is that I would now have a central
> place,SeleniumAdapter.txt, to change the Selenium2Library parameters.
This is correct, you now have one place to import Selennium2Library. I
also use similar way to import all my libraries. It must be noted that,
Robot Framework does not force you to use this method or any other
method, you must internally agree how your library imports are done and
controlled. One can freely import libraries almost anywhere.
> This also ensures that I'm using the same Selenium instance across my
> test suite.
This is not correct. The library instance is not determined where you
import you library, it is controlled by the library itself by the
library scope. The library scope is explained in the user guide [1].
Because Selenium2Library has a global scope, you will have only one
instance of Selenium2Library.

Note that the above answers do not change, is the __init__.robot file
used or not.

-Tatu
[1]
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-library-scope

Reply all
Reply to author
Forward
0 new messages