目前,我正在尝试编写一个使用Selenium和鼻部的自动化测试套件。我正在尝试抽象webdriver并通过工厂模式来实现。这里还创建了Page_object类,它在创建对象时将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的新手。
发布于 2014-10-09 07:09:35
您正确地实现了页面对象,因为您正在以大多数人的方式来实现它。
我对页面对象的处理稍有不同--不需要webdriver来实例化它们。因为我经常遇到几个正文内容不同但页眉和页脚部分相同的页面。因此,我没有在每个页面对象中复制页眉/页脚定位器和方法,而是为页眉和页脚创建了一个单独的页面对象。但是,使用1个webdriver来实例化多个页面对象来测试单个页面,似乎违反了这个范例。因此,我的页面对象实际上只是定位器和方法的集合,而不一定是webdriver。
我知道你没有提到页眉和页脚...我猜大多数人之所以围绕webdriver构建他们的页面对象,是为了创建一种假设每个页面只有一个页面对象的范例。在我的例子中,这将导致页面对象之间的重复代码。一些需要考虑的事情。希望这能有所帮助!
发布于 2020-03-12 16:41:54
有一个Python库,它提供了页面工厂方法来实现selenium中的页面对象模型。
https://stackoverflow.com/questions/11420042
复制相似问题