What are the advantages of using Selenium2Library in page object design pattern?

702 views
Skip to first unread message

Mohan R

unread,
Dec 12, 2012, 1:27:45 PM12/12/12
to robotframe...@googlegroups.com
I am using page object design pattern where in each page is represented by a class and all the methods needed to interact with the page are contained within that class.
Due to this structure, I end up calling the selenium 2 library functions directly from python. I don't call them from the test cases. This make each of my page object a library that I can import and use in the test suites.
This has made me wonder, what is the advantage of using the Selenium2Library wrapper instead of the python binding for Selenium directly?
What are the differences?

I guess Selenium2Library provides several higher level and/or convenience functions than Selenium library.
Is there a document that lists the differences, advantages? 
I haven't used Selenium python binding directly much in real world.
I have no major complaints with Selenium2Library. I am content with it - but would like to have information to justify my choice (of using Selenium2Library wrapper instead of the Selenium python binding).


Kevin O.

unread,
Dec 12, 2012, 10:26:27 PM12/12/12
to robotframe...@googlegroups.com
I follow the page object using resource files almost exclusively, but here are some thoughts:
automatic screenshots if your page objects subclass Selenium2Library.keywords.keywordgroup.KeywordGroup
excellent logging
string-based locators improve readability -  wd.find_element_by_name("login").click() vs. s2l.click("name=login")
if you are using ElementFinder to fetch web elements it can be similarly easier to write/read
easier to handle multiple browsers

Using S2L that way is not common so I doubt anyone has documented the differences much. I only implement keywords in Python when the implementation is complex (diverges on browser type, action chains, etc.).

Kevin

Tatu aalto

unread,
Dec 13, 2012, 1:36:25 AM12/13/12
to korm...@gmail.com, robotframe...@googlegroups.com

Ugh

I do not have comparison list. But I think that choosing a tool depends where it is used.

Example: If one plans to use the webdriver among people that can code (to do integration or unit test), then I can understand why one is second guessing the use of the selenium2library.

But if the one aims for readability. Example to define test cases among other stakeholders (testers, project managers and so on) of the project. Then reading of the code comes a burden and people are not willing to do it. Then the selenium2library could play major part and provide readability.

Also it might be that selenium2library might not contain all you need, but I would not worry that too much. If tester like me can extend the selenium2library, with small effort, it tells for me that code base is clear and easy to read.

-Tatu

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/robotframework-users/-/DcuPaBQfPM4J.
To post to this group, send email to robotframe...@googlegroups.com.
To unsubscribe from this group, send email to robotframework-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/robotframework-users?hl=en.

Mohan R

unread,
Dec 13, 2012, 2:44:38 PM12/13/12
to robotframe...@googlegroups.com, korm...@gmail.com
Thanks for your answers Tatu and Kevin.

Kevin, excellent points. I agree with then.
Using resource files to implement page objects - that is interesting.
And sub-classing KeywordGroup - that is interesting too.

But regarding sub-classing KeywordGroup, I have a question.
I already have a "Register Keyword To Run On Failure      Capture screen and html" in my init.txt, which registers a function, which in turn calls capture_page_screenshot().

Will sub-classing KeywordGroup provide the same functionality? Or does it provide more?

Thanks,
To unsubscribe from this group, send email to robotframework-users+unsub...@googlegroups.com.

Kevin O.

unread,
Dec 13, 2012, 3:28:43 PM12/13/12
to robotframe...@googlegroups.com, korm...@gmail.com
It provides the same functionality. Without it, you do not get the screenshot & page source if it fails in your code, for example while you are working directly with a WebElement (unless you write your own decorator).
You have to have a method in your class or superclass like this for it to work:
def _run_on_failure(self):
        selenium2library()._run_on_failure()
If it fails when you are calling an S2L keyword from your code, your registered keyword is called twice :(
Its nice to have a single point of configuration.

Pekka Klärck

unread,
Dec 18, 2012, 4:58:33 PM12/18/12
to mohanr...@gmail.com, robotframe...@googlegroups.com, korm...@gmail.com
2012/12/13 Mohan R <mohanr...@gmail.com>:
> Thanks for your answers Tatu and Kevin.
>
> Kevin, excellent points. I agree with then.

Very good points both by Kevin and Tatu. Thanks for starting such an
interesting discussion.

> Using resource files to implement page objects - that is interesting.

Here's a good blog post explaining one way to do that:
http://www.beer30.org/?p=54

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

Mohan Raj Rajamanickam

unread,
Dec 18, 2012, 5:18:11 PM12/18/12
to Pekka Klärck, robotframe...@googlegroups.com, korm...@gmail.com
Thanks Kevin. I can now rest the nagging feeling of not knowing 'exactly' why I am using Sel2Lib.
Thanks Pekka for the blog link. I had gone through it when I was in the process of choosing RF.

I have one more question on the topic of page objects.
How do you accommodate the dynamic nature of the pages in page objects.
E.g. a number of pages that I test have a couple of states (think e.g. tabs that switch to different but related views / functionalities) accessed under the same URL.
I have found that my page object methods become quite messy in trying to handle this complexity.
I have been thinking about sub-classing - one class for each view / state derived from a common class - to manage this complexity.
How do you handle this? Any ideas?
How would you implement this using resource files?

ps: I posted a similar question in Selenium user group. But interested to know how people using RF Sel2Lib handle this.
--
Mohan Raj

Kevin Ormbrek

unread,
Dec 19, 2012, 10:57:41 PM12/19/12
to Mohan Raj Rajamanickam, Pekka Klärck, robotframe...@googlegroups.com
I have keywords for a tab based multi-order page where only the current order is displayed. That sounds simpler than what you are talking about.
I think it is OK for a keyword to have pre-conditions or even take state data as arguments (should be well documented). Consider putting some of the state management in the high-level keywords that are unique to a test or a few tests if it is easier than trying to manage state in a page object.
After trying different ways you can get a feel for what is the right fit for you and your business/department's needs and wishes. I am still working on that :)

Go To Refund
    ${index}    Get Selected Order
    Simulate    id=refund${index}    click    # Selenium bugs prevent normal click

Get Selected Order
    Wait Until Element Visible    xpath=id('tabs')/button[@class='selected']
    ${id}    Get Element Attribute    xpath=id('tabs')/button[@class='selected']@id
    Should Start With    ${id}    item    An order is not selected.
    ${index}    Set Variable    ${id[4:]}
    [Return]    ${index}

Kevin

elizaleong

unread,
Apr 17, 2013, 2:46:35 PM4/17/13
to robotframe...@googlegroups.com

I'm testing the feasibility of the approaches suggested here.  There are 2 problems I ran into as I tried to create a small working example, hoping that I can get some pointers from people who've succeeded.

1. after you create your page object (using python packages... as described in a 2-year old post from Adam Goucher on robotframework and page objects), where do you instantiate your class? 

My non-working code (wikipage.py)
import Selenium2Library.keywords

class WikiPageKeywords(KeywordGroup) :   
    def enter_term(self, text):
         input_text(self, searchInput, text)
    def submit_term(self):
         click_button(self, name=go)

2. in order to subclass from KeywordGroup, what do you need to import?  I'm getting NameError: name 'KeywordGroup' is not defined. I imported Selenium2Library.keyword in my python file. What should PYTHONPATH point to? 

My small robot test case has this:

| ** Settings ** |
| Library | Selenium2Library | run_on_failure=None |
| Resource | ${CURDIR}/../modules/common_resource.txt  |  #this imports the page object via:   Library wikipage
| Test Setup | Open Browser  |   http://wikipedia.org   |  firefox |
| Test Teardown | Close Browser  |

|  *** Testcases ***  |
| Test 1 |
| | Enter Term  |  robot framework |  #how do you instantiate the wikipedia class object so that you can call the method enter_term in it?
| | Submit Term |

David

unread,
Aug 4, 2014, 1:08:16 AM8/4/14
to robotframe...@googlegroups.com, mohanr...@gmail.com, korm...@gmail.com
I didn't quite like the exact implementation of page objects as resource files in the blog post Pekka mentions, so here's an alternate resource file implementation example on how I'd do it with some tweaks.


And I am curious as to how Kevin implements his page objects as resource files as well.
Reply all
Reply to author
Forward
0 new messages