需要连续在不同的浏览器中运行测试(即首先在 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()
最佳答案
为此,我使用了简单的装饰器,它通过指定的网络驱动程序运行测试:
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):
...
关于python - 如何在不同的浏览器中使用 LiveServerTestCase 运行 selenium 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821281/