Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions casper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from .helpers import BaseCasperJs

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done as a helper so the user can just

from casper import BaseCasperJs

instead of

from casper.helpers import BaseCasperJs

32 changes: 32 additions & 0 deletions casper/helpers.py
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so that it can be overriden

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')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user can override the casper-tests folder-name if they look here ;)


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')),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is too long - 80 columns should be the max. I need to set up flake8 for this repository.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not worked on this lib since; 2014. Is it still a decent pull?

'STATIC_PATH': kwargs.get('STATIC_PATH', settings.STATIC_ROOT),
})

return self.casper(test_path, **kwargs)