Factory - pattern for Selenium webdriver

I'm currently trying to write an automated test suite using Selenium and Proboscis. I am trying to distract webdriver and implement it using factory template. This also creates the Page_object class, which takes a webdriver as an argument when 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 to do this? Or are there any better solutions? If you find something stupid, then please point out and ignore my negligence, as I am new to Python and Selenium.

+7
source share
1 answer

You implement the page object correctly because you do it the way most people do.

I made the page objects a little differently - without requiring the webdriver to create them. Because I often come across several pages with different body contents, but with the same header and footer sections. So instead of duplicating the locators and header / footer methods in each page object, I have a separate obj page only for the header and only for the footer. But then, using 1 webdriver to create multiple page objects to validate a single page seemed to break the paradigm. Thus, the objects of my page are just a collection of locators and methods, and not necessarily webdriver.

I understand that you did not specify headers and footers ... I think the reason why most people build their page objects around the webdriver is to create a paradigm that involves only 1 page object per page. In my case, this would lead to duplication of code across pages. Something to consider. Hope this helps!

+2
source

All Articles