【问题标题】:How to run selenium tests using LiveServerTestCase in different browsers?如何在不同的浏览器中使用 LiveServerTestCase 运行 selenium 测试?
【发布时间】:2015-01-05 10:15:41
【问题描述】:

需要在不同的浏览器中连续运行测试(即首先在 Firefox 中,接下来在 chrome 中进行相同的测试..)。解决这个问题的最佳方法是什么?

我试图在 setUpClass 中放置循环,但它并没有真正帮助:

class UITest(LiveServerTestCase):

    fixtures = ['initial_test_data.json']

    @classmethod
    def setUpClass(self):
        for browser in [webdriver.Firefox(), webdriver.PhantomJS(), webdriver.Chrome()]:
            self.selenium = browser
            super(UITest, self).setUpClass()

【问题讨论】:

    标签: python django testing selenium


    【解决方案1】:

    @Alex Lisovoy 的上述解决方案似乎取自 Evan Lewis 解决方案,我发现它不起作用。

    我能够使用nose_parameterized 模块同时测试两个浏览器。请参阅我的答案/示例this other SO question

    【讨论】:

      【解决方案2】:

      为此,我使用了简单的装饰器,它通过指定的网络驱动程序运行测试:

      import functools
      
      
      def run_through_drivers(driver_pool='drivers'):
          def wrapped(test_func):
              @functools.wraps(test_func)
              def decorated(test_case, *args, **kwargs):
                  test_class = test_case.__class__
                  web_driver_pool = getattr(test_class, driver_pool)
                  for web_driver in web_driver_pool:
                      setattr(test_case, 'selenium', web_driver)
                      test_func(test_case, *args, **kwargs)
              return decorated
          return wrapped
      

      使用方法:

      class UITest(LiveServerTestCase):
      
          fixtures = ['initial_test_data.json']
          selenium = None
      
          @classmethod
          def setUpClass(self):
              cls.drivers = WebDriverList(
                  webdriver.Chrome(),
                  webdriver.Firefox(),
                  webdriver.PhantomJS
              )
              super(UITest, cls).setUpClass()
      
          @classmethod
          def tearDownClass(cls):
              for driver in cls.drivers:
                  driver.quit()
              super(UITest, cls).tearDownClass()
      
          @run_through_drivers()
          def test_example(self):
              ...
      

      【讨论】:

      • 来自 Evan Lewis 的更完整的 solution
      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2014-12-27
      • 2012-08-03
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多