Factory-pattern for Selenium webdriver
Asked Answered
S

2

9

Currently I am trying to write an automated test suite using Selenium and Proboscis. I am trying to abstract the webdriver and implementing through the factory pattern. Page_object class is also created here, which takes the webdriver as an argument while creating an object. Below is the code.

     import selenium.webdriver as webdriver
     from proboscis import TestProgram
     from proboscis import test
     from proboscis import before_class
     from proboscis import after_class    

     class WebdriverFactory:
        @staticmethod
        def getWebdriver(browserName):  
            if(browserName == 'firefox'):
             return webdriver.Firefox()
            elif(browserName == 'chrome'):
             return webdriver.Chrome()
            elif(browserName == 'ie'):
             return webdriver.Ie()        

            raise Exception("No such " + browserName + " browser exists")  

   class Page_Object:
    def __init__(self, driver):
      self.driver = driver

    def go_to_home(self):
        self.driver.get("http://google.com")
        return self
    def go_to_page(self,url):
        self.driver.get(url)
        return self
    def run_search(self, url, query):
        self.driver.get(url)
        self.driver.find_element_by_id(locators['search_box']).send_keys(query)
        self.driver.find_element_by_id(locators['search_button']).click()

    def tear_down(self):
        self.driver.close()   

   @test(groups=['selenium'])
   class Test_Scripts:

     @test(groups=['WebDemo'])
     def test_1(self):
        driver = WebdriverFactory.getWebdriver("firefox")
        pageObj = Page_Object(driver)
        pageObj.run_search("http://google.com",'apples')
        pageObj.tear_down()      
     def run_tests(self):
        TestProgram().run_and_exit()

   Test_Scripts().run_tests()   

Is this the right way of doing this? Or are there any better solutions possible? If you find something stupid then please point out and ignore my negligence since I am new to Python and Selenium.

Shower answered 10/7, 2012 at 18:58 Comment(1)
Really surprised that this question hasn't got much attention :(Sophy
A
4

You are implementing page object correctly, in that you are doing it the way most people do.

I have done page objects a little differently - not requiring a webdriver to instantiate them. Because I often run into several pages with different body contents, but identical header and footer sections. So rather than duplicating header/footer locators and methods in each page object, I have a separate page obj just for header, and just for footer. But then using 1 webdriver to instantiate multiple page objects to test a single page, seemed to violate the paradigm. So my page objects are really just a collection of locators and methods, and not necessarily a webdriver.

I realize you didn't mention headers or footers... I guess the reason why most people build their page objects around a webdriver is to create a paradigm which assumes only 1 page object per page. In my case, that would have resulted in duplicate code across page objects. Something to consider. Hope that helps!

Abducent answered 8/10, 2014 at 23:9 Comment(1)
Pat Meeker, do you have some example to share which I am exactly looking for? By the way, currently using this as starting point, combined with robot framework. As you mentioned, there are common objects and not like to repeat on each page. Also not much hands on python for getting inheretence to get it working.Diaphaneity
C
1

There is Python library which provides page factory approach to implement page object model in selenium -

selenium-page-factory

Cockscomb answered 12/3, 2020 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.