Skip to content

Instantly share code, notes, and snippets.

@fishi0x01
Last active March 21, 2020 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fishi0x01/c879b5d110b32ea18282 to your computer and use it in GitHub Desktop.
Save fishi0x01/c879b5d110b32ea18282 to your computer and use it in GitHub Desktop.
Functional headless UI testing in Django with Selenium. Code for blog post https://fishi.devtail.io/weblog/2015/03/02/functional-headless-ui-testing-django-selenium/
<!-- Simple webpage for Django UI example test -->
<html>
<head>
</head>
<body>
<p id="test">Hello World!</p>
</body>
</html>
"""
A simple functional headless UI test with pyvirtualdisplay and selenium
"""
from django.test import LiveServerTestCase
from django.utils.unittest import skipIf
from django.conf import settings
from pyvirtualdisplay import Display
from selenium import webdriver
SKIP_TESTS = True
if hasattr(settings, 'SKIP_FUNCTIONAL_TESTS'):
SKIP_TESTS = settings.SKIP_FUNCTIONAL_TESTS
SKIP_TEXT = 'Functional tests are disabled'
class ExampleTestCase(LiveServerTestCase):
def setUp(self):
# start display
self.vdisplay = Display(visible=0, size=(1024, 768))
self.vdisplay.start()
# start browser
self.selenium = webdriver.Firefox()
self.selenium.maximize_window()
super(ExampleTestCase, self).setUp()
def tearDown(self):
# stop browser
self.selenium.quit()
super(ExampleTestCase, self).tearDown()
# stop display
self.vdisplay.stop()
# check if this test should be skipped
@skipIf(SKIP_TESTS, SKIP_TEXT)
def test_example(self):
# run tests
self.selenium.get(
'%s%s' % (self.live_server_url, '/test/example/')
)
test = self.selenium.find_element_by_id('test')
self.assertEqual(test.text,"Hello World!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment