diff options
author | wei <> | 2006-07-05 07:30:55 +0000 |
---|---|---|
committer | wei <> | 2006-07-05 07:30:55 +0000 |
commit | b5c7c7b77d33aa3e04ed6c16a489a2076a30f57a (patch) | |
tree | 03f30eb8fec8f1b58b9854058d6338bfad698f98 /test_tools | |
parent | 8d8b6688cbbb1febe92012ccc2a4158fa594fcb3 (diff) |
Move tests around.
Diffstat (limited to 'test_tools')
-rw-r--r-- | test_tools/README.txt | 532 | ||||
-rw-r--r-- | test_tools/functional_tests.php | 66 | ||||
-rw-r--r-- | test_tools/unit_tests.php | 139 |
3 files changed, 737 insertions, 0 deletions
diff --git a/test_tools/README.txt b/test_tools/README.txt new file mode 100644 index 00000000..cfa02abe --- /dev/null +++ b/test_tools/README.txt @@ -0,0 +1,532 @@ +== PRADO Functional Tests == + +Functional tests are browser based that tests the overall functional of a Prado application. The tests can be written in PHP, see "framework/..." within this directory to see some examples. To run the tests, open your browser to "../tests/FunctionalTests/index.php" and click on the "All" button. + + +=== Writing Tests === +Lets test some part of a Prado application. Create a new php file, e.g. + +testExample1.php + +<php> +<?php +class testExample1 extends SeleniumTestCase +{ + function setup() + { + $this->open('../examples/myexample/index.php'); + } + + function testButtonClickExample() + { + //using xpath to find the button with value "Click Me!" + $this->click('//input[@value="Click Me!"]'); + + //..more commands and assertions + } +} +?> +</php> + +=== Tests as part of Example code === +Tests can also be place within the example page, e.g. suppose we have an example call MyButtonExample. + +File: MyButtonExample.php +<php> +<?php +//Example class, changes the Text of a button when clicked. +class MyButtonExample extends TPage +{ + function button_clicked($sender, $param) + { + $sender->Text = "Hello World!"; + } +} + + +class testMyButtonExample extends SeleniumTestCase +{ + function setup() + { + //get the test page url + $page = Prado::getApplication()->getTestPage(__FILE__); + + //open MyButtonExample page + $this->open($page); + } + + function testButtonClick() + { + $this->assertTextPresent("My Button Example"); + $this->click('//input[@value="Click Me!"]'); + $this->click('//input[@value="Hello World!"]'); + } +} +?> +</php> + +File: MyButtonExample.tpl +<prado> +<com:TForm> + <h1>My Button Example</h1> + <com:TButton Text="Click Me!" + Click="button_clicked" /> +</com:TForm> +</prado> + +== Selenium Reference == + +A '''command''' is what tells Selenium what to do. Selenium commands come in two 'flavors', '''Actions''' and '''Assertions'''. Each command call has the following syntax +<php> + $this->[command]([target], [value]); +</php> +Note that some commands does not need a [value]. + +'''Actions''' are commands that generally manipulate the state of the application. They do things like "click this link" and "select that option". If an Action fails, or has an error, the execution of the current test is stopped. + + +'''Checks''' verify the state of the application conforms to what is expected. Examples include "make sure the page title is X" and "check that this checkbox is checked". It is possible to tell Selenium to stop the test when an Assertion fails, or to simply record the failure and continue. + +'''Element Locators''' tell Selenium which HTML element a command refers to. Many commands require an Element Locator as the "target" attribute. Examples of Element Locators include "elementId" and "document.forms[0].element". These are described more clearly in the next section. + +'''Patterns''' are used for various reasons, e.g. to specify the expected value of an input field, or identify a select option. Selenium supports various types of pattern, including regular-expressions, all of which are described in more detail below. + +=== Element Locators === + +Element Locators allow Selenium to identify which HTML element a command refers to. Selenium support the following strategies for locating elements: + +==== '''id='''''id'' ==== +Select the element with the specified @id attribute. + +==== '''name='''''name'' ==== +Select the first element with the specified @name attribute. + +==== '''identifier='''''id''==== +Select the element with the specified @id attribute. If no match is found, select the first element whose @name attribute is ''id''. + +==== '''dom='''''javascriptExpression''==== +Find an element using JavaScript traversal of the HTML Document Object Model. DOM locators ''must'' begin with "document.". +* dom=document.forms['myForm'].myDropdown +* dom=document.images[56] + +==== '''xpath='''''xpathExpression''==== +Locate an element using an XPath expression. XPath locators ''must'' begin with "//". +* xpath=//img[@alt='The image alt text'] +* xpath=//table[@id='table1']//tr[4]/td[2] + +==== '''link='''''textPattern'' ==== +Select the link (anchor) element which contains text matching the specified ''pattern''. +* link=The link text + +Without a locator prefix, Selenium uses: + +* dom, for locators starting with "document." +* xpath, for locators starting with "//" +* identifier, otherwise + + +=== Select Option Specifiers === + +Select Option Specifiers provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Specifier. + +==== label=labelPattern ==== +matches options based on their labels, i.e. the visible text. +* label=regexp:^[Oo]ther + +==== value=valuePattern ==== +matches options based on their values. +* value=other + +==== id=id ==== +matches options based on their ids. +* id=option1 + +==== index=index ==== +matches an option based on its index (offset from zero). +* index=2 + +Without a prefix, the default behaviour is to only match on labels. + +String-match Patterns + + Various Pattern syntaxes are available for matching string values: + + glob:pattern + Match a string against a "glob" (aka "wildmat") pattern. "Glob" is a kind of limited regular-expression syntax typically used in command-line shells. In a glob pattern, "*" represents any sequence of characters, and "?" represents any single character. Glob patterns match against the entire string. + regexp:regexp + Match a string using a regular-expression. The full power of JavaScript regular-expressions is available. + exact:string + Match a string exactly, verbatim, without any of that fancy wildcard stuff. + + If no pattern prefix is specified, Selenium assumes that it's a "glob" pattern. + +Selenium Actions + + Actions tell Selenium to do something in the application. They generally represent something a user would do. + + Many Actions can be called with the "AndWait" suffix. This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load. The exceptions to this pattern are the "open" and "click" actions, which will both wait for a page to load by default. + + open( url ) + + Opens a URL in the test frame. This accepts both relative and absolute URLs. + + Note: The URL must be on the same site as Selenium due to security restrictions in the browser (Cross Site Scripting). + + examples: + + open /mypage + open http://localhost/ + + click( elementLocator ) + + Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), use "clickAndWait". + + examples: + + click aCheckbox + clickAndWait submitButton + clickAndWait anyLink + + note: + Selenium will always automatically click on a popup dialog raised by the alert() or confirm() methods. (The exception is those raised during 'onload', which are not yet handled by Selenium). You must use [verify|assert]Alert or [verify|assert]Confirmation to tell Selenium that you expect the popup dialog. You may use chooseCancelOnNextConfirmation to click 'cancel' on the next confirmation dialog instead of clicking 'OK'. + + type( inputLocator, value ) + + Sets the value of an input field, as though you typed it in. + + Can also be used to set the value of combo boxes, check boxes, etc. In these cases, value should be the value of the option selected, not the visible text. + + examples: + + type nameField John Smith + typeAndWait textBoxThatSubmitsOnChange newValue + + select( dropDownLocator, optionSpecifier ) + + Select an option from a drop-down, based on the optionSpecifier. If more than one option matches the specifier (e.g. due to the use of globs like "f*b*", or due to more than one option having the same label or value), then the first matches is selected. + + examples: + + select dropDown Australian Dollars + select dropDown index=0 + selectAndWait currencySelector value=AUD + selectAndWait currencySelector label=Aus*lian D*rs + + selectWindow( windowId ) + + Selects a popup window. Once a popup window has been selected, all commands go to that window. To select the main window again, use "null" as the target. + + target: The id of the window to select. + + value: ignored + + examples: + + selectWindow myPopupWindow + selectWindow null + + goBack() + + Simulates the user clicking the "back" button on their browser. + + examples: + + goBack + + close() + + Simulates the user clicking the "close" button in the titlebar of a popup window. + + examples: + + close + + pause( milliseconds ) + + Pauses the execution of the test script for a specified amount of time. This is useful for debugging a script or pausing to wait for some server side action. + + examples: + + pause 5000 + pause 2000 + + fireEvent( elementLocator, eventName ) + + Explicitly simulate an event, to trigger the corresponding "onevent" handler. + + examples: + + fireEvent textField focus + fireEvent dropDown blur + + waitForValue( inputLocator, value ) + + Waits for a specified input (e.g. a hidden field) to have a specified value. Will succeed immediately if the input already has the value. This is implemented by polling for the value. Warning: can block indefinitely if the input never has the specified value. + + example: + + waitForValue finishIndication isfinished + + store( valueToStore, variableName ) + + Stores a value into a variable. The value can be constructed using either variable substitution or javascript evaluation, as detailed in 'Parameter construction and Variables' (below). + + examples: + + store Mr John Smith fullname + store ${title} ${firstname} ${surname} fullname + store javascript{Math.round(Math.PI * 100) / 100} PI + + storeValue( inputLocator, variableName ) + + Stores the value of an input field into a variable. + + examples: + + storeValue userName userID + type userName ${userID} + + storeText( elementLocator, variableName ) + + Stores the text of an element into a variable. + + examples: + + storeText currentDate expectedStartDate + verifyValue startDate ${expectedStartDate} + + storeAttribute( elementLocator@attributeName, variableName ) + + Stores the value of an element attribute into a variable. + + examples: + + storeAttribute input1@class classOfInput1 + verifyAttribute input2@class ${classOfInput1} + + chooseCancelOnNextConfirmation() + + Instructs Selenium to click Cancel on the next javascript confirmation dialog to be raised. By default, the confirm function will return true, having the same effect as manually clicking OK. After running this command, the next confirmation will behave as if the user had clicked Cancel. + + examples: + + chooseCancelOnNextConfirmation + + answerOnNextPrompt( answerString ) + + Instructs Selenium to return the specified answerString in response to the next prompt. + + examples: + + answerOnNextPrompt Kangaroo + +Selenium Checks + + Checks are used to verify the state of the application. They can be used to check the value of a form field, the presense of some text, or the URL of the current page. + + All Selenium Checks can be used in 2 modes, "assert" and "verify". These behave identically, except that when an "assert" check fails, the test is aborted. When a "verify" check fails, the test will continue execution. This allows a single "assert" to ensure that the application is on the correct page, followed by a bunch of "verify" checks to test form field values, labels, etc. + + assertLocation( relativeLocation ) + + examples: + + verifyLocation /mypage + assertLocation /mypage + + assertTitle( titlePattern ) + + Verifies the title of the current page. + + examples: + + verifyTitle My Page + assertTitle My Page + + assertValue( inputLocator, valuePattern ) + + Verifies the value of an input field (or anything else with a value parameter). For checkbox/radio elements, the value will be "on" or "off" depending on whether the element is checked or not. + + examples: + + verifyValue nameField John Smith + assertValue document.forms[2].nameField John Smith + + assertSelected( selectLocator, optionSpecifier ) + + Verifies that the selected option of a drop-down satisfies the optionSpecifier. + + examples: + + verifySelected dropdown2 John Smith + verifySelected dropdown2 value=js*123 + assertSelected document.forms[2].dropDown label=J* Smith + assertSelected document.forms[2].dropDown index=0 + + assertSelectOptions( selectLocator, optionLabelList ) + + Verifies the labels of all options in a drop-down against a comma-separated list. Commas in an expected option can be escaped as ",". + + examples: + + verifySelectOptions dropdown2 John Smith,Dave Bird + assertSelectOptions document.forms[2].dropDown Smith\, J,Bird\, D + + assertText( elementLocator, textPattern ) + + Verifies the text of an element. This works for any element that contains text. This command uses either the textContent (Mozilla-like browsers) or the innerText (IE-like browsers) of the element, which is the rendered text shown to the user. + + examples: + + verifyText statusMessage Successful + assertText //div[@id='foo']//h1 Successful + + assertAttribute( elementLocator@attributeName, valuePattern ) + + Verifies the value of an element attribute. + + examples: + + verifyAttribute txt1@class bigAndBold + assertAttribute document.images[0]@alt alt-text + verifyAttribute //img[@id='foo']/@alt alt-text + + assertTextPresent( text ) + + Verifies that the specified text appears somewhere on the rendered page shown to the user. + + examples: + + verifyTextPresent You are now logged in. + assertTextPresent You are now logged in. + + assertTextNotPresent( text ) + + Verifies that the specified text does NOT appear anywhere on the rendered page. + + assertElementPresent( elementLocator ) + + Verifies that the specified element is somewhere on the page. + + examples: + + verifyElementPresent submitButton + assertElementPresent //img[@alt='foo'] + + assertElementNotPresent( elementLocator ) + + Verifies that the specified element is NOT on the page. + + examples: + + verifyElementNotPresent cancelButton + assertElementNotPresent cancelButton + + assertTable( cellAddress, valuePattern ) + + Verifies the text in a cell of a table. The cellAddress syntax tableName.row.column, where row and column start at 0. + + examples: + + verifyTable myTable.1.6 Submitted + assertTable results.0.2 13 + + assertVisible( elementLocator ) + + Verifies that the specified element is both present and visible. An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors. + + examples: + + verifyVisible postcode + assertVisible postcode + + assertNotVisible( elementLocator ) + + Verifies that the specified element is NOT visible. Elements that are simply not present are also considered invisible. + + examples: + + verifyNotVisible postcode + assertNotVisible postcode + + verifyEditable / assertEditable( inputLocator ) + + Verifies that the specified element is editable, ie. it's an input element, and hasn't been disabled. + + examples: + + verifyEditable shape + assertEditable colour + + assertNotEditable( inputLocator ) + + Verifies that the specified element is NOT editable, ie. it's NOT an input element, or has been disabled. + + assertAlert( messagePattern ) + + Verifies that a javascript alert with the specified message was generated. Alerts must be verified in the same order that they were generated. + + Verifying an alert has the same effect as manually clicking OK. If an alert is generated but you do not verify it, the next Selenium action will fail. + + NOTE: under Selenium, javascript alerts will NOT pop up a visible alert dialog. + + NOTE: Selenium does NOT support javascript alerts that are generated in a page's onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until you manually click OK. + + examples: + + verifyAlert Invalid Phone Number + assertAlert Invalid Phone Number + + assertConfirmation( messagePattern ) + + Verifies that a javascript confirmation dialog with the specified message was generated. Like alerts, confirmations must be verified in the same order that they were generated. + + By default, the confirm function will return true, having the same effect as manually clicking OK. This can be changed by prior execution of the chooseCancelOnNextConfirmation command (see above). If an confirmation is generated but you do not verify it, the next Selenium action will fail. + + NOTE: under Selenium, javascript confirmations will NOT pop up a visible dialog. + + NOTE: Selenium does NOT support javascript confirmations that are generated in a page's onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until you manually click OK. + + examples: + + assertConfirmation Remove this user? + verifyConfirmation Are you sure? + + assertPrompt( messagePattern ) + + Verifies that a javascript prompt dialog with the specified message was generated. Like alerts, prompts must be verified in the same order that they were generated. + + Successful handling of the prompt requires prior execution of the answerOnNextPrompt command (see above). If a prompt is generated but you do not verify it, the next Selenium action will fail. + + examples: + + answerOnNextPrompt Joe + click id=delegate + verifyPrompt Delegate to who? + +Parameter construction and Variables + + All Selenium command parameters can be constructed using both simple variable substitution as well as full javascript. Both of these mechanisms can access previously stored variables, but do so using different syntax. + + Stored Variables + + The commands store, storeValue and storeText can be used to store a variable value for later access. Internally, these variables are stored in a map called "storedVars", with values keyed by the variable name. These commands are documented in the command reference. + + Variable substitution + + Variable substitution provides a simple way to include a previously stored variable in a command parameter. This is a simple mechanism, by which the variable to substitute is indicated by ${variableName}. Multiple variables can be substituted, and intermixed with static text. + + Example: + + store Mr title + storeValue nameField surname + store ${title} ${surname} fullname + type textElement Full name is: ${fullname} + + Javascript evaluation + + Javascript evaluation provides the full power of javascript in constructing a command parameter. To use this mechanism, the entire parameter value must be prefixed by 'javascript{' with a trailing '}'. The text inside the braces is evaluated as a javascript expression, and can access previously stored variables using the storedVars map detailed above. Note that variable substitution cannot be combined with javascript evaluation. + + Example: + + store javascript{'merchant' + (new Date()).getTime()} merchantId + type textElement javascript{storedVars['merchantId'].toUpperCase()}
\ No newline at end of file diff --git a/test_tools/functional_tests.php b/test_tools/functional_tests.php new file mode 100644 index 00000000..73bd6226 --- /dev/null +++ b/test_tools/functional_tests.php @@ -0,0 +1,66 @@ +<?php + +$TEST_TOOLS = dirname(__FILE__); + +if(isset($_GET['sr'])) +{ + + if(($selenium_resource=realpath($TEST_TOOLS.'/selenium/'.$_GET['sr']))!==false) + echo file_get_contents($selenium_resource); + exit; +} + +require_once($TEST_TOOLS.'/simpletest/unit_tester.php'); +require_once($TEST_TOOLS.'/simpletest/web_tester.php'); +require_once($TEST_TOOLS.'/simpletest/mock_objects.php'); +require_once($TEST_TOOLS.'/simpletest/reporter.php'); +require_once($TEST_TOOLS.'/selenium/php/selenium.php'); + +class PradoFunctionalTester +{ + private $_name; + private $_basePath; + private $_selenium; + + public function __construct($basePath,$selenium='',$name='All Tests') + { + $this->_name=$name; + if($basePath==='' || ($this->_basePath=realpath($basePath))===false) + throw new Exception('Invalid base path '.$basePath); + $this->_basePath=strtr($this->_basePath,'\\','/'); + + $this->_selenium = $selenium.'selenium/'; + } + + public function run($simpleReporter) + { + $server=SimpleSeleniumProxyServer::getInstance(dirname(__FILE__));//, '', $this->_selenium); + + $groupTest=new GroupTest($this->_name); + $this->collectTestFiles($groupTest,$this->_basePath); + $groupTest->run($simpleReporter); + + $server->handleRequest(); + } + + protected function collectTestFiles($groupTest,$basePath) + { + $folder=@opendir($basePath); + while($entry=@readdir($folder)) + { + $fullPath=strtr($basePath.'/'.$entry,'\\','/'); + if(is_file($fullPath) && $this->isValidFile($entry)) + $groupTest->addTestFile($fullPath); + else if($entry[0]!=='.') + $this->collectTestFiles($groupTest,$fullPath); + } + @closedir($folder); + } + + protected function isValidFile($entry) + { + return preg_match('/\w+\.php$/',$entry); + } +} + +?>
\ No newline at end of file diff --git a/test_tools/unit_tests.php b/test_tools/unit_tests.php new file mode 100644 index 00000000..b714f452 --- /dev/null +++ b/test_tools/unit_tests.php @@ -0,0 +1,139 @@ +<?php + +if(!defined('PRADO_FRAMEWORK')) + define('PRADO_FRAMEWORK',realpath(dirname(__FILE__).'/../framework')); + +$TEST_TOOLS = dirname(__FILE__); + +require_once($TEST_TOOLS.'/simpletest/unit_tester.php'); +require_once($TEST_TOOLS.'/simpletest/web_tester.php'); +require_once($TEST_TOOLS.'/simpletest/mock_objects.php'); +require_once($TEST_TOOLS.'/simpletest/reporter.php'); + +require_once(PRADO_FRAMEWORK.'/prado.php'); + +//set_include_path(get_include_path().";".PRADO_FRAMEWORK); + +//error_reporting(E_ALL); +//restore_error_handler(); + +class TMockApplication extends TApplication +{ + public function run() + { + $this->initApplication(); + } +} + +class TestFolder +{ + public $name=''; + public $url=''; + public $subFolders=array(); + public $testFiles=array(); + + public function __construct($path,$rootPath,$rootUri) + { + $script = basename($_SERVER['SCRIPT_NAME']); + $this->url="$rootUri/$script?target=".strtr(substr($path,strlen($rootPath)+1),"\\",'/'); + $this->name=basename($path); + $dir=opendir($path); + while(($entry=readdir($dir))!==false) + { + $fullpath="$path/$entry"; + if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($fullpath)) + { + $folder=new TestFolder($fullpath,$rootPath,$rootUri); + if(!empty($folder->subFolders) || !empty($folder->testFiles)) + $this->subFolders[]=$folder; + } + else if(is_file($fullpath) && (strncmp($entry,'ut',2)===0 + || preg_match('/test.*\.php/', strtolower($entry)))) + { + $this->testFiles[$entry]="$rootUri/$script?target=".strtr(substr($fullpath,strlen($rootPath)+1),"\\",'/'); + } + } + closedir($dir); + } + + public function getHtml($level=0) + { + $str=str_repeat(' ',$level*4)."[ <a href=\"{$this->url}\">{$this->name}</a> ]<br/>\n"; + foreach($this->subFolders as $folder) + $str.=$folder->getHtml($level+1); + foreach($this->testFiles as $name=>$url) + $str.=str_repeat(' ',($level+1)*4)."<a href=\"$url\">$name</a><br/>\n"; + return $str; + } +} + +class PradoUnitTester +{ + private $_root; + + function __construct($root, $app_dir) + { + $this->_root = $root; + $app = new TMockApplication($app_dir); + $app->run(); + } + + function addTests($test,$path,$recursive) + { + $dir=opendir($path); + while(($entry=readdir($dir))!==false) + { + if(is_file($path.'/'.$entry) && (strncmp($entry,'ut',2)===0||preg_match('/test.*\.php/', strtolower($entry)))) + $test->addTestFile($path.'/'.$entry); + else if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive) + $this->addTests($test,$path.'/'.$entry,$recursive); + } + closedir($dir); + } + + function run($reporter) + { + $rootPath=$this->_root; + $rootUri=dirname($_SERVER['PHP_SELF']); + if(isset($_GET['target'])) + { + $target=$_GET['target']; + $recursive=true; + $fullpath=realpath("$rootPath/$target"); + if($fullpath===false || strpos($fullpath,$rootPath)!==0) + die('invalid test target'); + + if(is_dir($fullpath)) + { + + $test=new GroupTest(basename($rootPath)."/$target"); + $this->addTests($test,$fullpath,$recursive); + $test->run($reporter); + //$test->run(new HtmlReporterWithCoverage('index.php',$rootPath)); + } + else + { + $testClass=basename($fullpath,'.php'); + include_once($fullpath); + $test=new $testClass(basename($rootPath)."/$target"); + $test->run($reporter); + } + } + else + { + echo "<html> + <head> + <title>Prado Unit Tests</title> + <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> + </head> + <body> + <h1>Prado Unit Tests</h1> + "; + $root=new TestFolder($rootPath,$rootPath,$rootUri); + echo $root->getHtml(); + echo "</body>\n</html>"; + } + } +} + +?>
\ No newline at end of file |