├── fixtures └── README.md ├── common.js ├── tests ├── homepage.js └── search.js ├── LICENSE ├── testrun └── README.md /fixtures/README.md: -------------------------------------------------------------------------------- 1 | This directory contains testing data that you can use on tests. For example, 2 | images to be used for upload form fields. 3 | -------------------------------------------------------------------------------- /common.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Helper methods for navigating through a site. 3 | * 4 | * This file is included automagically by the "test" executable. 5 | */ 6 | 7 | /** 8 | * Wrapper for http://docs.casperjs.org/en/latest/modules/casper.html#open 9 | * 10 | * Uses url argument from the command line in order to open a URL path. 11 | */ 12 | casper.openPath = function (path) { 13 | var cleanPath = path.replace(/^\//, ''); 14 | return casper.open(casper.cli.get('url') + '/' + cleanPath); 15 | }; 16 | 17 | /** 18 | * Wrapper for http://docs.casperjs.org/en/latest/modules/casper.html#thenopen 19 | * 20 | * Uses url argument from the command line in order to open a URL path and 21 | * define a navigation step. 22 | */ 23 | casper.thenOpenPath = function (path, thenCallback) { 24 | var cleanPath = path.replace(/^\//, ''); 25 | return casper.thenOpen(casper.cli.get('url') + '/' + cleanPath, thenCallback); 26 | }; 27 | -------------------------------------------------------------------------------- /tests/homepage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Homepage tests. 3 | */ 4 | 5 | casper.test.begin('Tests homepage structure', 7, function suite(test) { 6 | casper.start(); 7 | 8 | // Open the homepage. 9 | casper.thenOpenPath('/', function() { 10 | // Verify that the main menu links are present. 11 | test.assertExists('a.j-signin-label', '"Sign in" link is found.'); 12 | test.assertExists('a.j-register-label', '"Sign up" link is found.'); 13 | test.assertExists('li.main-nav__link--explore a', '"Explore" link is found.'); 14 | test.assertExists('li.main-nav__link--watch a', '"Watch" link is found.'); 15 | test.assertExists('li.main-nav__link--join-in a', '"Join In" link is found.'); 16 | test.assertExists('li.main-nav__link--speak-out a', '"Speak Out" link is found.'); 17 | // 10 articles should be listed. 18 | test.assertElementCount('article', 10, '10 articles are listed.'); 19 | }); 20 | 21 | casper.run(function() { 22 | test.done(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/search.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Search form tests. 3 | */ 4 | 5 | casper.test.begin('Tests search form submission and results', 2, function suite(test) { 6 | casper.start(); 7 | 8 | // Open the homepage. 9 | casper.thenOpenPath('/', function() { 10 | casper.waitFor(function check() { 11 | // Fill out the search form with 'health' and submit it. 12 | return this.evaluate(function() { 13 | jQuery('#site_search_text').val('health'); 14 | jQuery('#site_search_text').trigger('input'); 15 | return jQuery('div#site-search input[type="submit"]').trigger('click'); 16 | }); 17 | }, function then() { 18 | // Check current URL and search results. 19 | test.assertUrlMatch(/search\/health/, 'Current path is search/health'); 20 | test.assertExists('div.view-search-pane div.view-content div.views-row', 'Search results are listed under "Stories".'); 21 | }, function timeout() { 22 | this.echo("Search form has not been submitted.").exit(); 23 | }); 24 | }); 25 | 26 | casper.run(function() { 27 | test.done(); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Lullabot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /testrun: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Wrapper executable for casperjs which sets some useful defaults. 4 | 5 | usage() { 6 | echo "Usage: ./testrun [-u http://domain.to.test] [test to run located at tests folder] 7 | 8 | Examples: 9 | ./testrun >> Runs all tests at http://localhost. 10 | ./testrun homepage.js >> Runs homepage.js tests at http://localhost. 11 | ./testrun -u http://www.msnbc.com homepage.js >> Runs homepage.js test at http://www.msnbc.com." 12 | exit 1 13 | } 14 | 15 | # Set defaults. 16 | URL='http://localhost' 17 | 18 | # Parse options. 19 | while getopts hu: OPTION; do 20 | case $OPTION in 21 | h) 22 | usage 23 | ;; 24 | u) 25 | URL=$OPTARG 26 | ;; 27 | esac 28 | done 29 | 30 | # Remove the switches we parsed above from the arguments. 31 | shift `expr $OPTIND - 1` 32 | 33 | # Now, parse arguments. 34 | TEST=${1-.} 35 | 36 | # Remove stored cookies from previous test runs. 37 | rm -f cookies.txt 38 | 39 | # Run tests. 40 | echo casperjs --verbose --cookies-file=cookies.txt test --includes=common.js tests/$TEST --root=`pwd` --url=$URL 41 | casperjs --verbose --cookies-file=cookies.txt test --includes=common.js tests/$TEST --root=`pwd` --url=$URL 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **This piece of logic has been moved to https://www.drupal.org/project/casperjs** 2 | 3 | # Automated testing 4 | 5 | This directory contains [CasperJS](http://casperjs.org) tests that ensure the 6 | stability of the site. 7 | 8 | ## Installation 9 | 10 | Clone this repository anywhere in your project. Ideally, next to the docroot. 11 | The contents of this repository do not need to be available to a web browser. 12 | 13 | ### OSX - Homebrew 14 | Homebrew will install the phantomjs dependency automatically. 15 | ```bash 16 | brew install casperjs --devel 17 | ``` 18 | 19 | ### From Source 20 | ```bash 21 | cd /usr/share 22 | sudo git clone git://github.com/n1k0/casperjs.git 23 | cd casperjs 24 | sudo ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs 25 | # Download phantomjs http://phantomjs.org/download.html to /usr/share 26 | cd /usr/share/phantomjs-1.9.2-linux-x86_64/bin 27 | sudo ln -sf `pwd`/phantomjs /usr/local/bin/phantomjs 28 | # Next, install Python 2.6 or greater for casperjs in the bin/ directory. 29 | # Finally, when running `casperjs`, the output should be: 30 | casperjs 31 | CasperJS version 1.1.0-beta3 at /usr/share/casperjs, using phantomjs version 1.9.2 32 | ``` 33 | 34 | ## Running tests 35 | Asuming your local environment is set up at http://localhost, all tests may 36 | be run with the following command: 37 | 38 | ```bash 39 | ./testrun 40 | ``` 41 | 42 | You can also run a specific test by giving it as an argument to the command. 43 | homepage.js is a sample test for http://www.msnbc.com. Here is how you could 44 | run it: 45 | 46 | ```bash 47 | ./testrun -u http://www.msnbc.com homepage.js 48 | ``` 49 | 50 | *NOTE* `test` is a wrapper for `casperjs` which sets some useful defaults when 51 | running tests. Run `./testrun -h` for a list of all the available options. 52 | 53 | ## Writing tests 54 | 55 | Tests are JavaScript files which are located at the `tests` directory. 56 | They can be organized depending on different aspects of the site such as the 57 | homepage, the external header and footer, or the search engine. 58 | 59 | `common.js` contains useful methods for all tests and it is included 60 | automatically when running tests. 61 | 62 | Some useful resources for writing tests are: 63 | * [Navigation steps](http://docs.casperjs.org/en/latest/faq.html#how-does-then-and-the-step-stack-work) 64 | let you wait for certain events such a page getting fully rendered before 65 | running assertions over it. 66 | * The [casper](http://docs.casperjs.org/en/latest/modules/casper.html) object has 67 | commands to interact with the browser such as opening a URL or filling 68 | out a form. 69 | * The [test](http://docs.casperjs.org/en/latest/modules/tester.html) 70 | object contains methods to run assertions over the current context. 71 | 72 | ## Cookies 73 | PhantomJS (the browser that CasperJS uses for navitation) stores session 74 | data in a cookie file. Future test runs will reuse the cookie if the file is 75 | present. This is the reason why the `test` executable creates a file called 76 | `cookies.txt` while running tests to store cookie information and deletes it 77 | the next time tests are run. 78 | 79 | ## Tips 80 | ### Taking screenshots 81 | You can take a screenshot with `casper.capture('filename');`. 82 | 83 | Alternatively, you can use `casper.captureSelector('filename', 'div.some-class');` 84 | to take a screenshot of a given selector. 85 | 86 | Find more examples at http://docs.casperjs.org/en/latest/modules/casper.html#capture. 87 | 88 | ### Evaluating code 89 | [casper.evaluate()](http://docs.casperjs.org/en/latest/modules/casper.html#evaluate) 90 | method (and its alternatives such as `casper.evaluateOrDie()`, `casper.thenEvaluate()` or 91 | `test.assertEvaluate()`) are highly powerful methods since they will run JavaScript 92 | code on the page just as if you were debugging with the browser's JavaScript console. 93 | --------------------------------------------------------------------------------