简体   繁体   中英

How to run selenium tests using LiveServerTestCase in different browsers?

Need run tests in different browsers successively (ie first in firefox, next same tests in chrome..). What is the best way to solve this problem?

Im trying to put loop in setUpClass, but it does not realy helped:

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()

For this purpose I use simple decorator which runs the tests through specified web drivers:

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

How to use:

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

The above solution from @Alex Lisovoy appears to be taken from the Evan Lewis solution, which I found didn't work.

I was able to test two browsers at once using the nose_parameterized module. See my answer/example this other SO question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM