I previously wrote about the internals of Mocha. The reason that I was intrigued by the inner workings of Mocha was because I was in the process of building a functional testing tool - Phantom Mochachino.
Phantom Mochachino is an extension of Mocha which works with the PhantomJS headless web browser to offer an end to end functional testing utility. A detailled explanation of how you can get started using Phantom Mochachino is found at the link above.
I am a big fan of PHP, and the site that I built Phantom Mocachino for is written in PHP. As such I wrote a PHP script to run my functional tests with.
I am sharing my implementation to demonstrate how you can use Phantom Mochachino to make functional testing fun.
<?php
$testRunner = "FunctionalTestRunner.js";
$testFiles = array(
'RegisterTest.js' => array(
'paths' => array(
'/account/register'
),
'useCookies' => true,
'argumentsVar' => 'loginArguments'
),
'LoginTest.js' => array(
'paths' => array(
'/account/login'
),
'useCookies' => true,
'argumentsVar' => 'loginArguments'
),
'DoActionsTest.js' => array(
'paths' => array(
'do/actions'
),
'useCookies' => true
),
);
while (true) {
$loginArguments = array(
'username' => substr(md5(rand()), 0, 7), //random username
'password' => 'password'
);
foreach ($testFiles as $testFile => $dataArray) {
echo "\n";
echo ">> PHP TEST RUNNER - NEW FILE \n";
echo ">> RUNNING " . $testFile . "\n" ;
echo "\n";
$testPaths = $dataArray['paths'];
foreach ($testPaths as $path) {
echo "\n";
echo ">> Against " . $path . "\n";
echo "\n";
$commandParts = array();
$commandParts[] = "phantomjs";
if ($dataArray["useCookies"]) {
$commandParts[] = "--cookies-file=cookies.txt";
}
$commandParts[] = $testRunner;
$commandParts[] = $testFile;
$commandParts[] = $path;
if (isset($dataArray['argumentsVar'])) {
$arguments = ${$dataArray['argumentsVar']};
if (count($arguments) > 0) {
foreach ($arguments as $argument) {
$commandParts[] = $argument;
}
}
}
$command = join(" ", $commandParts);
passthru($command, $response);
}
}
echo "\n";
echo ">> Sleeping for 10 seconds";
echo "\n";
sleep(10);
}