-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial forked from: https://github.com/Lullabot/drupal-boilerplate
- Loading branch information
1 parent
b873fac
commit 8998178
Showing
6 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
# FORKED FROM | ||
|
||
https://github.com/Lullabot/drupal-boilerplate | ||
|
||
# Automated testing | ||
|
||
This directory contains [CasperJS](http://casperjs.org) tests that ensure the | ||
stability of the site. | ||
|
||
## Installation | ||
|
||
### Requirements | ||
|
||
* Python 2.6 or greater. | ||
* PhantomJS 1.9.2. | ||
* CasperJS 1.1-beta3. | ||
|
||
### OSX - Homebrew | ||
Homebrew will install the phantomjs dependency automatically. | ||
|
||
```bash | ||
brew install casperjs --devel | ||
``` | ||
|
||
### Manual installation | ||
|
||
#### Python | ||
|
||
Python is already installed in most platforms. Check the pyhon version with the | ||
following command: | ||
|
||
```bash | ||
python --version | ||
``` | ||
|
||
If the above output is less than 2.6, go to https://www.python.org/download, | ||
find the link for your platform at the 2.7 section (even though CasperJS seems | ||
to work on Python 3 we have not tested it yet). | ||
|
||
You can find further installation tips at | ||
http://docs.casperjs.org/en/latest/installation.html#prerequisites | ||
|
||
#### CasperJS | ||
|
||
```bash | ||
cd /usr/share | ||
sudo git clone git://github.com/n1k0/casperjs.git | ||
cd casperjs | ||
git checkout 1.1-beta3 | ||
sudo ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs | ||
``` | ||
|
||
#### PhantomJS | ||
|
||
Download phantomjs 1.9.2 http://phantomjs.org/download.html to `/usr/share` | ||
|
||
```bash | ||
cd /usr/share/phantomjs-1.9.2-linux-x86_64/bin | ||
sudo ln -sf `pwd`/phantomjs /usr/local/bin/phantomjs | ||
``` | ||
|
||
### Verify your installation | ||
|
||
When running `casperjs`, the output should be: | ||
|
||
```bash | ||
casperjs | ||
CasperJS version 1.1.0-beta3 at /usr/share/casperjs, using phantomjs version 1.9.2 | ||
``` | ||
|
||
## Running tests | ||
Assuming your local environment is set up at http://drupal.local, all tests may | ||
be run with the following command: | ||
|
||
```bash | ||
cd path-to-drupal-root | ||
drush casperjs --url=http://drupal.local | ||
``` | ||
|
||
You can also run a specific test by giving it as an argument to the command: | ||
|
||
```bash | ||
drush casperjs --url=http://drupal.local | ||
``` | ||
|
||
*NOTE* `drush casperjs` is a wrapper for `casperjs` which sets some useful defaults | ||
when running tests for your Drupal site. Run `drush casperjs -h` for a list of all the | ||
available options and arguments. | ||
|
||
## Writing tests | ||
|
||
Tests are JavaScript files which are located at the `tests` directory. | ||
They can be organized either by which aspect of the site they test (the homepage, | ||
the contact form, the editorial workflow) or by mapping its name with a module | ||
so it tests the functionality added by that particular module. The choice is | ||
yours to take. | ||
|
||
In order to write a new test, copy any of the existing ones and use it as a | ||
template. Be sure to read through the includes such as `drush/commands/casperjs/includes/common.js` | ||
and drush/commands/casperjs/includes/session.js to find if there are any helper | ||
functions you should be aware of in addition to the ones provided by CasperJS. | ||
|
||
Some useful resources for writing tests are: | ||
* [Navigation steps](http://docs.casperjs.org/en/latest/faq.html#how-does-then-and-the-step-stack-work) | ||
let you wait for certain events such a page getting fully rendered before | ||
running assertions over it. | ||
* The [casper](http://docs.casperjs.org/en/latest/modules/casper.html) object has | ||
commands to interact with the browser such as opening a URL or filling | ||
out a form. | ||
* The [test](http://docs.casperjs.org/en/latest/modules/tester.html) | ||
object contains methods to run assertions over the current context. | ||
* `drush/commands/casperjs/includes/common.js` has a list of useful methods to | ||
navigate through a Drupal project. Rely on these methods as much as possible | ||
in order to simplify your testing code. | ||
* `drush/commands/casperjs/includes/session.js` implements session management so | ||
you can swap from an anonymous user to an authentcated user in just one step. | ||
|
||
## Cookies | ||
Some tests like will need an authenticated user to work with order to test the | ||
backend. The `drush casperjs` command creates a temporary cookies file at the | ||
temorary directory while running tests to store cookie information and deletes it | ||
the next time tests are run. | ||
|
||
If you need session management in your test call `casper.boilerRun()` to run | ||
your test so that all opened sessions are closed for the next test run. | ||
|
||
## Tips | ||
### Taking screenshots | ||
You can take a screenshot and capture markup with `casper.boilerCapture('my-unique-string');`. | ||
This is perfectly fine to commit to your test suite, as this will do nothing | ||
unless you specifically configured your environment to actually save these files. | ||
There are three ways to do this: | ||
|
||
The first is to turn on capturing by exporting | ||
an environment variable: | ||
|
||
```bash | ||
export BOILER_TEST_CAPTURE=true | ||
``` | ||
|
||
The second is to run the `drush casperjs` command with debug mode turned on. | ||
See the *Debugging* section for more details on that. | ||
|
||
The third method is to pass `true` as the second argument to `casper.boilerCapture()`. | ||
This will force capturing. Do *not* commit code in this state, as this means | ||
capturing will occur on all environments. | ||
|
||
Once enabled, this command will save a screenshot to `./screenshots/my-unique-string-12345678.jpg` | ||
and the HTML markup to `./pages/my-unique-string-1234578.html`, where `12345678` | ||
is the current timestamp. You may need to create those directories yourself and | ||
give proper write permissions. | ||
|
||
Alternatively, you can use `casper.captureSelector('filename', 'div.some-class');` | ||
to take a screenshot of a given selector. In this case, specify a full filename | ||
to save the capture to, such as `screenshots/my-selector-12345678.jpg`. You can | ||
find more examples about capturing [here](http://docs.casperjs.org/en/latest/modules/casper.html#capture). | ||
|
||
### Debugging | ||
|
||
If you would like to see a more verbose output of the test suites, you can run | ||
the `drush casperjs` command with the `-d` flag: | ||
|
||
```bash | ||
drush casperjs -d | ||
``` | ||
|
||
This will execute CasperJS with `log-level` set to `debug`. In your scripts, you | ||
can log to this debug mode by using `casper.log('my message', 'debug');`. | ||
|
||
In addition, other helpful debugging tools are [`casper.debugPage()`](http://casperjs.readthedocs.org/en/latest/modules/casper.html#debugpage) | ||
and [`casper.debugHTML()`](http://casperjs.readthedocs.org/en/latest/modules/casper.html#debughtml). | ||
|
||
### Evaluating code | ||
|
||
The [casper.evaluate()](http://docs.casperjs.org/en/latest/modules/casper.html#evaluate) | ||
method (and its alternatives such as `casper.evaluateOrDie()`, `casper.thenEvaluate()` or | ||
`test.assertEvaluate()`) are highly powerful since they will run JavaScript code | ||
on the page just as if you were debugging with the browser's JavaScript console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* | ||
* CasperJS Drush integration. | ||
*/ | ||
|
||
/** | ||
* Implements hook_drush_command(). | ||
*/ | ||
function casperjs_drush_command() { | ||
$items['casperjs'] = array( | ||
'description' => 'Wrapper for running CasperJS tests. Accepts extra options for casperjs command.', | ||
'callback' => 'drush_casperjs', | ||
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE, | ||
'arguments' => array( | ||
'tests' => 'A comma separated list of tests to run. If not provided, all tests at the tests directory will be run.', | ||
), | ||
'options' => array( | ||
'test-root' => 'Overrides the default location of tests.', | ||
'url' => 'The base URL to use for test requests. Defaults to the URL set by Drush or the current Drush alias.', | ||
'includes' => 'Comma-separated list of files to include before each test file execution.', | ||
'cookies-file' => 'Sets the file name to store the persistent cookies. If not provided a random file in the system temporary directory will be used.', | ||
), | ||
'examples' => array( | ||
'drush casperjs --url=http://boiler.local' => 'Runs all tests located at the tests directory against http://boiler.local', | ||
'drush casperjs --url=http://boiler.local homepage.js' => 'Runs homepage.js test against http://boiler.local.', | ||
'export BOILER_TEST_CAPTURE=true && drush -v casperjs --url=http://boiler.local --log-level=debug' => 'Runs all tests against http://boiler.local with extra verbose logging and taking screenshots on failed assertions.', | ||
), | ||
'allow-additional-options' => TRUE, | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Implements drush_COMMANDFILE(). | ||
*/ | ||
function drush_casperjs($tests = NULL) { | ||
|
||
$command = 'casperjs test --verbose'; | ||
|
||
if (!drush_get_option('cookies-file')) { | ||
$cookie_file = drush_tempnam('casper-cookie-'); | ||
$command .= ' --cookies-file=' . drush_escapeshellarg($cookie_file); | ||
} | ||
|
||
if (!drush_get_option('url')) { | ||
$uri = drush_get_context('DRUSH_SELECTED_URI'); | ||
$command .= ' --url=' . drush_escapeshellarg($uri); | ||
} | ||
|
||
// Add include files. | ||
$command .= ' --includes=' . drush_escapeshellarg(dirname(__FILE__) . '/includes/common.js,' . dirname(__FILE__) . '/includes/session.js'); | ||
$command .= ' --pre=' . drush_escapeshellarg(dirname(__FILE__) . '/includes/pre-test.js'); | ||
$command .= ' --post=' . drush_escapeshellarg(dirname(__FILE__) . '/includes/post-test.js'); | ||
if ($includes = drush_get_option('includes')) { | ||
$command .= ',' . $includes; | ||
} | ||
|
||
// Set the root where tests are. Defaults to the tests directory | ||
if (!$root = drush_get_option('test-root')) { | ||
$root = drush_get_context('DRUSH_DRUPAL_ROOT') . '/../tests'; | ||
} | ||
|
||
// If a list of tests to run were passed, append them to the command. | ||
// Otherwise, just set the root where tests are located so CasperJS | ||
// will run all of them. | ||
$tests_to_run = $root; | ||
if ($tests) { | ||
$tests_to_run = ''; | ||
foreach (explode(',', $tests) as $test_file) { | ||
$tests_to_run .= ' ' . drush_escapeshellarg($root . '/' . $test_file); | ||
} | ||
} | ||
$command .= ' ' . $tests_to_run; | ||
|
||
// Append additional CasperJS options to the command. | ||
$args = array(); | ||
foreach (drush_get_original_cli_args_and_options() as $arg) { | ||
// Don't pass some options through. | ||
if (strpos($arg, '--test-root') !== FALSE | ||
|| strpos($arg, '--simulate') !== FALSE | ||
|| strpos($arg, '--includes') !== FALSE | ||
|| strpos($arg, '-') !== 0) { | ||
continue; | ||
} | ||
$args[] = $arg; | ||
} | ||
if (!empty($args)) { | ||
$command .= ' ' . implode(' ', $args); | ||
} | ||
|
||
echo $command . "\n"; | ||
$result = drush_shell_proc_open($command); | ||
if ($result !== 0) { | ||
return drush_set_error('CASPERJS_TEST_FAILED', dt('Tests failed.')); | ||
} | ||
else { | ||
drush_log(dt('Tests succeeded.'), 'success'); | ||
return TRUE; | ||
} | ||
} |
Oops, something went wrong.