Accessing the selenium object directly in keywords implemented in python code

93 views
Skip to first unread message

afrotypa

unread,
Jul 25, 2011, 12:32:38 PM7/25/11
to robotframework-users
When there is complex logic involved in the implementation of keywords
for selenium driven in-browser testing, (and perhaps you need to
access selenium api functions not implemented as SeleniumLibrary
keywords) a good practice is to implement those keywords directly in
python.

In such a case it makes a whole lot of sense (i.e. it is more
convenient and in most cases more elegant) to interact directly with
the underlying selenium object instance.

So instead of the following code :-

from robot.libraries.BuiltIn import BuiltIn
def title_should_start_with(expected):
seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
xml_data =
seleniumlib.call_selenium_api('captureNetworkTraffic','xml')
# do something useful with xml_data
title = seleniumlib.get_title()
if not title.startswith(expected):
raise AssertionError("Title '%s' did not start with '%s'"
% (title, expected))

you could have the following code snippet which uses the private
selenium member object ('_selenium') of the SeleniumLibrary instance
directly:-

from robot.libraries.BuiltIn import BuiltIn
def title_should_start_with(expected):
seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
seleniumobj = seleniumlib._selenium
xml_data = seleniumobj.captureNetworkTraffic('xml')
# do something useful with xml_data
title = seleniumobj.get_title() #calling get_title directly on
the _selenium instance
if not title.startswith(expected):
raise AssertionError("Title '%s' did not start with '%s'"
% (title, expected))

This alternate approach would require that the ._selenium member
object of the robotframework SeleniumLibrary instance be always
available through subsequent releases.

I hope this is a good assumption to make.

Are there any thoughts on this?

Please get back.

thanks

Afrotypa

PS: In trying to express the underlying concept of accessing the
selenium object directly in RF keywords implemented in python, I have
written this code on the fly and did not test for correctness or run
it - so there might be errors in the code.

afrotypa

unread,
Jul 25, 2011, 1:19:05 PM7/25/11
to robotframework-users
Upon further reflection if it is deemed of considerable value to
access the _selenium member directly, it may be more pythonic for the
SeleniumLibrary API to explicitly provide a public reference to the
_selenium private member.

i.e. something like

def get_selenium_object(self):
return self._selenium

This is perhaps a more rock solid contract between the robotframework
Seleniumlibrary and its users.

Thoughts?.

Pekka Klärck

unread,
Jul 25, 2011, 5:08:28 PM7/25/11
to ovua...@gmail.com, robotframework-users
2011/7/25 afrotypa <ovua...@gmail.com>:

>
> This alternate approach would require that the ._selenium member
> object of the robotframework SeleniumLibrary instance be always
> available through subsequent releases.
>
> I hope this is a good assumption to make.

I think this is a pretty safe assumption. I don't think there's any
need to change the attribute in general and we know it would be
backwards incompatible.

I also agree with your comment in a subsequent mail about making this
attribute more public. Instead of a getter I would rather add
`selenium` or `se` property, though. Please submit an enhancement
request to SeLib's tracker and we'll add it for the next release.

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

afrotypa

unread,
Jul 25, 2011, 6:02:54 PM7/25/11
to robotframework-users
Done!.

Added issue 200 (following) as an enhancement request :-

http://code.google.com/p/robotframework-seleniumlibrary/issues/detail?id=200

Thanks Pekka!.

On Jul 25, 5:08 pm, Pekka Klärck <p...@iki.fi> wrote:
> 2011/7/25 afrotypa <ovuaia...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages