Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory-pattern for Selenium webdriver

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.

like image 589
Rakesh Avatar asked Jul 10 '12 18:07

Rakesh


1 Answers

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!

like image 130
Dingredient Avatar answered Sep 30 '22 19:09

Dingredient