python - 如何在不同的浏览器中使用 LiveServerTestCase 运行 selenium 测试?

标签 python django testing selenium

需要连续在不同的浏览器中运行测试(即首先在 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/

相关文章:

django - 在/admin/配置不当

Angular 异步检测变化

java - Junit - 您应该在课前还是课前创建数据库连接?

.NET 的 Python "unable to find assembly"错误

python - Django 反向正则表达式匹配

python - 您可以将静态文件夹放在项目根目录中,而不是应用程序根目录中吗?

python - ListView 中的 Django 表单,表单 POST 时出错

c# - 无法使用 xunit 测试 mvc.controller 检查 returnType 是否为 HttpNotFoundResult

Python 将奇怪的 Unicode 写入 CSV

python - 如何在 Emacs 中查找 Python 方法的封闭类