2014-11-08 49 views
2

需要在不同的浏览器中连续运行测试(即先在Firefox中运行,然后在Chrome中进行相同的测试..)。解决这个问题的最好方法是什么?如何在不同的浏览器中使用LiveServerTestCase运行硒测试?

林试图把环路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() 

回答

3

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

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): 
     ... 
+0

来自Evan Lewis的更完整的[解决方案](https://groups.google.com/d/msg/django-users/Sckf9y2xIho/mwLTr8YyNDkJ)。 – 2014-11-08 20:42:10

+0

真棒,谢谢! – aphex 2014-11-08 21:56:58

1

以上来自@Alex Lisovoy的解决方案似乎取自第埃文刘易斯解决方案,我发现没有工作。

我能够使用nose_parameterized模块一次测试两个浏览器。看到我的回答/示例this other SO question

相关问题