Porto

Automated Testing With Selenium2 And PHPUnit

Automated Testing With Selenium2 And PHPUnit

  1. Abstract
  2. Installation
  3. First Test
  4. Setting Up Selenium Environment
  5. Testing With Multiple Browsers
  6. Assertions
  7. Test Form Exists
  8. Test Form Action
  9. Fill Form Fields and Submit Form
  10. Click Form Submit Button
  11. Check Form Submitted

Abstract

Selenium2 is a software testing framework for web applications. This tutorial focuses on automating browser testing using Selenium2.

Installation

The Selenium 2.x server is a drop-in replacement for the old Selenium RC and WebDriver
Get the latest server. At the time of this writing it is 2.37.0
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.37.0.jar

Then, to start the server, use this command

java -jar selenium-server-standalone-2.37.0.jar

Selenium2 testing for PHP runs out of PHPUnit, and the can be installed using this command

pear install phpunit/PHPUnit_Selenium

Now the first test can be created to test

First Test

With the Selenium 2 server in place, and PHPUnit Selenium add on ready, the first test can be created. Here we will test the title tag of the PHPRO.ORG website.

Create a file called phpro.php with your favourite editor, and place this in it.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function 
setUp()
    {
        
$this->setBrowser('firefox');
        
$this->setBrowserUrl('http://www.phpro.org/');
    }

    
/*
    *
    * Tests the title of PHPRO.ORG is correct
    *
    */
    
public function testTitle()
    {
        
$title 'PHP Tutorials Examples phPro - Tutorials Articles Examples Development';
        
$this->url('http://www.phpro.org/');
        
$this->assertEquals$title$this->title());
    }
}
?>

To execute the above code, simply call with PHPUnit, as shown with this command.

phpunit phpro.php

If all is well, the results from the above test will look like this:

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.
.
Time: 5.06 seconds, Memory: 5.50Mb
OK (1 test, 1 assertion)

The dot signifies the test as successful. So, everything is up and running.

This test case makes use of the PHPUnit setup method. When testing with Selenium, all test classes must have a setup method to set up the Selenium2 environment.

Setting Up the Selenium Environment

In the above example, the setBrowser() and setBrowserUrl() methods were used to aid in the setting up of hte Selenium2 enviroment to use with PHPUnit.

Below is a table of methods available to classes which extend the PHPUnit_Extensions_SeleniumTestCase class.

MethodUse
setBrowser()Sets the browser to be used by the Selenium 2 Server.
setBrowserUrl()Sets the base URL for these tests.
setHost()Sets the hostname for the connection to the Selenium 2 Server
setPort()Sets the port for the connection to the Selenium 2 Server
Sets the timeout for the connection to the Selenium 2 Server
setSleep()Sets the number of seconds the Selenium Server client sleeps between sending tests to the Selenium 2 Server

Testing With Multiple Browsers

Up to this point, only a single browser has been used. However, there are many different browsers to test for. In this test, Firefox, Opera, and Chrome are also tested for.


<?php

class phproTest extends PHPUnit_Extensions_SeleniumTestCase
{
        public static 
$browsers = array(
                array(
                        
'name'    => 'Linux Firefox',
                        
'browser' => '*firefox',
                        
'host'    => 'localhost',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     ),
                array(
                        
'name'    => 'Linux Opera',
                        
'browser' => '*opera',
                        
'host'    => 'localhost',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     ),
                array(
                        
'name'    => 'Linux Chrome',
                        
'browser' => '*chrome',
                        
'host'    => 'localhost',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     ),

                array(
                        
'name'    => 'MacOSX Safari',
                        
'browser' => '*safari',
                        
'host'    => 'my.macosx.box',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     ),
                array(
                        
'name'    => 'Windows Safari',
                        
'browser' => '*custom C:\Program Files\Safari\Safari.exe -url',
                        
'host'    => 'my.windowsxp.box',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     ),
                array(
                        
'name'    => 'Windows IE',
                        
'browser' => '*iexplore',
                        
'host'    => 'my.windowsxp.box',
                        
'port'    => 4444,
                        
'timeout' => 30000,
                     )
                );

        protected function 
setUp()
        {
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testTitle()
        {
                
$this->open('http://www.phpro.org/');
                
$this->assertTitle('PHP Tutorials Examples phPro - Tutorials Articles Examples Development');
        }
}
?>

In the above example, an array of browsers for various operating systems has been specified along with other configuration options. When run, the code executes for each of the browsers specified. Should the browser not exist, the test is skipped.

This test was performed on a Linux system, so should execute three times, once for each of the linux browsers specified. The results look like this.

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.
...SSS
Time: 22.66 seconds, Memory: 6.25Mb

The resulting report tells that the test has been has been run six times. The first three tests have passed, however, the next three browser tests have failed, as the operating system is not correct for this test.

Assertions

In the above examples, the tests have been to assert the value of the phpro.org index page is equal to a given value using the assertEquals() method. Below is a list of the assertions possible with the PHPUnit_Extensions_Selenium2TestCase class.

Assert Equals By ID


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function 
setUp()
    {
        
// Which browser to use
        
$this->setBrowser('firefox');
        
// The base URL
        
$this->setBrowserUrl('http://www.phpro.org/');
    }

    public function 
testContactName()
    {
        
// The URL to test
        
$this->url('http://www.phpro.org/contact');
        
// check the value of an element by ID
        
$this->assertEquals('Anonymous Coward'$this->byId('Name')->value());
    }
}
?>

Assert Contains

Asserts that an element contains a give value.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function 
setUp()
    {
        
// Which browser to use
        
$this->setBrowser('firefox');
        
// The base URL
        
$this->setBrowserUrl('http://www.phpro.org/');
    }

    public function 
testContactName()
    {
        
// The URL to test
        
$this->url('http://www.phpro.org/contact');
        
// check the value of an element by ID
        
$this->assertContains'Anonymous Coward'$this->byId('Name')->value());
    }
}

Assert Not Contains


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function 
setUp()
    {
        
// Which browser to use
        
$this->setBrowser('firefox');
        
// The base URL
        
$this->setBrowserUrl('http://www.phpro.org/');
    }

    public function 
testContactName()
    {
        
// The URL to test
        
$this->url('http://www.phpro.org/contact');
        
// check the value of an element by ID
        
$this->assertContains'Anonymous Coward'$this->byId('Name')->value());
    }
}

assertByRegExp

To assert by regular expression is made very simple by Selenium2 and PHPUnit with the use of hte assertByRegExp() method. In this example, the PHPRO.ORG index page is loaded, and searched for the string 'Poll'. Where this occurs in the page is not important in this test, just that the string exists. To load the entire page for searching, the Selenium2 source method is used as seen here.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testPollExists()
        {
                
$this->url'http://www.phpro.org/' );

                
// check the value
                
$this->assertRegExp'/Poll/i'$this->source() );
        }
}
?>

Test Form Exists

In this example, the contact form for PHPRO.ORG is used to as an example of how to check for the existence of a form, and check that all the elements are in place.

The form has five elements, and the existence of each element needs to be verified.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testContactFormExists()
        {
                
$this->url'http://www.phpro.org/contact' );

                
$name $this->byName'sender_name' );
                
$email $this->byName'sender_email' );
                
$submit $this->byName'submit_button' );
                
$message $this->byName'sender_message' );
                
$surname $this->byName'sender_surname' );
        }
}
?>

When the test code is run, Selenium2 checks for the existance of each of the form fields. However, we want to also check the default values of each of the forms. We can check these values as shown above for each of the fields.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testContactFormExists()
        {
                
$this->url'http://www.phpro.org/contact' );

                
$name $this->byName'sender_name' );
                
$email $this->byName'sender_email' );
                
$submit $this->byName'submit_button' );
                
$message $this->byName'sender_message' );
                
$surname $this->byName'sender_surname' );

                
$this->assertEquals'Anonymous Coward'$name->value() );
                
$this->assertEquals''$email->value() );
                
$this->assertEquals'Submit'$submit->value() );
                
$this->assertEquals'A small message here'$message->value() );
                
$this->assertEquals''$surname->value() );
        }
}
?>

Now when the test is executed, this is the result:

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.

.

Time: 8.94 seconds, Memory: 5.75Mb

OK (1 test, 5 assertions)

Test Form Action

This test creates a second test to our contact page testing class. Here a test is created to check the value of the contact form action. This could be acheived by using the $this-<byCssSelector() method like this.


$action = $this->byCssSelector( 'form' )->attribute( 'action' );

However, as the contact page has many forms, this would not work, as the selector would not know which form to get the action from. So, the byId() method is used to target the correct form. Our test class now looks like this..


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testContactFormExists()
        {
                
$this->url'http://www.phpro.org/contact' );

                
$name $this->byName'sender_name' );
                
$email $this->byName'sender_email' );
                
$submit $this->byName'submit_button' );
                
$message $this->byName'sender_message' );
                
$surname $this->byName'sender_surname' );

                
$this->assertEquals'Anonymous Coward'$name->value() );
                
$this->assertEquals''$email->value() );
                
$this->assertEquals'Submit'$submit->value() );
                
$this->assertEquals'A small message here'$message->value() );
                
$this->assertEquals''$surname->value() );
        }

        public function 
testSubmitToSelf()
        {
                
// set the url
                
$this->url'contact' );
                
// get the form action
                
$action $this->byId'contact_form' )->attribute'action' );
                
// check the action value
                
$this->assertEquals'http://www.phpro.org/contact'$action );
        }
}
?>

When this test code is run, both the tests are run and the output tells us the story, as below.

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.

..

Time: 9.25 seconds, Memory: 5.75Mb

OK (2 tests, 6 assertions)

Fill Form Fields and Submit Form

Filling out form field values is a simple task. All that is required is to specify the field name and assign the value as in the example below. Submitting the form is even easier and all that is required is a call to the submit method. With this code in place, the test now looks like this.


<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testContactFormExists()
        {
                
$this->url'http://www.phpro.org/contact' );

                
$name $this->byName'sender_name' );
                
$email $this->byName'sender_email' );
                
$submit $this->byName'submit_button' );
                
$message $this->byName'sender_message' );
                
$surname $this->byName'sender_surname' );

                
$this->assertEquals'Anonymous Coward'$name->value() );
                
$this->assertEquals''$email->value() );
                
$this->assertEquals'Submit'$submit->value() );
                
$this->assertEquals'A small message here'$message->value() );
                
$this->assertEquals''$surname->value() );
        }

        public function 
testSubmitToSelf()
        {
                
// set the url
                
$this->url'contact' );

                
// create a form object for reuse
                
$form $this->byId'contact_form' );

                
// get the form action
                
$action $form->attribute'action' );

                
// check the action value
                
$this->assertEquals'http://www.phpro.org/contact'$action );
                
// fill in the form field values
                
$this->byName'sender_name' )->value'Kevin Waterson' );
                
$this->byName'sender_email' )->value'kevin@example.com' );
                
$this->byName'sender_message' )->value'A new message' );

                
// submit the form
                
$form->submit();
        }
}
?>

The results of the above code should look like this:

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.

..

Time: 11.72 seconds, Memory: 6.00Mb

OK (2 tests, 6 assertions)

Check Form submitted

When the contact form is posted successfully, the page is reloaded with a message saying "Mail sent to PHPRO.ORG". This message is inside a h3 HTML tag, and so we can target this with a CSS Selector to check if this is visible. If the form has posted successfully, we can use assertEquals to test the value.

<?php

class phproTest extends PHPUnit_Extensions_Selenium2TestCase
{
        protected function 
setUp()
        {
                
// Which browser to use
                
$this->setBrowser('firefox');
                
// The base URL
                
$this->setBrowserUrl('http://www.phpro.org/');
        }

        public function 
testContactFormExists()
        {
                
$this->url'http://www.phpro.org/contact' );

                
$name $this->byName'sender_name' );
                
$email $this->byName'sender_email' );
                
$submit $this->byName'submit_button' );
                
$message $this->byName'sender_message' );
                
$surname $this->byName'sender_surname' );

                
$this->assertEquals'Anonymous Coward'$name->value() );
                
$this->assertEquals''$email->value() );
                
$this->assertEquals'Submit'$submit->value() );
                
$this->assertEquals'A small message here'$message->value() );
                
$this->assertEquals''$surname->value() );
        }

       public function 
testSubmitToSelf()
        {
                
// set the url
                
$this->url'contact' );

                
// create a form object for reuse
                
$form $this->byId'contact_form' );

                
// get the form action
                
$action $form->attribute'action' );

                
// check the action value
                
$this->assertEquals'http://www.phpro.org/contact'$action );

                
// fill in the form field values
                
$this->byName'sender_name' )->value'Kevin Waterson' );
                
$this->byName'sender_email' )->value'kevin@example.com' );
                
$this->byName'sender_message' )->value'A new message' );

                
// submit the form
                
$form->submit();

                
// check if form was posted
                
$success $this->byCssSelector('h3')->text();

                
// check the value
                
$this->assertEquals'Mail sent to PHPRO.ORG'$success );
        }
}
?>

With the new assertion testing for the successful message, the return from testing this code should look like this..

# phpunit phpro.php
PHPUnit 3.7.28 by Sebastian Bergmann.

..

Time: 18.44 seconds, Memory: 6.00Mb

OK (2 tests, 7 assertions)