Automation testing using integration of Selenium, PHPUnit, Ant and create reports

Prerequisites for the integration of automation suit Selenium, PHPUnit and Ant are as below-

http://www.unclecode.com/2010/05/install-phpunit-on-wamp-xamp-manually-without-pear/

http://www.unclecode.com/2012/05/how-to-setup-selenium-webdriver-and-execute-automation-script/

http://www.unclecode.com/2012/06/executing-and-creating-selenium-rc-automation-test-cases-using-phpunit-by-sebastian-bergmann/

Install Ant

Download ant from http://ant.apache.org/bindownload.cgi “zip” file for windows.
Follow all the steps for installation http://ant.apache.org/manual/install.html and  also optionally install the libraries as mentioned

Automation testing using integration of Selenium, PHPUnit and Ant

Run Selenium through command prompt as mentioned in one of the article above.

Create a folder “runtests”  under your project directory some where on the file system.

This “runtests” folder will contain all the selenium test case files generated through Selenium IDE.

Create AllMyTests.php file under runtests folder. This configures phpunit to execute all the files under runtest folder.  Source code provided below.

Create build.xml file under your project directory adjacent to rntests folder.

On Command Prompt run/execute the following command in this folder (make sure ant is configured, test ant -version on command prompt to check)-
>ant build

This will read build.xml file and execute all the selenium test cases. Source code for build.xml is provided below.

build.xml

<!– This project launches the test generator and execute all phpunit selenium tests –>
<project name=”proj” default=”build” basedir=””>
<!–Get environment variables –>
<property environment=”env” />
<property name=”logFolder” value=”${basedir}\logs”/>
<property name=”testFolder” value=”${basedir}\runtest”/>

<property name=”test” value=”**” />
<condition property=”pattern” value=”runtest/*.php”>
<os family=”windows” />
</condition>

<tstamp/>

<!– Check Operating system to set phpunit path–>
<target name=”check_os”>
<condition property=”isWindows”>
<os family=”windows” />
</condition>
<condition property=”isLinux”>
<os family=”unix” />
</condition>
</target>

<target name=”if_windows” depends=”check_os” if=”isWindows”>
<property name=”exe.phpunit” value=”C:\wamp\bin\php\php5.2.9-2\phpunit.bat” />
</target>

<target name=”if_unix” depends=”check_os” if=”isLinux”>
<property name=”exe.phpunit” value=”${env.PHP_HOME}/includes/PHPUnit-3.2.0/PHPUnit” />
</target>

<target name=”prepare” depends=”if_windows, if_unix”>
<mkdir dir=”${logFolder}”/>
</target>

<target name=”phpunit”>
<!– Check if folder empty –>
<fileset id=”fileset.test” dir=”${testFolder}”>
<include name=”*.*”/>
</fileset>
<fail message=”Files not found”>
<condition>
<resourcecount refid=”fileset.test” when=”less” count=”1″></resourcecount>
</condition>
</fail>
<!– Execute phpunit tests –>
<exec executable=”${exe.phpunit}” failonerror=”true” dir=”runtest”>
<arg line=”–verbose –testdox-html ‘${logFolder}/phpunit-report-${TODAY}.html’ –no-syntax-check –filter ${test} AllMyTests” />
</exec>
</target>

<target name=”build” depends=”prepare,phpunit”/>
</project>

AllMyTests.php file under “runtest” folder

<?php
define(‘PATTERN_OF_TEST_FILES’, ‘-test.php’);

spl_autoload_register(“autoloader_for_tests”);

function autoloader_for_tests($c)
{
$location = getLocationForClassName($c);
if (!empty($location))
{
require($location);
}
}

class AllMyTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
//optional
ini_set(‘memory_limit’, ‘200M’);

$phpunitsuit = new PHPUnit_Framework_TestSuite(‘PHPUnit Framework’);

$files = new FilterTheDirectory(dirname(__FILE__));

foreach($files as $file_name)
{
include_once($file_name);

$phpunitsuitName = str_replace(PATTERN_OF_TEST_FILES, ”, $file_name);
$phpunitsuit->addTestSuite($phpunitsuitName);
}
return $phpunitsuit;
}
}

class FilterTheDirectory extends FilterIterator
{
public function __construct($path)
{
parent::__construct(new DirectoryIterator($path));
}

public function accept()
{
return strpos($this->getInnerIterator()->getFilename(), PATTERN_OF_TEST_FILES) !== FALSE;
}
}

if (!defined(‘PHPUnit_MAIN_METHOD’))
{
AllMyTests::main();
}
?>

Bookmark and Share
Both comments and pings are currently closed.

Comments are closed.

n