Setting up simple python webdriver page object framework

2,004 views
Skip to first unread message

Barry

unread,
May 1, 2012, 1:23:15 AM5/1/12
to Selenium Users
Hi,
I am trying to setup framework using page object pattern approach
with Webdriver/Python. I currently have a suite of tests using a flat
model approach.

I have been looking at the examples used here:

https://github.com/davehunt/qmo-tests

If i have a setup with

*page.py
*base_page.py
*home_page.py
*test.py

How/Where do i initialize Webdriver - on page.py?

I get an error when i run test.py:

self = <home_page.HomePage object at 0x102e71d90>

def go_to_home_page(self):

> driver = self.driver


E AttributeError: 'HomePage' object has no attribute 'driver'
here is test.py:

#!/usr/bin/python

import home_page

class TestSignIn:

def test_user_can_sign_in(self):
home_pg = home_page.HomePage()
home_pg.go_to_home_page()

*************************************************
home_page.py:

#!/usr/bin/env python

from selenium.webdriver.common.by import By

import base_page

class HomePage(base_page.BasePage):

_page_title = ""
_sign_in_locator = (By.LINK_TEXT, "Sign In")

def go_to_home_page(self):
driver = self.driver
self.driver.get("%s/" % self.base_url)
'''self.is_the_current_page'''

def click_sign_in(self):
self.selenium.find_element(*self._sign_in_locator).click()

*************************************************************************

page.py:

#!/usr/bin/python

from selenium import webdriver

import time
import base64

class Page(object):

def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://xxx.com/"
self.verificationErrors = []

******************************************************************

base-page.py

#!/usr/bin/python

from selenium.webdriver.common.by import By

from page import Page

class BasePage(Page):

_page_title_locator = (By.CSS_SELECTOR, "#page-title")

@property
def page_title(self):
return
self.selenium.find_element(*self._page_title_locator).text
~

Barry

unread,
May 9, 2012, 6:51:02 AM5/9/12
to Selenium Users
anyone?

Greg

unread,
Jun 27, 2012, 5:53:29 PM6/27/12
to seleniu...@googlegroups.com
Barry did you ever end up figuring this problem out? I am on it atm myself. My current thinking is to just create a setup/tear down method and then call it for each test. This is computationally intensive but I don't see another way.

amadain

unread,
Aug 13, 2012, 9:52:19 AM8/13/12
to seleniu...@googlegroups.com
Could you share your code with me please

A.

On Saturday, July 7, 2012 7:58:28 AM UTC+1, Ali-Akber Saifee wrote:
Greg/Barry: its been a while since I've used selenium - but very recently ran into some extremely verbose test code which was screaming 'WE NEED PAGE OBJECTS IN PYTHON' at me. 

I've managed to hack out something that would allow us my team to write what I like to call '0 bullshit pageobjects' and '0 bullshit tests' with selenium and they end up looking something like http://pastebin.com/0dav5Jac. Theres a little bit of plumbing going on behind the scenes with the PageObject, PageElement(s) classes - but in a nutshell the core of the design is to use a PageElement as a very light proxy to a WebElement - simply allowing test authors to access PageObject properties directly. The "PageElements" class simply  wraps up a list of WebElements which are queried by the find_elements family of methods. PageElement objects are also pretentious enough to automatically return a selenium.webdriver.support.ui.Select obeject instead of a WebElement object if the element it encounters is indeed a select tag. Lastly, Locators are also just a proxy to the selenium.webdriver.common.by.By enumeration. The main thing here is that you dont have to copy paste the same old find_element_by / Wait.until type stuff in either your test code or your page object declarations.. 

Now I only googled around for a couple of minutes to see if something was already out there - before I decided to roll my own solution - but I'm more than happy to share it. My personal opinion gravitates towards having PageObjects available from the python selenium bindings themselves as this would be the clearest way for new test authors to not go down the obvious path of bundling page interactions/definitions with test code. Having said that - if its quicker, I can bundle this up into a simple python package on pypi as well.

Let me know.  

best,
./A

Ali-Akber Saifee

unread,
Jan 8, 2013, 7:20:01 AM1/8/13
to seleniu...@googlegroups.com
Hi, 
I've opened up the page object / utilities code at :
do let me know if this works, or if theres anything you'd be interested in seeing in that library.


best,
./A
Reply all
Reply to author
Forward
0 new messages