How to use selenium test against different browsers using LiveServerTestCase

928 views
Skip to first unread message

Tianyi Wang

unread,
Sep 26, 2013, 12:46:43 PM9/26/13
to django...@googlegroups.com
Hi guys,


In the example, the test only test against Firefox. How can I test against different browsers without duplicate the example code?

Thanks

Tianyi

Rafael Durán Castañeda

unread,
Sep 26, 2013, 1:18:13 PM9/26/13
to django...@googlegroups.com
Hi,

I don´t know any out-of-the-box solution for this, but sure you can write your own test runner/suite, so any selenium test is run by multiple browsers (maybe just using settings to select the browser). However, if you are really concerned about testing on multiple browsers, multiples version for each one, multiple OS,… you might look at any of the available solutions for cross browser testing as a service.

HTH

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Evan Leis

unread,
Sep 26, 2013, 2:55:46 PM9/26/13
to django...@googlegroups.com
Alright, tested tried and True:

The following code provides the ability to decorate tests so that a whole list of drivers is used... see the docs for examples...

import functools


def test_drivers(pool_name='drivers', target_attr='selenium'):
    """
    Run tests with `target_attr` set to each instance in the `WebDriverPool`
    named `pool_name`.

    For example, in you setUpClass method of your LiveServerTestCase:

        # Importing the necessaries:
        from selenium import webdriver

        ### In your TestCase:

        # Be sure to add a place holder attribute for the driver variable
        selenium = None

        # Set up drivers
        @classmethod
        def setUpClass(cls):
            cls.drivers = WebDriverList(
                webdriver.Chrome(),
                webdriver.Firefox(),
                webdriver.Opera(),
                webdriver.PhantomJS,
            )
            super(MySeleniumTests, cls).setUpClass()

        # Tear down drivers
        @classmethod
        def tearDownClass(cls):
            cls.drivers.quit()
            super(MySeleniumTests, cls).tearDownClass()

        # Use drivers
        @test_drivers()
        def test_login(self):
            self.selenium.get('%s%s' % (self.live_server_url, '/'))
            self.assertEquals(self.selenium.title, 'Awesome Site')

    This will run `test_login` with each of the specified drivers as the
    attribute named "selenium"

    """
    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, pool_name)
            for web_driver in web_driver_pool:
                setattr(test_case, target_attr, web_driver)
                test_func(test_case, *args, **kwargs)
        return decorated
    return wrapped

class WebDriverList(list):
    """
    A sequence that has a `.quit` method that will run on each item in the list.
    Used to easily "quit" a list of WebDrivers.
    """

    def __init__(self, *drivers):
        super(WebDriverList, self).__init__(drivers)

    def quit(self):
        for driver in self:
            driver.quit()

Arnold Krille

unread,
Sep 26, 2013, 3:32:33 PM9/26/13
to django...@googlegroups.com
I would propose this:
In your classes __init__ (or in the init of your mixin specially for
this) check for an environment-variable or a django-setting to tell you
the browser to use. With a fallback if nothing is defined.

Then when you run the tests locally, you can decide which browser to
use. And when run in jenkins, the browser is a matrix-variable giving
you functional tests with all the defined browsers.

Have fun,

Arnold
signature.asc

Tianyi Wang

unread,
Sep 27, 2013, 4:46:06 AM9/27/13
to django...@googlegroups.com
Thanks for all your answers. 
Reply all
Reply to author
Forward
0 new messages