First, you will need to get your testing sandbox up and running. You will need Drupal with clean URLs support.
Quick start
This will start your Selenium server:
Now, start a new terminal session - this will be your client.
Details
If you did an SVN Install, you need to set up the default permissions for Anonymous Users to the same values as from a default Drupal installation. In particular, enable permissions for:
- CiviMail: access CiviMail subscribe/unsubscribe pages
- CiviCRM: access all custom data
- CiviCRM: access uploaded files
- CiviContribute: make online contributions
- CiviCRM: profile create
- CiviCRM: profile view
- CiviEvent: register for events
It can be useful to download & install Selenium IDE (as well as the Selenium IDE PHP plugin) but is not necessary to run web tests. You will also need Java 1.5+ (which is probably installed on your machine already) and Selenium Standalone, which is in the repository (we'll come to that later). (If you get an error about java.lang.IllegalArgumentException: Invalid uri then you may need to upgrade to java 1.6.)
| Browser Support Using the latest version of the Selenium Server - 2.15 (which is available from the CiviCRM repository) Web tests have been successfully run with FireFox versions 3 thru 8 and Safari version 3. Selenium lists the supported browsers for running webtests here: |
One more step to be done is getting to tests/phpunit/CiviTest/ directory, copying CiviSeleniumSettings.php.txt to CiviSeleniumSettings.php and adjusting variables there. These are:
- $browser - this defaults to'*firefox' and should work in most of the cases. You might want to adjust it in case you want to test with different browser, or change to '*firefox /path/to/your/firefox/binary' if you're experiencing problems with starting the browser when running tests
- $sandboxURL - this defaults to 'http://tests.dev.civicrm.org', which means you will be running your tests against public CiviCRM testing sandbox. It's much better to run it against your local sandbox, so you might want to change it to something like 'http://localhost'
- $sandboxPATH - default is '/drupal/', which is a URL path of public CiviCRM testing sandbox. If your local sandbox is installed in the subdirectory, adjust it appropriately, if not, leave it as empty string ('').
- $username - Drupal username in the sandbox you're going to run your tests against.
- $password - password for Drupal username mentioned above.
And on the basic level - that's it for the setup. There is also another part of the setup that allows you to get code coverage reports from webtests, but that part is still a bit unstable and not crucial to start work (instructions will be posted here soon).
So, how to write your first Selenium test? We are using PHPUnit to run tests, so first of all, you will have to switch your Selenium IDE to register tests in proper format. Go to Tools > Selenium IDE menu item in your Firefox, than click Options > Format > PHP - Selenium RC. You're ready to go. Oh, and for now, we are only ready to run webtests with Drupal, so use your Drupal sandbox, not Joomla or Standalone.
Our Selenium tests look like this:
Above tests goes to your local sandbox, logs in, than goes to CiviCRM Dashboard and basically checks if there is a certain link there.
So let's start. First, think about what's the scenario that you're going to test. For example, it can be adding a contact. Make sure you have it thought over before you start.
Prepare a file with contents like below. It should go into tests/phpunit/WebTest directory. The structure there reflects specific test scenarios, so if you're testing for contact related functionality, you should place your test in tests/phpunit/WebTest/Contact directory. If you're testing something related to contributions, the directory should be tests/phpunit/WebTest/Contribute, etc. Also make sure, that the name of your webtest is descriptive: NewContactTest.php, AddContributionWithCustomFieldsTest.php, etc. Don't be afraid of long names here!
Now, go to your local CiviCRM sandbox and log out from it. Go to main page, open Selenium IDE and click red, round "Record" button in upper right corner of the window. From now on, Selenium IDE will be translating all your clicks and actions in the browser to webtests. So go ahead, and start clicking through your scenario: log in, go to CiviCRM's main page, go to contact adding screen, fill in all the fields, click save. While you're there, you might also want to for example check if certain elements on the page are present - right-click on chosen element and choose assertElementPresent option from the context menu. You should see all your actions being registered in Selenium IDE window, as PHPunit code. Once you're done, click the "Record" icon again to stop recording, copy the contents of testMyTestCase() method in Selenium IDE window and paste it into the method that you prepared. Save the file.
Running a Webtest
Now it's time to run your test. Before you do it, you need to make sure that Selenium RC is running. Go to tools/packages/SeleniumRC/selenium-server-2.15/ and run following command:
It's best if you do it in separate terminal window - it needs to be running when you will be executing your Selenium tests! -browserSideLog parameter will give you more useful output - showing the messages which would normally appear only in browser when you execute the test. If you want to be able to see those messages separately, you can use a little trick below (works only on Unix systems). Pipe your Selenium server to separate log file (adjust the /tmp/ directory path as needed for your environment):
...and in same terminal window, run:
This way you'll get more complete information on what's going on when the test runs.
Ok, got Selenium RC running? Now go to tools/ directory in your repository checkout and run:
You can also choose to run a single test function in the test class file using the --filter command.
If you are using Windows, you will need to run it with "php scripts\phpunit -u user -ppassword -hhostname WebTest_Contact_YourWebtestName". |
There you go. You just executed your first Selenium on PHPUnit test. ![]()
| Stopping the selenium server If the selenium server is the active process in your command line window, you can use Ctrl+C to stop the server. If you're running the selenium server as a background process (you've used the startup example above that ends with '&') - then you'll need to figure out the PID (process id) and use the kill command to stop it. For example .. |
