Rak*_*esh 9 python selenium factory-pattern
目前我正在尝试使用Selenium和Proboscis编写自动化测试套件.我试图抽象webdriver并通过工厂模式实现.Page_object这里也创建了class,它在创建对象时将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()
Run Code Online (Sandbox Code Playgroud)
这是正确的做法吗?或者有更好的解决方案吗?如果你发现一些愚蠢的东西,那么请指出并忽略我的疏忽,因为我是Python和Selenium的新手.
您正在正确地实现页面对象,因为您正在按照大多数人的方式进行操作。
我对页面对象的处理方式略有不同 - 不需要网络驱动程序来实例化它们。因为我经常遇到几个正文内容不同但页眉和页脚部分相同的页面。因此,我没有在每个页面对象中复制页眉/页脚定位器和方法,而是为页眉和页脚提供了一个单独的页面对象。但随后使用 1 个 webdriver 实例化多个页面对象来测试单个页面,似乎违反了范例。所以我的页面对象实际上只是定位器和方法的集合,不一定是网络驱动程序。
我意识到你没有提到页眉或页脚...我猜大多数人围绕网络驱动程序构建页面对象的原因是创建一个假设每页只有 1 个页面对象的范例。就我而言,这会导致页面对象之间出现重复的代码。需要考虑的事情。希望有帮助!
| 归档时间: |
|
| 查看次数: |
2352 次 |
| 最近记录: |