diff --git a/README.md b/README.md index ef3fec7..71fc449 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/test_my_page.py + # 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') + 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