Source for file selenese_tester.php

Documentation is available at selenese_tester.php

  1. <?php
  2. /**
  3.  *    @package    SimpleTest
  4.  *    @subpackage    Extensions
  5.  *    @version    $Id: selenese_tester.php 1802 2008-09-08 10:43:58Z maetl_ $
  6.  */
  7.  
  8. /**#@+
  9.  * include SimpleTest files
  10.  */
  11. require_once dirname(__FILE__).'/../unit_tester.php';
  12. require_once dirname(__FILE__).'/selenium/remote-control.php';
  13. /**#@-*/
  14.  
  15. /**
  16.  * SeleneseTestCase
  17.  * 
  18.  * Integrate selenese html test suite support (can be generated by selenium-IDE)
  19.  * 
  20.  * @category Testing
  21.  * @package SimpleTest
  22.  * @subpackage Extensions
  23.  * @author Guidet Alexandre <alwex@free.fr>
  24.  * @param simpleSelenium $selenium 
  25.  * @param string $html 
  26.  * @param  string $testFile 
  27.  * @param array $parsed_table 
  28.  * @param string $logMessages 
  29.  * @param array $_commandMap 
  30.  * 
  31.  */
  32. class SeleneseTestCase extends UnitTestCase {
  33.     var $selenium;
  34.     var $html;
  35.     var $testFile;
  36.     var $parsed_table;
  37.     var $logMessages;
  38.     var $_commandMap = array("verify",
  39.                             "verifyErrorOnNext",
  40.                             "verifyNotErrorOnNext",
  41.                             "verifyFailureOnNext",
  42.                             "verifyNotFailureOnNext",
  43.                             "verifySelected",
  44.                             "verifyNotSelected",
  45.                             "verifyAlert",
  46.                             "verifyNotAlert",
  47.                             "verifyAllButtons",
  48.                             "verifyNotAllButtons",
  49.                             "verifyAllFields",
  50.                             "verifyNotAllFields",
  51.                             "verifyAllLinks",
  52.                             "verifyNotAllLinks",
  53.                             "verifyAllWindowIds",
  54.                             "verifyNotAllWindowIds",
  55.                             "verifyAllWindowNames",
  56.                             "verifyNotAllWindowNames",
  57.                             "verifyAllWindowTitles",
  58.                             "verifyNotAllWindowTitles",
  59.                             "verifyAttribute",
  60.                             "verifyNotAttribute",
  61.                             "verifyAttributeFromAllWindows",
  62.                             "verifyNotAttributeFromAllWindows",
  63.                             "verifyBodyText",
  64.                             "verifyNotBodyText",
  65.                             "verifyConfirmation",
  66.                             "verifyNotConfirmation",
  67.                             "verifyCookie",
  68.                             "verifyNotCookie",
  69.                             "verifyCursorPosition",
  70.                             "verifyNotCursorPosition",
  71.                             "verifyElementHeight",
  72.                             "verifyNotElementHeight",
  73.                             "verifyElementIndex",
  74.                             "verifyNotElementIndex",
  75.                             "verifyElementPositionLeft",
  76.                             "verifyNotElementPositionLeft",
  77.                             "verifyElementPositionTop",
  78.                             "verifyNotElementPositionTop",
  79.                             "verifyElementWidth",
  80.                             "verifyNotElementWidth",
  81.                             "verifyEval",
  82.                             "verifyNotEval",
  83.                             "verifyExpression",
  84.                             "verifyNotExpression",
  85.                             "verifyHtmlSource",
  86.                             "verifyNotHtmlSource",
  87.                             "verifyLocation",
  88.                             "verifyNotLocation",
  89.                             "verifyLogMessages",
  90.                             "verifyNotLogMessages",
  91.                             "verifyMouseSpeed",
  92.                             "verifyNotMouseSpeed",
  93.                             "verifyPrompt",
  94.                             "verifyNotPrompt",
  95.                             "verifySelectedId",
  96.                             "verifyNotSelectedId",
  97.                             "verifySelectedIds",
  98.                             "verifyNotSelectedIds",
  99.                             "verifySelectedIndex",
  100.                             "verifyNotSelectedIndex",
  101.                             "verifySelectedIndexes",
  102.                             "verifyNotSelectedIndexes",
  103.                             "verifySelectedLabel",
  104.                             "verifyNotSelectedLabel",
  105.                             "verifySelectedLabels",
  106.                             "verifyNotSelectedLabels",
  107.                             "verifySelectedValue",
  108.                             "verifyNotSelectedValue",
  109.                             "verifySelectedValues",
  110.                             "verifyNotSelectedValues",
  111.                             "verifySelectOptions",
  112.                             "verifyNotSelectOptions",
  113.                             "verifyTable",
  114.                             "verifyNotTable",
  115.                             "verifyText",
  116.                             "verifyNotText",
  117.                             "verifyTitle",
  118.                             "verifyNotTitle",
  119.                             "verifyValue",
  120.                             "verifyNotValue",
  121.                             "verifyWhetherThisFrameMatchFrameExpression",
  122.                             "verifyNotWhetherThisFrameMatchFrameExpression",
  123.                             "verifyWhetherThisWindowMatchWindowExpression",
  124.                             "verifyNotWhetherThisWindowMatchWindowExpression",
  125.                             "verifyAlertPresent",
  126.                             "verifyAlertNotPresent",
  127.                             "verifyChecked",
  128.                             "verifyNotChecked",
  129.                             "verifyConfirmationPresent",
  130.                             "verifyConfirmationNotPresent",
  131.                             "verifyEditable",
  132.                             "verifyNotEditable",
  133.                             "verifyElementPresent",
  134.                             "verifyElementNotPresent",
  135.                             "verifyOrdered",
  136.                             "verifyNotOrdered",
  137.                             "verifyPromptPresent",
  138.                             "verifyPromptNotPresent",
  139.                             "verifySomethingSelected",
  140.                             "verifyNotSomethingSelected",
  141.                             "verifyTextPresent",
  142.                             "verifyTextNotPresent",
  143.                             "verifyVisible",
  144.                             "verifyNotVisible",
  145.                             "assert",
  146.                             "assertErrorOnNext",
  147.                             "assertNotErrorOnNext",
  148.                             "assertFailureOnNext",
  149.                             "assertNotFailureOnNext",
  150.                             "assertSelected",
  151.                             "assertNotSelected",
  152.                             "assertAlert",
  153.                             "assertNotAlert",
  154.                             "assertAllButtons",
  155.                             "assertNotAllButtons",
  156.                             "assertAllFields",
  157.                             "assertNotAllFields",
  158.                             "assertAllLinks",
  159.                             "assertNotAllLinks",
  160.                             "assertAllWindowIds",
  161.                             "assertNotAllWindowIds",
  162.                             "assertAllWindowNames",
  163.                             "assertNotAllWindowNames",
  164.                             "assertAllWindowTitles",
  165.                             "assertNotAllWindowTitles",
  166.                             "assertAttribute",
  167.                             "assertNotAttribute",
  168.                             "assertAttributeFromAllWindows",
  169.                             "assertNotAttributeFromAllWindows",
  170.                             "assertBodyText",
  171.                             "assertNotBodyText",
  172.                             "assertConfirmation",
  173.                             "assertNotConfirmation",
  174.                             "assertCookie",
  175.                             "assertNotCookie",
  176.                             "assertCursorPosition",
  177.                             "assertNotCursorPosition",
  178.                             "assertElementHeight",
  179.                             "assertNotElementHeight",
  180.                             "assertElementIndex",
  181.                             "assertNotElementIndex",
  182.                             "assertElementPositionLeft",
  183.                             "assertNotElementPositionLeft",
  184.                             "assertElementPositionTop",
  185.                             "assertNotElementPositionTop",
  186.                             "assertElementWidth",
  187.                             "assertNotElementWidth",
  188.                             "assertEval",
  189.                             "assertNotEval",
  190.                             "assertExpression",
  191.                             "assertNotExpression",
  192.                             "assertHtmlSource",
  193.                             "assertNotHtmlSource",
  194.                             "assertLocation",
  195.                             "assertNotLocation",
  196.                             "assertLogMessages",
  197.                             "assertNotLogMessages",
  198.                             "assertMouseSpeed",
  199.                             "assertNotMouseSpeed",
  200.                             "assertPrompt",
  201.                             "assertNotPrompt",
  202.                             "assertSelectedId",
  203.                             "assertNotSelectedId",
  204.                             "assertSelectedIds",
  205.                             "assertNotSelectedIds",
  206.                             "assertSelectedIndex",
  207.                             "assertNotSelectedIndex",
  208.                             "assertSelectedIndexes",
  209.                             "assertNotSelectedIndexes",
  210.                             "assertSelectedLabel",
  211.                             "assertNotSelectedLabel",
  212.                             "assertSelectedLabels",
  213.                             "assertNotSelectedLabels",
  214.                             "assertSelectedValue",
  215.                             "assertNotSelectedValue",
  216.                             "assertSelectedValues",
  217.                             "assertNotSelectedValues",
  218.                             "assertSelectOptions",
  219.                             "assertNotSelectOptions",
  220.                             "assertTable",
  221.                             "assertNotTable",
  222.                             "assertText",
  223.                             "assertNotText",
  224.                             "assertTitle",
  225.                             "assertNotTitle",
  226.                             "assertValue",
  227.                             "assertNotValue",
  228.                             "assertWhetherThisFrameMatchFrameExpression",
  229.                             "assertNotWhetherThisFrameMatchFrameExpression",
  230.                             "assertWhetherThisWindowMatchWindowExpression",
  231.                             "assertNotWhetherThisWindowMatchWindowExpression",
  232.                             "assertAlertPresent",
  233.                             "assertAlertNotPresent",
  234.                             "assertChecked",
  235.                             "assertNotChecked",
  236.                             "assertConfirmationPresent",
  237.                             "assertConfirmationNotPresent",
  238.                             "assertEditable",
  239.                             "assertNotEditable",
  240.                             "assertElementPresent",
  241.                             "assertElementNotPresent",
  242.                             "assertOrdered",
  243.                             "assertNotOrdered",
  244.                             "assertPromptPresent",
  245.                             "assertPromptNotPresent",
  246.                             "assertSomethingSelected",
  247.                             "assertNotSomethingSelected",
  248.                             "assertTextPresent",
  249.                             "assertTextNotPresent",
  250.                             "assertVisible",
  251.                             "assertNotVisible");
  252.     
  253.     /**
  254.      * constructor
  255.      * 
  256.      * Construct the object with the specified browser and url
  257.      * 
  258.      * @param string $browser 
  259.      * @param string $url 
  260.      */
  261.     function __construct($browser$url{
  262.         $this->selenium = new SimpleSeleniumRemoteControl($browser$url);
  263.         $this->parsed_table = array();
  264.     }
  265.     
  266.     /**
  267.      * tidy
  268.      * 
  269.      * Reformat the selenium-IDE html test suites
  270.      */
  271.     function tidy({
  272.         $tmp $this->html;
  273.         preg_match('/<meta.*>/'$tmp$matche);    
  274.         $matche[0str_replace("/>"">"$matche[0]);
  275.         $matche[0str_replace(">""/>"$matche[0]);
  276.         $tmp preg_replace('/<meta.*>/'$matche[0]$tmp);
  277.         $this->html = $tmp;
  278.     }
  279.  
  280.     /**
  281.      * parse
  282.      * 
  283.      * Extract the called selenium fonction from the html suite
  284.      */
  285.     function parse({
  286.         $parsedTab array();
  287.         $key1 0;
  288.     
  289.         $contenthtml new DOMDocument;
  290.         @$contenthtml->loadHtml($this->html);
  291.         $content simplexml_import_dom($contenthtml);
  292.         foreach ($content->body->table->tbody->tr as $tr){
  293.             $key2 0;
  294.             foreach ($tr->td as $td){
  295.                 $parsedTab[$key1][$key2$td;
  296.                 $key2++;
  297.             }
  298.             $key1++;
  299.         }
  300.  
  301.         $this->parsed_table = $parsedTab;
  302.     }
  303.  
  304.     /**
  305.      * assertFunction
  306.      * 
  307.      * Integrate selenium fonctions in simpletest
  308.      * 
  309.      * @param string $function 
  310.      * @param string $param1 
  311.      * @param string $param2 
  312.      * 
  313.      */
  314.     function assertFunction($function$param1$param2{
  315.         $_verifyMap array('verify''verifyTextPresent''verifyTextNotPresent''verifyValue');
  316.         
  317.         $reponse $this->selenium->__call($functionarray($param1$param2));
  318.         
  319.         $message $reponse;
  320.         $message .= " using command '".$function ."' with target '".$param1."'";
  321.         if (!empty($param2)) {
  322.             $message .= " and value '".$param2."'";
  323.         }
  324.         $message .= " in file '".$this->testFile."'";
  325.  
  326.         if (!in_array($function$_verifyMap)) {
  327.             $reponse substr($reponse02== 'OK' true false;
  328.         }
  329.         
  330.         $this->assertTrue($reponse$message);
  331.     }
  332.  
  333.     /**
  334.      * launch
  335.      * 
  336.      * Launch the html test suite from a PHP variable on the url declared wihle
  337.      * constructing the object. The filename is used to localize the error.
  338.      * 
  339.      * @param string $testFile 
  340.      * @param string $filename 
  341.      * 
  342.      */
  343.     function launch($html=""{
  344.         $this->html = $html;
  345.         $this->tidy();
  346.         $this->parse();
  347.         
  348.         $this->selenium->start();
  349.         foreach ($this->parsed_table as $test{    
  350.             if (in_array($test[0]$this->_commandMap)) {
  351.                 $this->assertFunction($test[0]$test[1]$test[2]);
  352.             else {        
  353.                 $this->selenium->__call($test[0]array($test[1]$test[2]));
  354.             }
  355.         }
  356.         $this->selenium->stop();
  357.     }
  358.     
  359.     /**
  360.      * launchPhpFile
  361.      * 
  362.      * Parse the PHP file then launch the computed test suite
  363.      * 
  364.      * @param string $file 
  365.      * 
  366.      */
  367.     function launchPhpFile($file{
  368.         ob_start();
  369.         require($file);
  370.         $data ob_get_contents();
  371.         ob_end_clean();
  372.         
  373.         $this->testFile = $file;
  374.         $this->html = $data;
  375.         $this->launch($this->html);
  376.     }
  377.     
  378.     /**
  379.      * launchFile
  380.      * 
  381.      * Launch the html test suite file on the url declared wihle constructing the object
  382.      * 
  383.      * @param string $testFile 
  384.      * 
  385.      */
  386.     function launchFile($testFile{
  387.         $this->testFile = $testFile
  388.         $this->html = file_get_contents($testFile);
  389.         $this->launch($this->html);
  390.     }
  391. }
  392.  
  393. ?>

Documentation generated on Thu, 01 Oct 2009 20:55:45 -0500 by phpDocumentor 1.4.2