From 3a33afc0ae90bbbad3e0c20dd90a28dfbd49b825 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Wed, 16 Apr 2014 12:18:27 +0200 Subject: [PATCH 1/3] imported into base module --- README.md | 32 ++++++++++++++++++++++++++++++++ casper/__init__.py | 2 ++ casper/helpers.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 casper/helpers.py diff --git a/README.md b/README.md index ef3fec7..82fec42 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,38 @@ Then access these keyword arguments from the JS file: } ); + +## Using the helper + +``` +from casper.helpers import BaseCasperJs + + +class MyProjectPageTestCase(BaseCasperJs): + def test_my_page_js(self): + # login as a user - use use djangos LiveServerTestCase in casper so this + # is available + self.client.login(username=user.username, password=self.password) + + # the js_file in this example is located in "casper-tests" which is a + # folder the same directory as this test file + # + # if your path was: + # project/tests/file.py + # then the casper-tests folder is located at + # project/tests/casper-tests/url_to_test.js + # + url = reverse('project:url_to_test') + casper_result = self.load_casper_file(js_file='url_to_test.js', + test_label='Test the Project page', + url=url) + + # evaluate the result + self.assertTrue(casper_result) + +``` + + ## Bypassing log-in procedure When testing parts of the website for which the client needs to be logged in, diff --git a/casper/__init__.py b/casper/__init__.py index e69de29..ccad8f3 100644 --- a/casper/__init__.py +++ b/casper/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from .helpers import BaseCasperJs \ No newline at end of file diff --git a/casper/helpers.py b/casper/helpers.py new file mode 100644 index 0000000..41c1024 --- /dev/null +++ b/casper/helpers.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from django.conf import settings +from .tests import CasperTestCase + +import sys +import os.path + + +class BaseCasperJs(CasperTestCase): + """ + Base Class with helper methods to load casper tests + """ + @property + def test_path(self): + return os.path.dirname(sys.modules[self.__module__].__file__) + + def load_casper_file(self, js_file, **kwargs): + casper_test_folder_path = kwargs.get('casper_test_folder', 'casper-tests') + + test_path = getattr(self, 'test_path', os.path.dirname(__file__)) + + test_path = os.path.join(test_path, + casper_test_folder_path, + js_file + ) + kwargs.update({ + 'timeout': 30000, + 'casper_helper_js_path': kwargs.get('casper_helper_js_path', os.path.join(settings.SITE_ROOT, 'casper/jslib/djangocasper.js')), + 'STATIC_PATH': kwargs.get('STATIC_PATH', settings.STATIC_ROOT), + }) + + return self.casper(test_path, **kwargs) \ No newline at end of file From 2236b3cd57c803445e88001750d898cadad2af17 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Wed, 16 Apr 2014 12:19:43 +0200 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 82fec42..c1c4dd5 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ class MyProjectPageTestCase(BaseCasperJs): # # if your path was: # project/tests/file.py - # then the casper-tests folder is located at + # then the casper-tests folder and js file should be created at # project/tests/casper-tests/url_to_test.js # url = reverse('project:url_to_test') From 6db019018d9a9e1b2ee7fc85c5d5ea5c56188550 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Wed, 16 Apr 2014 12:20:15 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1c4dd5..71fc449 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ class MyProjectPageTestCase(BaseCasperJs): # folder the same directory as this test file # # if your path was: - # project/tests/file.py + # project/tests/test_my_page.py # then the casper-tests folder and js file should be created at # project/tests/casper-tests/url_to_test.js #