I’m using it to generate d3 graphs for testings whereby images of the webpage as saved and compared to expected images.
from subprocess import Popen, PIPE
from django.test import LiveServerTestCase
# Tests and fixtures go here as well as login via self.client (as usual)
def phantom(self, filename, **kwargs):
"""Run a phantomjs script and return True if it was successful"""
pre_args = 
# These cookies require the target js to support them.
cn = settings.SESSION_COOKIE_NAME
if cn in self.client.cookies:
kwargs['cookie-' + cn] = self.client.cookies[cn].value
args = ['--%s=%s' % item for item in kwargs.items()]
cmd = [self.script] + pre_args + [self.get_js_file(filename)] + args
process = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = process.communicate()
if process.returncode != 0:
raise IOError("Process did not complete: %d" % process.returncode)
The above code should be well formatted, but wordpress (or this version of it) doesn’t want to use the right tags for code. :-/