I am willing to test my Django application using Selenium. From what I read, Django already cover the testing part and allow you to write your own tests.
Willing to use this with Selenium, here is my <application>/test.py
:
from some.path.to.my.utilitary.module import TestTools
class FormTestCase(TestCase):
def setUp(self):
self.webui = TestTools()
def test_advanced_settings(self):
self.webui.go_to('home')
self.webui.click('id', 'button-advanced-settings')
self.webui.click('id', 'id_setting_0')
self.webui.click('id', 'id_setting_1')
self.webui.click('id', 'id_setting_2')
self.webui.click('id', 'id_setting_3', submit=True)
def test_zone_selector(self):
self.webui.go_to('home')
self.webui.click('id', 'button-zone-selector')
I've written a Python class in which I implemented the Selenium logic (TestTools
), so I can focus on writing test code in my Django applications:
class TestTools():
def __init__(self):
self.driver = webdriver.Firefox(...)
...
# Those methods use self.driver to do things
def click(...):
...
def go_to(...):
...
def quit(...):
...
I noticed I couldn't override the __init__
method in the TestCase
child, so I put the self.webui = TestTools()
in the setUp
method. However, it is called twice (for each test method I guess), and thus create 2 webdrivers.
What I want to do is running ./manage test
, opening only one browser and running all my tests upon it. Where should the webdriver initialization live ?
Thanks,
Here's my insight.
I prefer to create a custom TestCase
based on LiveServerTestCase:
class SeleniumTestCase(LiveServerTestCase):
"""
A base test case for selenium, providing different helper methods.
"""
def setUp(self):
self.driver = WebDriver()
def tearDown(self):
self.driver.quit()
def open(self, url):
self.driver.get("%s%s" % (self.live_server_url, url))
Then, all my test cases are derived from this SeleniumTestCase
.
Hope that helps.