-
-
Save tyndyll/dac557c1dc81dbacfb6402eb7267d113 to your computer and use it in GitHub Desktop.
Run Selenium tests against the django test server, from command line with no GUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run Selenium headless tests against Django test server | |
------------------------------------------------------ | |
Creates and destroys a test environment each time. | |
Requires Xvfb and IceWeasel to be installed. For Debian flavours: | |
sudo apt-get install xvfb iceweasel | |
For details, see: | |
http://pyuseful.wordpress.com/2012/11/08/running-cucumber-style-tests-in-django-using-behave/ | |
""" | |
import os | |
from os.path import abspath, join, split | |
import subprocess | |
import sys | |
import time | |
from behave.__main__ import main as behave_main | |
DJANGO_PROJECT_DIR = 'djangoproject' # Substitute your top level Django project folder name | |
if __name__ == '__main__': | |
project_dir = split(abspath(__file__))[0] | |
sys.path.insert(0, join(project_dir, DJANGO_PROJECT_DIR)) | |
def run(): | |
print('Starting django testserver') | |
django_process = subprocess.Popen( | |
['python', join(DJANGO_PROJECT_DIR, 'manage.py'), 'testserver', '--noinput'], | |
stdout=open('/dev/null', 'w'), | |
stderr=open('/dev/null', 'w')) | |
time.sleep(2) | |
print('Starting Xvfb') | |
xvfb_process = subprocess.Popen( | |
['Xvfb', ':99', '-ac', '-screen', '0', '1024x768x8']) | |
os.environ["DISPLAY"] = ":99" | |
try: | |
behave_main() # This calls sys.exit() under all circumstances | |
finally: | |
print('Stopping django testserver') | |
django_process.terminate() | |
print('Stopping Xvfb') | |
xvfb_process.terminate() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment