Difference between revisions of "SpecialSelenium.php"

From Organic Design wiki
m
(this is now up and running the new way)
Line 24: Line 24:
  
 
# Process suite or test if ?suite=article_name in query string
 
# Process suite or test if ?suite=article_name in query string
if ($wgRequest->getText('suite')) {
+
if ($suite = $wgRequest->getText('suite')) {
  
# Grab article $param and process contents
+
# Get the content of the suite article and parse it
$suiteTitle = Title::newFromText($wgRequest->getText('suite'));
+
$suiteTitle = Title::newFromText($suite);
 +
$suiteArticle = new Article($suiteTitle);
 +
$text = $wgParser->parse($suiteArticle->getContent(), $suiteTitle, new ParserOptions(), true, true)->getText();
  
# get the content of the suite article and expand braces
+
# Extract the tests from the expanded content and  
$suiteArticle = new Article($suiteTitle);
+
$numTests = preg_match_all(
$wikitext = $wgParser->preprocess($suiteArticle->getContent(), $suiteTitle, new ParserOptions());
+
"|<div class=\"selenium\" id=\"(.+?)\">\\s*(.+?)\\s*</div><!-- selenium end -->|is",
 +
$text,
 +
$matches,
 +
PREG_PATTERN_ORDER
 +
);
  
# Preparse encapsulations around Selenium tags e.g. <nowiki><selenium></nowiki>
+
# If any tests were extracted, build content and return it to the client
$wikitext = preg_replace("|<nowiki></?selenium></nowiki>|", "<nowiki><selenium></nowiki>", $wikitext);
+
if ($numTests > 0) {
  
$matches = array();
+
# Re-organise the matched test content into a hash indexed by test name
if (!$this->articleRegex($wikitext, $matches)) {
+
for ($i = 0; $i < $numTests; $i++) $tests[$matches[1][$i]] = $matches[2][$i];
$this -> build404('Specified suite contains no selenium test tags');
 
}
 
else {
 
  
# Determine whether suite or test from ?suite=article_name&test=section_num in query string     
+
# If a test is specified, return it from the suite article content in raw HTML Selenium test format
if ($wgRequest->getText('test')) {
+
if ($test = $wgRequest->getText('test')) {
$testTitle = Title::newFromText($wgRequest->getText('test'));
+
if (array_key_exists($test, $tests)) print $tests[$test];
if (in_array($testTitle->getText(), $matches[1])) {
 
$counter = 0;
 
foreach ($matches[1] as $value) {
 
if ($value == $testTitle->getText()) {
 
# Generating test html
 
$matches[2][$counter] = preg_replace(
 
'|.+?<selenium>\n?(.+?)</selenium>|ms',
 
"$1",
 
$matches[2][$counter]
 
);
 
print $matches[2][$counter];
 
break; #exit foreach loop when test is found
 
}
 
$counter++;      
 
}
 
}
 
else $this->build404("Specified test does not exist in ".$wgRequest->getText('suite'));
 
 
}
 
}
 +
 +
# Or if no test specified, return the whole suite in raw HTML Selenium test-suite format
 
else {
 
else {
# Obtain suite section headers
+
$html = "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">
$suite_urls = array();
+
<title>Selenium</title></head><body><table cellpadding=\"1\" cellspacing=\"1\" border=\"1\">
foreach ($matches[1] as $value) array_push($suite_urls, $value);
+
<tbody><tr><td><b>$suite</b></td></tr>\n";
print $this->buildSuite($suite_urls, $wgRequest->getText('suite'));
+
$title = Title::makeTitle(NS_SPECIAL, 'Selenium');
 +
foreach (array_keys($tests) as $test)
 +
$html .= "<tr><td><a href=\"".$title->getLocalURL("suite=$suite&test=$test")."\">$test</a></td></tr>\n";
 +
print "$html</tbody></table></body></html>";
 
}
 
}
 
}
 
}
 
}
 
}
+
 
 +
# No suite or test specified, render the main special page and form
 
else {
 
else {
global $egSeleniumCategory, $egSeleniumPath;
+
global $egSeleniumCategory, $egSeleniumPath, $egResultsUrlPath;
 +
$this->setHeaders();
 +
$title = Title::makeTitle(NS_SPECIAL, 'Selenium');
  
# Retrieve articles in category labelled $egSeleniumCategory
+
# Render section for running tests manually with a select list of all the suites
$list = array();
+
$wgOut->addWikitext('=='.wfMsg('selenium-runmanual').'==');
$dbr  = &wfGetDB(DB_SLAVE);
 
$cl  = $dbr->tableName('categorylinks');
 
$res  = $dbr->select($cl, 'cl_from', "cl_to = '$egSeleniumCategory'", __METHOD__, array('ORDER BY' => 'cl_sortkey'));
 
while ($row = $dbr->fetchRow($res)) $list[] = Title::newFromID($row[0])->getPrefixedText();
 
  
# Grab article $param and process contents
+
# Retrieve suites from Selenium category (only selenium tests should be in this cat)
 
$suites = array();
 
$suites = array();
foreach ($list as $articleName) {
+
$dbr = &wfGetDB(DB_SLAVE);
$suiteTitle = Title::newFromText($articleName);
+
$cl  = $dbr->tableName('categorylinks');
$suiteArticle = new Article($suiteTitle);
+
$res = $dbr->select($cl, 'cl_from', "cl_to = '$egSeleniumCategory'", __METHOD__, array('ORDER BY' => 'cl_sortkey'));
if ($this->articleRegex($suiteTitle->getContent(), $matches)) $suites[] = $articleName;
+
while ($row = $dbr->fetchRow($res)) $suites[] = Title::newFromID($row[0])->getPrefixedText();
}
 
 
$selectTag = '<select name="selenium">';
 
foreach ($suites as $suite) $selectTag .= "<option>$suite</option>";
 
$selectTag .= '</select>';
 
 
 
$this->setHeaders();
 
$title = Title::makeTitle(NS_SPECIAL, 'Selenium');
 
 
 
$path = "$egSeleniumPath?test=".urlencode($title->getLocalURL());
 
  
 
$form  = wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null);
 
$form  = wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null);
$form .= '<fieldset><legend>Select a suite to run</legend>';
+
$form .= "<fieldset><legend>".wfMsg('selenium-select')."</legend>";
$form .= "$selectTag";
+
$form .= "<select name=\"selenium\"><option>".join("</option>\n<option>", $suites)."</option></select>";
 
$form .=  wfElement('input', array('type' => 'submit', value => 'Go'));
 
$form .=  wfElement('input', array('type' => 'submit', value => 'Go'));
$form .= "<br /><br />" . wfCheckLabel("Run automatically", "wfRunAuto" , "wfRunAuto", $checked = false);
+
$form .= "<br /><br />".wfCheckLabel(wfMsg('selenium-autorun'), 'wfRunAuto' , 'wfRunAuto', $checked = false);
$form .= '</fieldset></form>';
+
$form .= '</fieldset></form>';
 
 
 
$wgOut->addHTML($form);
 
$wgOut->addHTML($form);
  
if ($wgRequest->getText('action')) {
+
# Render link to run test
# foreach($_REQUEST as $key=>$value) {
+
$selenium = $wgRequest->getText('selenium');
#   print "$key>$value" . "<br />";
+
$path = "$egSeleniumPath/core/TestRunner.html?test=".urlencode($title->getLocalURL("suite=$selenium"));
}
 
$path .= urlencode($wgRequest->getText('selenium'));
 
 
 
 
if ($wgRequest->getBool('wfRunAuto')) $path .= "&auto=true&resultsUrl=$egResultsUrlPath";
 
if ($wgRequest->getBool('wfRunAuto')) $path .= "&auto=true&resultsUrl=$egResultsUrlPath";
 +
$wgOut->addHTML("<fieldset><legend>".wfMsg('selenium-runcore')."</legend><a href=\"$path\">$selenium</a></fieldset>");
  
$form_link  = '<fieldset><legend>Click link to run selenium-core</legend>';
+
$wgOut->addWikitext('=='.wfMsg('selenium-manage').'==');
$form_link .= "<a href=\"$path\">" . $wgRequest->getText('selenium') . '</a></fieldset>';
 
$wgOut->addHTML($form_link);
 
}
 
}
 
 
 
/**
 
* Bypass $wgOut -must be called early to avoid conflict with other extensions
 
*/
 
static function disableHTML(&$wgOut) {
 
$wgOut->disable();
 
wfResetOutputBuffers();
 
return;
 
}
 
  
/**
+
$wgOut->addWikitext('=='.wfMsg('selenium-create').'==');
* Generate 404 error
 
*/
 
private function build404($message) {
 
header('HTTP/1.0 404 not Found');
 
$err = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Error</title></head>
 
<body><h1>$message</h1><p>The requested URL was not found on this server.</p></body></html>";
 
print $err;
 
return;
 
}
 
  
/**
 
* Build a suite from an array of tests
 
*/
 
private function buildSuite($tests, $article) {
 
$html = "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">
 
<title>Selenium</title></head><body><table cellpadding=\"1\" cellspacing=\"1\" border=\"1\"><tbody>
 
<tr><td><b>$article</b></td></tr>";
 
$title = Title::makeTitle(NS_SPECIAL, 'Selenium');
 
foreach ($tests as $element) {
 
$url = $title->getLocalURL("suite=$article&test=$element");
 
$html .= "\n<tr><td><a href=\"$url\">$element</a></td></tr>";
 
 
}
 
}
return "$html\n</tbody></table></body></html>";
 
 
}
 
}
 
}
 
}

Revision as of 04:15, 2 August 2008

<?php /**

* Selenium extension
* Template:Php
* See http://www.mediawiki.org/Extension:Selenium for installation and usage details
* See http://www.organicdesign.co.nz/Extension_talk:Selenium.php for development notes and disucssion
* 
* @package MediaWiki
* @subpackage Extensions
* @author Marcus Davy User:Sven
* @copyright © 2007 Marcus Davy
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die();

class SpecialSelenium extends SpecialPage {

function __construct() { SpecialPage::SpecialPage('Selenium', , true, false, false, false); }

public function execute() { global $wgOut, $wgParser, $wgRequest, $egResultsUrlPath;

# Process suite or test if ?suite=article_name in query string if ($suite = $wgRequest->getText('suite')) {

# Get the content of the suite article and parse it $suiteTitle = Title::newFromText($suite); $suiteArticle = new Article($suiteTitle); $text = $wgParser->parse($suiteArticle->getContent(), $suiteTitle, new ParserOptions(), true, true)->getText();

# Extract the tests from the expanded content and $numTests = preg_match_all(

"|

\\s*(.+?)\\s*

|is",

$text, $matches, PREG_PATTERN_ORDER );

# If any tests were extracted, build content and return it to the client if ($numTests > 0) {

# Re-organise the matched test content into a hash indexed by test name for ($i = 0; $i < $numTests; $i++) $tests[$matches[1][$i]] = $matches[2][$i];

# If a test is specified, return it from the suite article content in raw HTML Selenium test format if ($test = $wgRequest->getText('test')) { if (array_key_exists($test, $tests)) print $tests[$test]; }

# Or if no test specified, return the whole suite in raw HTML Selenium test-suite format else { $html = " Selenium

\n"; $title = Title::makeTitle(NS_SPECIAL, 'Selenium'); foreach (array_keys($tests) as $test) $html .= "\n"; print "$html
$suite
getLocalURL("suite=$suite&test=$test")."\">$test

"; } } }

# No suite or test specified, render the main special page and form else { global $egSeleniumCategory, $egSeleniumPath, $egResultsUrlPath; $this->setHeaders(); $title = Title::makeTitle(NS_SPECIAL, 'Selenium');

# Render section for running tests manually with a select list of all the suites $wgOut->addWikitext('=='.wfMsg('selenium-runmanual').'==');

# Retrieve suites from Selenium category (only selenium tests should be in this cat) $suites = array(); $dbr = &wfGetDB(DB_SLAVE); $cl = $dbr->tableName('categorylinks'); $res = $dbr->select($cl, 'cl_from', "cl_to = '$egSeleniumCategory'", __METHOD__, array('ORDER BY' => 'cl_sortkey')); while ($row = $dbr->fetchRow($res)) $suites[] = Title::newFromID($row[0])->getPrefixedText();

$form = wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null); $form .= "<fieldset><legend>".wfMsg('selenium-select')."</legend>"; $form .= "<select name=\"selenium\"><option>".join("</option>\n<option>", $suites)."</option></select>"; $form .= wfElement('input', array('type' => 'submit', value => 'Go')); $form .= "

".wfCheckLabel(wfMsg('selenium-autorun'), 'wfRunAuto' , 'wfRunAuto', $checked = false); $form .= '</fieldset></form>'; $wgOut->addHTML($form);

# Render link to run test $selenium = $wgRequest->getText('selenium'); $path = "$egSeleniumPath/core/TestRunner.html?test=".urlencode($title->getLocalURL("suite=$selenium")); if ($wgRequest->getBool('wfRunAuto')) $path .= "&auto=true&resultsUrl=$egResultsUrlPath"; $wgOut->addHTML("<fieldset><legend>".wfMsg('selenium-runcore')."</legend><a href=\"$path\">$selenium</a></fieldset>");

$wgOut->addWikitext('=='.wfMsg('selenium-manage').'==');

$wgOut->addWikitext('=='.wfMsg('selenium-create').'==');

} } }