[ Index ] |
|
Code source de PRADO 3.0.6 |
1 == PRADO Functional Tests == 2 3 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. 4 5 6 === Writing Tests === 7 Lets test some part of a Prado application. Create a new php file, e.g. 8 9 testExample1.php 10 11 <php> 12 <?php 13 class testExample1 extends SeleniumTestCase 14 { 15 function setup() 16 { 17 $this->open('../examples/myexample/index.php'); 18 } 19 20 function testButtonClickExample() 21 { 22 //using xpath to find the button with value "Click Me!" 23 $this->click('//input[@value="Click Me!"]'); 24 25 //..more commands and assertions 26 } 27 } 28 ?> 29 </php> 30 31 === Tests as part of Example code === 32 Tests can also be place within the example page, e.g. suppose we have an example call MyButtonExample. 33 34 File: MyButtonExample.php 35 <php> 36 <?php 37 //Example class, changes the Text of a button when clicked. 38 class MyButtonExample extends TPage 39 { 40 function button_clicked($sender, $param) 41 { 42 $sender->Text = "Hello World!"; 43 } 44 } 45 46 47 class testMyButtonExample extends SeleniumTestCase 48 { 49 function setup() 50 { 51 //get the test page url 52 $page = Prado::getApplication()->getTestPage(__FILE__); 53 54 //open MyButtonExample page 55 $this->open($page); 56 } 57 58 function testButtonClick() 59 { 60 $this->assertTextPresent("My Button Example"); 61 $this->click('//input[@value="Click Me!"]'); 62 $this->click('//input[@value="Hello World!"]'); 63 } 64 } 65 ?> 66 </php> 67 68 File: MyButtonExample.tpl 69 <prado> 70 <com:TForm> 71 <h1>My Button Example</h1> 72 <com:TButton Text="Click Me!" 73 Click="button_clicked" /> 74 </com:TForm> 75 </prado> 76 77 == Selenium Reference == 78 79 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 80 <php> 81 $this->[command]([target], [value]); 82 </php> 83 Note that some commands does not need a [value]. 84 85 '''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. 86 87 88 '''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. 89 90 '''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. 91 92 '''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. 93 94 === Element Locators === 95 96 Element Locators allow Selenium to identify which HTML element a command refers to. Selenium support the following strategies for locating elements: 97 98 ==== '''id='''''id'' ==== 99 Select the element with the specified @id attribute. 100 101 ==== '''name='''''name'' ==== 102 Select the first element with the specified @name attribute. 103 104 ==== '''identifier='''''id''==== 105 Select the element with the specified @id attribute. If no match is found, select the first element whose @name attribute is ''id''. 106 107 ==== '''dom='''''javascriptExpression''==== 108 Find an element using JavaScript traversal of the HTML Document Object Model. DOM locators ''must'' begin with "document.". 109 * dom=document.forms['myForm'].myDropdown 110 * dom=document.images[56] 111 112 ==== '''xpath='''''xpathExpression''==== 113 Locate an element using an XPath expression. XPath locators ''must'' begin with "//". 114 * xpath=//img[@alt='The image alt text'] 115 * xpath=//table[@id='table1']//tr[4]/td[2] 116 117 ==== '''link='''''textPattern'' ==== 118 Select the link (anchor) element which contains text matching the specified ''pattern''. 119 * link=The link text 120 121 Without a locator prefix, Selenium uses: 122 123 * dom, for locators starting with "document." 124 * xpath, for locators starting with "//" 125 * identifier, otherwise 126 127 128 === Select Option Specifiers === 129 130 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. 131 132 ==== label=labelPattern ==== 133 matches options based on their labels, i.e. the visible text. 134 * label=regexp:^[Oo]ther 135 136 ==== value=valuePattern ==== 137 matches options based on their values. 138 * value=other 139 140 ==== id=id ==== 141 matches options based on their ids. 142 * id=option1 143 144 ==== index=index ==== 145 matches an option based on its index (offset from zero). 146 * index=2 147 148 Without a prefix, the default behaviour is to only match on labels. 149 150 String-match Patterns 151 152 Various Pattern syntaxes are available for matching string values: 153 154 glob:pattern 155 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. 156 regexp:regexp 157 Match a string using a regular-expression. The full power of JavaScript regular-expressions is available. 158 exact:string 159 Match a string exactly, verbatim, without any of that fancy wildcard stuff. 160 161 If no pattern prefix is specified, Selenium assumes that it's a "glob" pattern. 162 163 Selenium Actions 164 165 Actions tell Selenium to do something in the application. They generally represent something a user would do. 166 167 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. 168 169 open( url ) 170 171 Opens a URL in the test frame. This accepts both relative and absolute URLs. 172 173 Note: The URL must be on the same site as Selenium due to security restrictions in the browser (Cross Site Scripting). 174 175 examples: 176 177 open /mypage 178 open http://localhost/ 179 180 click( elementLocator ) 181 182 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". 183 184 examples: 185 186 click aCheckbox 187 clickAndWait submitButton 188 clickAndWait anyLink 189 190 note: 191 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'. 192 193 type( inputLocator, value ) 194 195 Sets the value of an input field, as though you typed it in. 196 197 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. 198 199 examples: 200 201 type nameField John Smith 202 typeAndWait textBoxThatSubmitsOnChange newValue 203 204 select( dropDownLocator, optionSpecifier ) 205 206 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. 207 208 examples: 209 210 select dropDown Australian Dollars 211 select dropDown index=0 212 selectAndWait currencySelector value=AUD 213 selectAndWait currencySelector label=Aus*lian D*rs 214 215 selectWindow( windowId ) 216 217 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. 218 219 target: The id of the window to select. 220 221 value: ignored 222 223 examples: 224 225 selectWindow myPopupWindow 226 selectWindow null 227 228 goBack() 229 230 Simulates the user clicking the "back" button on their browser. 231 232 examples: 233 234 goBack 235 236 close() 237 238 Simulates the user clicking the "close" button in the titlebar of a popup window. 239 240 examples: 241 242 close 243 244 pause( milliseconds ) 245 246 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. 247 248 examples: 249 250 pause 5000 251 pause 2000 252 253 fireEvent( elementLocator, eventName ) 254 255 Explicitly simulate an event, to trigger the corresponding "onevent" handler. 256 257 examples: 258 259 fireEvent textField focus 260 fireEvent dropDown blur 261 262 waitForValue( inputLocator, value ) 263 264 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. 265 266 example: 267 268 waitForValue finishIndication isfinished 269 270 store( valueToStore, variableName ) 271 272 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). 273 274 examples: 275 276 store Mr John Smith fullname 277 store $title} $firstname} $surname} fullname 278 store javascript{Math.round(Math.PI * 100) / 100} PI 279 280 storeValue( inputLocator, variableName ) 281 282 Stores the value of an input field into a variable. 283 284 examples: 285 286 storeValue userName userID 287 type userName $userID} 288 289 storeText( elementLocator, variableName ) 290 291 Stores the text of an element into a variable. 292 293 examples: 294 295 storeText currentDate expectedStartDate 296 verifyValue startDate $expectedStartDate} 297 298 storeAttribute( elementLocator@attributeName, variableName ) 299 300 Stores the value of an element attribute into a variable. 301 302 examples: 303 304 storeAttribute input1@class classOfInput1 305 verifyAttribute input2@class $classOfInput1} 306 307 chooseCancelOnNextConfirmation() 308 309 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. 310 311 examples: 312 313 chooseCancelOnNextConfirmation 314 315 answerOnNextPrompt( answerString ) 316 317 Instructs Selenium to return the specified answerString in response to the next prompt. 318 319 examples: 320 321 answerOnNextPrompt Kangaroo 322 323 Selenium Checks 324 325 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. 326 327 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. 328 329 assertLocation( relativeLocation ) 330 331 examples: 332 333 verifyLocation /mypage 334 assertLocation /mypage 335 336 assertTitle( titlePattern ) 337 338 Verifies the title of the current page. 339 340 examples: 341 342 verifyTitle My Page 343 assertTitle My Page 344 345 assertValue( inputLocator, valuePattern ) 346 347 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. 348 349 examples: 350 351 verifyValue nameField John Smith 352 assertValue document.forms[2].nameField John Smith 353 354 assertSelected( selectLocator, optionSpecifier ) 355 356 Verifies that the selected option of a drop-down satisfies the optionSpecifier. 357 358 examples: 359 360 verifySelected dropdown2 John Smith 361 verifySelected dropdown2 value=js*123 362 assertSelected document.forms[2].dropDown label=J* Smith 363 assertSelected document.forms[2].dropDown index=0 364 365 assertSelectOptions( selectLocator, optionLabelList ) 366 367 Verifies the labels of all options in a drop-down against a comma-separated list. Commas in an expected option can be escaped as ",". 368 369 examples: 370 371 verifySelectOptions dropdown2 John Smith,Dave Bird 372 assertSelectOptions document.forms[2].dropDown Smith\, J,Bird\, D 373 374 assertText( elementLocator, textPattern ) 375 376 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. 377 378 examples: 379 380 verifyText statusMessage Successful 381 assertText //div[@id='foo']//h1 Successful 382 383 assertAttribute( elementLocator@attributeName, valuePattern ) 384 385 Verifies the value of an element attribute. 386 387 examples: 388 389 verifyAttribute txt1@class bigAndBold 390 assertAttribute document.images[0]@alt alt-text 391 verifyAttribute //img[@id='foo']/@alt alt-text 392 393 assertTextPresent( text ) 394 395 Verifies that the specified text appears somewhere on the rendered page shown to the user. 396 397 examples: 398 399 verifyTextPresent You are now logged in. 400 assertTextPresent You are now logged in. 401 402 assertTextNotPresent( text ) 403 404 Verifies that the specified text does NOT appear anywhere on the rendered page. 405 406 assertElementPresent( elementLocator ) 407 408 Verifies that the specified element is somewhere on the page. 409 410 examples: 411 412 verifyElementPresent submitButton 413 assertElementPresent //img[@alt='foo'] 414 415 assertElementNotPresent( elementLocator ) 416 417 Verifies that the specified element is NOT on the page. 418 419 examples: 420 421 verifyElementNotPresent cancelButton 422 assertElementNotPresent cancelButton 423 424 assertTable( cellAddress, valuePattern ) 425 426 Verifies the text in a cell of a table. The cellAddress syntax tableName.row.column, where row and column start at 0. 427 428 examples: 429 430 verifyTable myTable.1.6 Submitted 431 assertTable results.0.2 13 432 433 assertVisible( elementLocator ) 434 435 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. 436 437 examples: 438 439 verifyVisible postcode 440 assertVisible postcode 441 442 assertNotVisible( elementLocator ) 443 444 Verifies that the specified element is NOT visible. Elements that are simply not present are also considered invisible. 445 446 examples: 447 448 verifyNotVisible postcode 449 assertNotVisible postcode 450 451 verifyEditable / assertEditable( inputLocator ) 452 453 Verifies that the specified element is editable, ie. it's an input element, and hasn't been disabled. 454 455 examples: 456 457 verifyEditable shape 458 assertEditable colour 459 460 assertNotEditable( inputLocator ) 461 462 Verifies that the specified element is NOT editable, ie. it's NOT an input element, or has been disabled. 463 464 assertAlert( messagePattern ) 465 466 Verifies that a javascript alert with the specified message was generated. Alerts must be verified in the same order that they were generated. 467 468 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. 469 470 NOTE: under Selenium, javascript alerts will NOT pop up a visible alert dialog. 471 472 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. 473 474 examples: 475 476 verifyAlert Invalid Phone Number 477 assertAlert Invalid Phone Number 478 479 assertConfirmation( messagePattern ) 480 481 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. 482 483 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. 484 485 NOTE: under Selenium, javascript confirmations will NOT pop up a visible dialog. 486 487 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. 488 489 examples: 490 491 assertConfirmation Remove this user? 492 verifyConfirmation Are you sure? 493 494 assertPrompt( messagePattern ) 495 496 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. 497 498 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. 499 500 examples: 501 502 answerOnNextPrompt Joe 503 click id=delegate 504 verifyPrompt Delegate to who? 505 506 Parameter construction and Variables 507 508 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. 509 510 Stored Variables 511 512 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. 513 514 Variable substitution 515 516 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. 517 518 Example: 519 520 store Mr title 521 storeValue nameField surname 522 store $title} $surname} fullname 523 type textElement Full name is: $fullname} 524 525 Javascript evaluation 526 527 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. 528 529 Example: 530 531 store javascript{'merchant' + (new Date()).getTime()} merchantId 532 type textElement javascript{storedVars['merchantId'].toUpperCase()}
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |