From 7eab3560e019d848f794b50b56d281166d21b5fd Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 14 Jul 2016 10:36:06 -0400 Subject: [PATCH 1/2] Improve API with a new assertCasper method --- casper/tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/casper/tests.py b/casper/tests.py index 27845c9..8c5be75 100644 --- a/casper/tests.py +++ b/casper/tests.py @@ -2,6 +2,7 @@ from subprocess import Popen, PIPE import os import sys +import inspect from django.contrib.staticfiles.handlers import StaticFilesHandler from django.contrib.staticfiles.views import serve @@ -25,6 +26,7 @@ class CasperTestCase(LiveServerTestCase): use_phantom_disk_cache = False load_images = False no_colors = True + casper_directory = None def __init__(self, *args, **kwargs): super(CasperTestCase, self).__init__(*args, **kwargs) @@ -78,3 +80,17 @@ def casper(self, test_filename, **kwargs): sys.stdout.write(out) sys.stderr.write(err) return p.returncode == 0 + + def get_casper_dir(self): + """Get the directory containing casper files""" + if self.casper_directory: + return self.casper_directory + root = os.path.dirname(inspect.getfile(type(self))) + auto = os.path.join(root, 'casper-tests') + return auto if os.path.isdir(auto) else root + + def assertCasper(self, casper_file, msg=None, **kwargs): + """Automate the casper API into a single assert""" + if not os.path.isfile(casper_file) and casper_directory: + casper_file = os.path.join(self.casper_directory, casper_file) + self.assertTrue(self.casper(casper_file, **kwargs), msg) From f76ee94f7c8fa527af0c1417ca199ae8ee21bb79 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 14 Jul 2016 10:39:53 -0400 Subject: [PATCH 2/2] Add assertCapser to docs --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index df81a0c..0027cc0 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,9 @@ Then, create the Python part of your tests: self.assertTrue(self.casper( os.path.join(os.path.dirname(__file__), 'casper-tests/test.js'))) + + # or just simply + self.assertCasper('test.js') Then, create the CasperJS part. Create a `casper-tests` directory somewhere (eg. in your `tests` directory). Copy `djangocasper.js` helper JS module @@ -120,6 +123,12 @@ in your Python tests: 'casper-tests/test.js')), user=fixture_username, pass=fixture_password) + + # Or tersely + self.assertCasper('test.js', + user=fixture_username, + pass=fixture_password) + Then access these keyword arguments from the JS file: