Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SeleniumWebDriver的工厂模式_Python_Selenium_Factory Pattern - Fatal编程技术网

Python SeleniumWebDriver的工厂模式

Python SeleniumWebDriver的工厂模式,python,selenium,factory-pattern,Python,Selenium,Factory Pattern,目前,我正在尝试使用Selenium和Proboscis编写一个自动化测试套件。我试图抽象webdriver并通过factory模式实现Page\u对象类,它在创建对象时将webdriver作为参数。下面是代码 import selenium.webdriver as webdriver from proboscis import TestProgram from proboscis import test from proboscis import be

目前,我正在尝试使用Selenium和Proboscis编写一个自动化测试套件。我试图抽象webdriver并通过factory模式实现<这里还创建了code>Page\u对象类,它在创建对象时将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新手

您正确地实现了页面对象,因为您是以大多数人的方式实现的

我对页面对象的处理略有不同——不需要webdriver来实例化它们。因为我经常会遇到几页不同的正文内容,但页眉和页脚部分是相同的。因此,我没有在每个页面对象中复制页眉/页脚定位器和方法,而是为页眉和页脚创建了一个单独的页面对象。但随后使用webdriver实例化多个页面对象来测试单个页面,似乎违反了这一范式。所以我的页面对象实际上只是定位器和方法的集合,而不一定是webdriver


我知道你没有提到页眉或页脚。。。我想大多数人围绕webdriver构建页面对象的原因是为了创建一个假设每页只有一个页面对象的范例。在我的例子中,这将导致页面对象之间的代码重复。需要考虑的事情。希望有帮助

有一个Python库,它提供了页面工厂方法来在selenium中实现页面对象模型-


这个问题没有引起太多关注,真是令人惊讶:(Pat Meeker,你有什么我想要的例子可以分享吗?顺便说一句,目前使用的是作为起点,结合
机器人框架
。正如你提到的,有一些常见的对象,不喜欢在每一页上重复。也没有太多的python经验来获得
固有性