Actions

Difference between revisions of "Selenium Test Case Methods"

From Joomla! Documentation

(6 intermediate revisions by one user not shown)
Line 160: Line 160:
  
 
=== toggleAssignedGroupCheckbox ===
 
=== toggleAssignedGroupCheckbox ===
 +
Toggles selecting a user's assigned group membership. Uses the manually defined variable of <code>$groupName</code> to locate the appropriate label and selects the input that precedes it.
 +
<source lang="php"> function toggleAssignedGroupCheckbox($groupName) {
 +
        $id = $this->getAttribute('//fieldset[@id=\'user-groups\']/ul/li[contains(label,\''.$groupName.'\')]/label@for');
 +
        $this->click($id);
 +
}</source>
  
 
=== deleteTestUsers ===
 
=== deleteTestUsers ===
 +
Deletes all users that partially match the manually defined variable of <code>$partialName</code>. If undefined, <code>$partialName</code> defaults to <code>test</code>.
 +
 +
<source lang="php">function deleteTestUsers($partialName = 'test')
 +
{
 +
echo "Browse to User Manager.\n";
 +
$this->click("link=User Manager");
 +
$this->waitForPageToLoad("30000");
 +
 +
echo "Filter on user name\n";
 +
$this->type("filter_search", $partialName);
 +
$this->click("//button[@type='submit']");
 +
$this->waitForPageToLoad("30000");
 +
 +
echo "Delete all users in view.\n";
 +
$this->click("checkall-toggle");
 +
echo("Delete new user.\n");
 +
$this->click("//li[@id='toolbar-delete']/a/span");
 +
$this->waitForPageToLoad("30000");
 +
try
 +
{
 +
$this->assertTrue($this->isTextPresent("success"));
 +
}
 +
catch (PHPUnit_Framework_AssertionFailedError $e)
 +
{
 +
echo "** ERROR in deleteTestUsers, SeleniumJoomlaTestCase, line 181 **\n";
 +
array_push($this->verificationErrors, $e->toString());
 +
}
 +
}</source>
  
 
=== createGroup ===
 
=== createGroup ===
 +
Creates a group, with the name of that defined by the variable <code>$groupName</code>, that has membership in the group designated by the variable <code>$groupParent</code>. If undefined, <code>$groupParent</code> defaults to Public.
 +
 +
<source lang=php>function createGroup($groupName, $groupParent = 'Public')
 +
{
 +
$this->click("link=Groups");
 +
$this->waitForPageToLoad("30000");
 +
echo "Create new group " . $groupName . ".\n";
 +
$this->click("link=New");
 +
$this->waitForPageToLoad("30000");
 +
$this->type("jform_title", $groupName);
 +
$this->select("id=jform_parent_id", "label=regexp:.*".$groupParent);
 +
$this->click("link=Save & Close");
 +
$this->waitForPageToLoad("30000");
 +
try
 +
{
 +
$this->assertTrue($this->isTextPresent("Group successfully saved"));
 +
echo "Creation of " . $groupName . " succeeded.\n";
 +
}
 +
catch (PHPUnit_Framework_AssertionFailedError $e)
 +
{
 +
array_push($this->verificationErrors, $e->toString());
 +
}
 +
}</source>
  
 
=== deleteGroup ===
 
=== deleteGroup ===

Revision as of 08:37, 6 July 2010

Contents

News and Updates

2010 02 10 : Initial draft of this article

Introduction

With the introduction of System tests in February 2010, there are also a number of Joomla! specific methods that can be used. These methods are part of the SeleniumJoomlaTestCase class, as defined by the file tests/system/SeleniumJoomlaTestCase.php

These methods exist so that none of the path or login information is coded into the tests. Instead, it comes from the config file located at tests/system/servers/configdef.php See Running_Automated_Tests_for_Version_1.6#Create_a_Selenium_Configuration_File These methods can also be used for creating streamlined tests by replacing common and repetitive actions.

Available Selenium Test Case Methods

As of this writing, the following methods belong to the SeleniumJoomlaTestCase class. You can check the actual source code for this class by looking at the file tests/system/SeleniumJoomlaTestCase.php file.

setUp

  • Loads the configuration variables defined in tests/system/servers/configdef.php and launches the test browser accordingly. This is normally the first line in every test method.
public function setUp()
{
    $cfg = new SeleniumConfig();
    $this->cfg = $cfg; // save current configuration
    $this->setBrowser($cfg->browser);
    $this->setBrowserUrl($cfg->host.$cfg->path);
    if(isset($cfg->selhost)) {
        $this->setHost($cfg->selhost);
    }
    echo ".\n".'Starting '.get_class($this).".\n";
}

doAdminLogin

  • Logs into the administrative side of Joomla!
function doAdminLogin()
{
    //$this->setUp();
    echo "Logging in to admin.\n";
    $cfg = new SeleniumConfig();
    $this->open($cfg->path . "administrator/index.php?option=com_login");
    $this->waitForPageToLoad("30000");
    $this->type("mod-login-username", $cfg->username);
    $this->type("mod-login-password", $cfg->password);
    $this->click("link=Log in");
    $this->waitForPageToLoad("30000");
}

doAdminLogout

  • Logs out of the administrative side of Joomla!
function doAdminLogout()
{
    $this->gotoAdmin();
    echo "Logging out of back end.\n";
    $this->click("link=Logout");
}

gotoAdmin

  • Navigates to the administrative side of Joomla!
function gotoAdmin()
{
    echo "Browsing to admin.\n";
    $cfg = new SeleniumConfig();
    $this->open($cfg->path . "administrator");
}

gotoSite

  • Navigates to the front end of Joomla!
function gotoSite()
{
    echo "Browsing to site.\n";
    $cfg = new SeleniumConfig();
    $this->open($cfg->path);
}

doFrontEndLogin

  • Logs into the front end of Joomla!
function doFrontEndLogin()
{
    $this->gotoSite();
    echo "Logging into front end of site.\n";
    $this->type("modlgn_username", "admin");
    $this->type("modlgn_passwd", "password");
    $this->click("Submit");
    $this->waitForPageToLoad("30000");
}

doFrontEndLogout

  • Logs out of the front end of Joomla!
function doFrontEndLogout()
{
    $this->gotoSite();
    echo "Logging out of front end of site.\n";
    $this->click("Submit");
    $this->waitForPageToLoad("30000");
}

setTinyText

  • Allows the test to input data
function setTinyText($text)
{
    $this->selectFrame("text_ifr");
    $this->type("tinymce", $text);
    $this->selectFrame("relative=top");
}

createUser

  • Creates a user automatically. You can specify the various user data fields or keep the defaults.
    function createUser($name = 'Test User', $userName = 'TestUser', $password = 'password', $email = 'testuser@test.com', $group = 'Manager')
    {
        $this->click("link=User Manager");
        $this->waitForPageToLoad("30000");
        echo("Add new user named " . $name . " in Group=" . $group . "\n");
        $this->click("//li[@id='toolbar-new']/a/span");
        $this->waitForPageToLoad("30000");
        $this->type("jform_name", $name);
        $this->type("jform_username", $userName);
        $this->type("jform_password", $password);
        $this->type("jform_password2", $password);
        $this->type("jform_email", $email);
        $id = $this->getAttribute('//fieldset[@id=\'user-groups\']/ul/li[contains(label,\''.$group.'\')]/label@for');
        $this->click($id);
        $this->click("link=Save & Close");
        $this->waitForPageToLoad("30000");
        echo "New user created\n";
    }

toggleAssignedGroupCheckbox

Toggles selecting a user's assigned group membership. Uses the manually defined variable of $groupName to locate the appropriate label and selects the input that precedes it.

        function toggleAssignedGroupCheckbox($groupName) {
                $id = $this->getAttribute('//fieldset[@id=\'user-groups\']/ul/li[contains(label,\''.$groupName.'\')]/label@for');
                $this->click($id);
        }

deleteTestUsers

Deletes all users that partially match the manually defined variable of $partialName. If undefined, $partialName defaults to test.

function deleteTestUsers($partialName = 'test')
        {
                echo "Browse to User Manager.\n";
                $this->click("link=User Manager");
                $this->waitForPageToLoad("30000");
 
                echo "Filter on user name\n";
                $this->type("filter_search", $partialName);
                $this->click("//button[@type='submit']");
                $this->waitForPageToLoad("30000");
 
                echo "Delete all users in view.\n";
                $this->click("checkall-toggle");
                echo("Delete new user.\n");
                $this->click("//li[@id='toolbar-delete']/a/span");
                $this->waitForPageToLoad("30000");
                try
                {
                        $this->assertTrue($this->isTextPresent("success"));
                }
                catch (PHPUnit_Framework_AssertionFailedError $e)
                {
                        echo "** ERROR in deleteTestUsers, SeleniumJoomlaTestCase, line 181 **\n";
                        array_push($this->verificationErrors, $e->toString());
                }
        }

createGroup

Creates a group, with the name of that defined by the variable $groupName, that has membership in the group designated by the variable $groupParent. If undefined, $groupParent defaults to Public.

function createGroup($groupName, $groupParent = 'Public')
        {
                $this->click("link=Groups");
                $this->waitForPageToLoad("30000");
                echo "Create new group " . $groupName . ".\n";
                $this->click("link=New");
                $this->waitForPageToLoad("30000");
                $this->type("jform_title", $groupName);
                $this->select("id=jform_parent_id", "label=regexp:.*".$groupParent);
                $this->click("link=Save & Close");
                $this->waitForPageToLoad("30000");
                try
                {
                        $this->assertTrue($this->isTextPresent("Group successfully saved"));
                        echo "Creation of " . $groupName . " succeeded.\n";
                }
                catch (PHPUnit_Framework_AssertionFailedError $e)
                {
                        array_push($this->verificationErrors, $e->toString());
                }
        }

deleteGroup

changeAccessLevel

clickGo

jInput

jClick

toggleFeatured

togglePublished

checkNotices