Frontend Unit Testing using PHPUnit and Selenium RC

How to Implement Frontend Unit Testing using PHPUnit and Selenium RC - on this tutorial we'll learn how to implement UnitTesting, UnitTesting which is known to be applicable only on server side scripts. On this article we'll tackle ways of using PHPUnit and Selenium RC so as to test the output of our sites on any browsers.


I. Pre-Requisites

Before we proceed, we assume that you have the following components on your PC:

1. Java installed. For the server to run you’ll need Java installed and the PATH environment variable correctly configured to run it from the console. You can check that you have Java correctly installed by running the following on a console:

C:\>java –version

Online check: http://www.java.com/en/download/installed.jsp

If you get a version number (which needs to be 1.5 or later), you’re ready to start using Selenium-RC.

2. PEAR installed. The base installation that comes with the PHP distribution contains all the stuff that is needed to run the PEAR installation tools etc. If you have a recent installation of PHP, you can relax: The PEAR base installation is already there - unless you have compiled your PHP with the ./configure flag --without-pear.

Read more:
http://pear.php.net/manual/en/installation.introduction.php


3. XAMPP. XAMMP is an easy to install Apache distribution containing MySQL, PHP and Perl.

http://www.apachefriends.org/en/xampp.html


II. Install PHPUnit using PEAR installer

Run the following command:

C:\xampp\php>pear channel-discover pear.phpunit.de
C:\xampp\php>pear install channel://pear.phpunit.de/PHPUnit

Read more: http://pear.phpunit.de/


III. Install Selenium Remote Control

1. Download the latest Selenium RC from this location:

http://seleniumhq.org/download/

2. Extract selenium-server-standalone-x.x.x.jar and place it in any desired directory.

For this tutorial we put it in C:\xampp\php


IV. Start Selenium RC Server

Run this command on a separate console:

C:\xampp\php>java -jar selenium-server-standalone-x.x.x.jar


V. Selenium Sample PHP Test Script

1. Copy-paste the PHP code below and save it as test.php in any desired directory. For this tutorial we put it in C:\xampp\php

2. Create folder: C:\xampp\htdocs\screenshots

Run this command on a separate console:

C:\xampp\php>phpunit test

This script creates list of AutoMD How-Tos to crawl based on existing links on http://www.automd.com/car-repair-guides-list/

If How-To page is missing breadcrumb, the script will take a screenshot, save it in C:\xampp\htdocs\screenshots, and continues to the next page.


<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class test extends PHPUnit_Extensions_SeleniumTestCase
{
  protected $autoStop = FALSE;
  protected $captureScreenshotOnFailure = TRUE;
  protected $screenshotPath = 'C:/xampp/htdocs/screenshots';
  protected $screenshotUrl = 'http://localhost/screenshots';

  function setUp()
  {
    $this->setTimeout(300);
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://www.automd.com/");
  }

  function testMyTestCase()
  {
    $this->open("/car-repair-guides-list/");

    for($i=1;$i<6;$i++)
    {
      $howto = "xpath=/html/body/div[@id='all']/div[@id='area']/div[@id='area-c']/div[@id='content']/div[@id='howto-".$i."']";
      if($this->isElementPresent($howto))
      {
        for($a=1; $a<26; $a++)
        {
          $ul1 = $howto."/ul[1]/li[$a]";
          if($this->isElementPresent($ul1))
          {
            $c++;
            $xpath[] = $link = $this->getAttribute($ul1."/a@href");
            echo $link ."\n";
          }

          $ul2 = $howto."/ul[2]/li[$a]";
          if($this->isElementPresent($ul2))
          {
            $c++;
            $xpath[] = $link = $this->getAttribute($ul2."/a@href");
            echo $link ."\n";
          }
        }
      }
    }

    foreach($xpath as $k => $v)
    {
      $this->checkBreadcrumb($v);
    }
  }

  function checkBreadcrumb($link)
  {
    $this->open($link);

    $this->waitForCondition($this->isElementPresent("//div[@id='content']/div[2]/div/h2"), 300000);
    $title = ($txt=$this->getText("//div[@id='content']/div[2]/div/h2")) ? $txt : 'blank title';

    echo $title . "\n";

    try 
    {
      $this->assertContains($title, $this->getText("//div[@id='content']/div[1]"));
    }
    catch (PHPUnit_Framework_AssertionFailedError $e)
    {
      array_push($this->verificationErrors, $e->toString());
      $this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . str_replace('/', '_', $link) . $this->testId . '.png');
    }
  }
}



References:

Chapter 18. PHPUnit and Selenium, http://www.phpunit.de/manual/current/en/selenium.html
PHPUnit PEAR Channel, http://pear.phpunit.de/
PEAR Introduction, http://pear.php.net/manual/en/installation.introduction.php
Selenium RC Documentation, http://seleniumhq.org/docs/05_selenium_rc.html
Selenium Reference, http://release.seleniumhq.org/selenium-core/0.8.0/reference.html

Comments

Popular posts from this blog

Remote Deployment with Ant and Rsync

How to Install CruiseControl