Difference between revisions of "Writing System Tests for Joomla! - Part 1"

From Joomla! Documentation

Line 170: Line 170:
 
with
 
with
 
<pre>$this->doAdminLogin();</pre>
 
<pre>$this->doAdminLogin();</pre>
 +
 +
At this point, running our test would succeed up until the point where we need to log into the administrative interface the second time. We'll fix that by replacing:
 +
<pre>    $this->type("mod-login-username", "admin");
 +
    $this->click("link=Log in");
 +
    $this->waitForPageToLoad("30000");</pre>
 +
 +
with
 +
 +
<pre>$this->doAdminLogin();</pre>
 +
 +
Running our test should now successfully create, verify the creation of, and delete our new test user.
 +
 +
 +
 +
 +
  
 
[[Category:Bug Squad]] [[Category:Development]] [[Category:Testing]]
 
[[Category:Bug Squad]] [[Category:Development]] [[Category:Testing]]

Revision as of 11:44, 11 February 2010


Introduction[edit]

As documented at Running_Automated_Tests_for_Version_1.6, Joomla! version 1.6 now includes a library of system tests. This document details the steps necessary to create system tests using Selenium IDE. You will also need to run Firefox and install the Selenium IDE add-on to Firefox. Instructions for this can be found in the Functional Testing article.

NOTE: This article assumes that you have read the aforementioned article and have PHPUnit and Selenium set up and working properly.

Overview[edit]

Writing a system test essentially entails converting a Selenium IDE test to a format that we can easily integrate into the Joomla! test suite. See Functional_Testing for a good primer on system tests as well as Intermediate_Selenium_Example for a great example of creating an Intermediate Selenium test.

Creating an Intermediate System Test[edit]

A few items to note:

  • It's recommended to update your local copy of 1.6 to the latest build before writing any tests.
  • The test are written in PHP and we are using Selenium RC to execute them later. You will need to select the PHP Selenium RC Format in Slenium IDE. Go to Options -> Format -> PHP Selenium RC
  • More information about writing test can be found at http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html

In this test, we are going to execute the test described at Intermediate_Selenium_Example and integrate it into the system tests suite in 1.6.

1. Open 1.6 in FireFox and navigate to the home page.

2. Open Selenium IDE by going to the Tools menu and select Selenium IDE. Selenium will start recording automatically and the record button will appear highlighted.

3. Verify that the Base URL displayed in Selenium is that of your test site.

4. Perform the test by following the instructions described above (note: there are some slight variations between the test described above and the one we performed. This is due to variation of 1.6 during the development process).

5. When you have completed your test, click the the red record button (see image below) to stop recording.

Selenium-screen.png

6. Once recording has stopped, create a new PHP file in Eclipse (or your favorite code editor) and copy / paste the code from Selenium IDE to there. The contents of the example test is:

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  function setUp()
  {
    $this->setBrowser("*chrome");
    $this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
  }

  function testMyTestCase()
  {
    $this->open("/workspace/joomla-1-6-source/administrator/index.php?option=com_login");
    $this->type("mod-login-username", "admin");
    $this->click("link=Log in");
    $this->waitForPageToLoad("30000");
    $this->click("link=User Manager");
    $this->waitForPageToLoad("30000");
    $this->click("//li[@id='toolbar-new']/a/span");
    $this->waitForPageToLoad("30000");
    $this->type("jform_name", "My Test User");
    $this->type("jform_username", "TestUser");
    $this->type("jform_password", "password");
    $this->type("jform_password2", "password");
    $this->type("jform_email", "test@example.com");
    $this->click("1group_2");
    $this->click("link=Save & Close");
    $this->waitForPageToLoad("30000");
    try {
        $this->assertTrue($this->isTextPresent("Item successfully saved."));
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors, $e->toString());
    }
    $this->type("search", "TestUser");
    $this->click("//button[@type='submit']");
    $this->waitForPageToLoad("30000");
    try {
        $this->assertTrue($this->isTextPresent("TestUser"));
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors, $e->toString());
    }
    $this->click("link=Log out");
    $this->waitForPageToLoad("30000");
    $this->click("link=Go to site home page.");
    $this->waitForPageToLoad("30000");
    $this->type("modlgn_username", "TestUser");
    $this->type("modlgn_passwd", "password");
    $this->click("Submit");
    $this->waitForPageToLoad("30000");
    try {
        $this->assertTrue($this->isTextPresent("My Test User"));
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors, $e->toString());
    }
    $this->click("link=Logout");
    $this->waitForPageToLoad("30000");
    $this->click("//button[@type='submit']");
    $this->waitForPageToLoad("30000");
    $this->click("link=Site Administrator");
    $this->waitForPageToLoad("30000");
    $this->type("mod-login-username", "admin");
    $this->click("link=Log in");
    $this->waitForPageToLoad("30000");
    $this->click("//img[@alt='User Manager']");
    $this->waitForPageToLoad("30000");
    $this->click("cb0");
    $this->click("link=Delete");
    $this->waitForPageToLoad("30000");
    try {
        $this->assertTrue($this->isTextPresent("1 item(s) successfully deleted."));
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
        array_push($this->verificationErrors, $e->toString());
    }
    $this->click("link=Log out");
    $this->waitForPageToLoad("30000");
  }
}
?>

As noted in Intermediate_Selenium_Example Selenium does not record passwords that are entered. Additionally, we can utilize Selenium_Test_Case_Methods to prevent coding path or login information in the test:

1. The first step in integrating this test into tests/suite is to call SeleniumJoomlaTestCase.php so that we can utilize the Selenium Test Case Methods. To do this, we will replace the first portion of our code:

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

with the following code

<?php
require_once 'SeleniumJoomlaTestCase.php';
  • We can now start utilizing the Selenium Joomla Test Case Methods.

2. We will now extend a class into our SeleniumJoomlaTestCase to implement the client/server protocol to talk to Selenium RC as well as specialized assertion methods for web testing.

This is done by replacing:

class Example extends PHPUnit_Extensions_SeleniumTestCase
{

with

class MyTestCase extends SeleniumJoomlaTestCase
{
  • The top portion of our test now looks like:
<?php
require_once 'SeleniumJoomlaTestCase.php';

class User0001Test extends SeleniumJoomlaTestCase
{

  function testMyTestCase()
  {

3. At this point, running the test would successfully launch a two browsers instances, one for Selenium Remote Control and one for test itself, but it would fail due to the lack of the password being recorded. Additionally, we want to be sure that none of the path information is hard coded in the test.

To improve our code, and achieve what we noted above, we would delete the function below:

function setUp()
  {
    $this->setBrowser("*chrome");
    $this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
  }

and add the following to our testMyTestCase function

	$this->setUp();

Additionaly, we can replace the following line of code:

$this->open("/workspace/joomla-1-6-source/administrator/index.php?option=com_login");

with

$this->gotoAdmin();

As you can see, this will prevent us from hard coding the path into the test.

We can now replace

$this->type("mod-login-username", "admin");
    $this->click("link=Log in");
    $this->waitForPageToLoad("30000");

with

$this->doAdminLogin();

At this point, running our test would succeed up until the point where we need to log into the administrative interface the second time. We'll fix that by replacing:

    $this->type("mod-login-username", "admin");
    $this->click("link=Log in");
    $this->waitForPageToLoad("30000");

with

$this->doAdminLogin();

Running our test should now successfully create, verify the creation of, and delete our new test user.