目前我正在嘗試使用Selenium和Proboscis編寫一個自動化測試套件。我試圖抽象webdriver並通過工廠模式實現。 Page_object
類也在這裏創建,它在創建對象時將webdriver作爲參數。以下是代碼。Selenium webdriver的工廠模式
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()
這是做這件事的正確方法嗎?或者有更好的解決方案嗎? 如果你發現一些愚蠢的話,請指出並忽略我的疏忽,因爲我是Python和Selenium的新手。
真的很驚訝,這個問題沒有得到太多的關注:( – 2012-08-24 20:10:38