/Server/SWXPHP/Prior Releases/1.01/php/tests/simpletest/docs/en/form_testing_documentation.html

http://swx-format.googlecode.com/ · HTML · 316 lines · 313 code · 3 blank · 0 comment · 0 complexity · 4bf2c3ddaa344314482e4d5a200fd892 MD5 · raw file

  1. <html>
  2. <head>
  3. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>Simple Test documentation for testing HTML forms</title>
  5. <link rel="stylesheet" type="text/css" href="docs.css" title="Styles">
  6. </head>
  7. <body>
  8. <div class="menu_back">
  9. <div class="menu">
  10. <a href="index.html">SimpleTest</a>
  11. |
  12. <a href="overview.html">Overview</a>
  13. |
  14. <a href="unit_test_documentation.html">Unit tester</a>
  15. |
  16. <a href="group_test_documentation.html">Group tests</a>
  17. |
  18. <a href="mock_objects_documentation.html">Mock objects</a>
  19. |
  20. <a href="partial_mocks_documentation.html">Partial mocks</a>
  21. |
  22. <a href="reporter_documentation.html">Reporting</a>
  23. |
  24. <a href="expectation_documentation.html">Expectations</a>
  25. |
  26. <a href="web_tester_documentation.html">Web tester</a>
  27. |
  28. <span class="chosen">Testing forms</span>
  29. |
  30. <a href="authentication_documentation.html">Authentication</a>
  31. |
  32. <a href="browser_documentation.html">Scriptable browser</a>
  33. </div>
  34. </div>
  35. <h1>Form testing documentation</h1>
  36. This page...
  37. <ul>
  38. <li>
  39. Changing form values and successfully
  40. <a href="#submit">Submitting a simple form</a>
  41. </li>
  42. <li>
  43. Handling <a href="#multiple">widgets with multiple values</a>
  44. by setting lists.
  45. </li>
  46. <li>
  47. <a href="#raw">Raw posting</a> when you don't have a button
  48. to click.
  49. </li>
  50. </ul>
  51. <div class="content">
  52. <p>
  53. <a class="target" name="submit">
  54. <h2>Submitting a simple form</h2>
  55. </a>
  56. </p>
  57. <p>
  58. When a page is fetched by the <span class="new_code">WebTestCase</span>
  59. using <span class="new_code">get()</span> or
  60. <span class="new_code">post()</span> the page content is
  61. automatically parsed.
  62. This results in any form controls that are inside &lt;form&gt; tags
  63. being available from within the test case.
  64. For example, if we have this snippet of HTML...
  65. <pre>
  66. &lt;form&gt;
  67. &lt;input type="text" name="a" value="A default" /&gt;
  68. &lt;input type="submit" value="Go" /&gt;
  69. &lt;/form&gt;
  70. </pre>
  71. Which looks like this...
  72. </p>
  73. <p>
  74. <form class="demo">
  75. <input type="text" name="a" value="A default">
  76. <input type="submit" value="Go">
  77. </form>
  78. </p>
  79. <p>
  80. We can navigate to this code, via the
  81. <a href="http://www.lastcraft.com/form_testing_documentation.php">LastCraft</a>
  82. site, with the following test...
  83. <pre>
  84. class SimpleFormTests extends WebTestCase {
  85. <strong>
  86. function testDefaultValue() {
  87. $this-&gt;get('http://www.lastcraft.com/form_testing_documentation.php');
  88. $this-&gt;assertField('a', 'A default');
  89. }</strong>
  90. }
  91. </pre>
  92. Immediately after loading the page all of the HTML controls are set at
  93. their default values just as they would appear in the web browser.
  94. The assertion tests that a HTML widget exists in the page with the
  95. name "a" and that it is currently set to the value
  96. "A default".
  97. As usual, we could use a pattern expectation instead if a fixed
  98. string.
  99. </p>
  100. <p>
  101. We could submit the form straight away, but first we'll change
  102. the value of the text field and only then submit it...
  103. <pre>
  104. class SimpleFormTests extends WebTestCase {
  105. function testDefaultValue() {
  106. $this-&gt;get('http://www.my-site.com/');
  107. $this-&gt;assertField('a', 'A default');<strong>
  108. $this-&gt;setField('a', 'New value');
  109. $this-&gt;click('Go');</strong>
  110. }
  111. }
  112. </pre>
  113. Because we didn't specify a method attribute on the form tag, and
  114. didn't specify an action either, the test case will follow
  115. the usual browser behaviour of submitting the form data as a <em>GET</em>
  116. request back to the same location.
  117. SimpleTest tries to emulate typical browser behaviour as much as possible,
  118. rather than attempting to catch missing attributes on tags.
  119. This is because the target of the testing framework is the PHP application
  120. logic, not syntax or other errors in the HTML code.
  121. For HTML errors, other tools such as
  122. <a href="http://www.w3.org/People/Raggett/tidy/">HTMLTidy</a> should be used.
  123. </p>
  124. <p>
  125. If a field is not present in any form, or if an option is unavailable,
  126. then <span class="new_code">WebTestCase::setField()</span> will return
  127. <span class="new_code">false</span>.
  128. For example, suppose we wish to verify that a "Superuser"
  129. option is not present in this form...
  130. <pre>
  131. &lt;strong&gt;Select type of user to add:&lt;/strong&gt;
  132. &lt;select name="type"&gt;
  133. &lt;option&gt;Subscriber&lt;/option&gt;
  134. &lt;option&gt;Author&lt;/option&gt;
  135. &lt;option&gt;Administrator&lt;/option&gt;
  136. &lt;/select&gt;
  137. </pre>
  138. Which looks like...
  139. </p>
  140. <p>
  141. <form class="demo">
  142. <strong>Select type of user to add:</strong>
  143. <select name="type">
  144. <option>Subscriber</option>
  145. <option>Author</option>
  146. <option>Administrator</option>
  147. </select>
  148. </form>
  149. </p>
  150. <p>
  151. The following test will confirm it...
  152. <pre>
  153. class SimpleFormTests extends WebTestCase {
  154. ...
  155. function testNoSuperuserChoiceAvailable() {<strong>
  156. $this-&gt;get('http://www.lastcraft.com/form_testing_documentation.php');
  157. $this-&gt;assertFalse($this-&gt;setField('type', 'Superuser'));</strong>
  158. }
  159. }
  160. </pre>
  161. The selection will not be changed on a failure to set
  162. a widget value.
  163. </p>
  164. <p>
  165. Here is the full list of widgets currently supported...
  166. <ul>
  167. <li>Text fields, including hidden and password fields.</li>
  168. <li>Submit buttons including the button tag, although not yet reset buttons</li>
  169. <li>Text area. This includes text wrapping behaviour.</li>
  170. <li>Checkboxes, including multiple checkboxes in the same form.</li>
  171. <li>Drop down selections, including multiple selects.</li>
  172. <li>Radio buttons.</li>
  173. <li>Images.</li>
  174. </ul>
  175. </p>
  176. <p>
  177. Although most standard HTML widgets are catered for by <em>SimpleTest</em>'s
  178. built in parser, it is unlikely that JavaScript will be implemented
  179. anytime soon.
  180. </p>
  181. <p>
  182. <a class="target" name="multiple">
  183. <h2>Fields with multiple values</h2>
  184. </a>
  185. </p>
  186. <p>
  187. SimpleTest can cope with two types of multivalue controls: Multiple
  188. selection drop downs, and multiple checkboxes with the same name
  189. within a form.
  190. The multivalue nature of these means that setting and testing
  191. are slightly different.
  192. Using checkboxes as an example...
  193. <pre>
  194. &lt;form class="demo"&gt;
  195. &lt;strong&gt;Create privileges allowed:&lt;/strong&gt;
  196. &lt;input type="checkbox" name="crud" value="c" checked&gt;&lt;br&gt;
  197. &lt;strong&gt;Retrieve privileges allowed:&lt;/strong&gt;
  198. &lt;input type="checkbox" name="crud" value="r" checked&gt;&lt;br&gt;
  199. &lt;strong&gt;Update privileges allowed:&lt;/strong&gt;
  200. &lt;input type="checkbox" name="crud" value="u" checked&gt;&lt;br&gt;
  201. &lt;strong&gt;Destroy privileges allowed:&lt;/strong&gt;
  202. &lt;input type="checkbox" name="crud" value="d" checked&gt;&lt;br&gt;
  203. &lt;input type="submit" value="Enable Privileges"&gt;
  204. &lt;/form&gt;
  205. </pre>
  206. Which renders as...
  207. </p>
  208. <p>
  209. <form class="demo">
  210. <strong>Create privileges allowed:</strong>
  211. <input type="checkbox" name="crud" value="c" checked>
  212. <br>
  213. <strong>Retrieve privileges allowed:</strong>
  214. <input type="checkbox" name="crud" value="r" checked>
  215. <br>
  216. <strong>Update privileges allowed:</strong>
  217. <input type="checkbox" name="crud" value="u" checked>
  218. <br>
  219. <strong>Destroy privileges allowed:</strong>
  220. <input type="checkbox" name="crud" value="d" checked>
  221. <br>
  222. <input type="submit" value="Enable Privileges">
  223. </form>
  224. </p>
  225. <p>
  226. If we wish to disable all but the retrieval privileges and
  227. submit this information we can do it like this...
  228. <pre>
  229. class SimpleFormTests extends WebTestCase {
  230. ...<strong>
  231. function testDisableNastyPrivileges() {
  232. $this-&gt;get('http://www.lastcraft.com/form_testing_documentation.php');
  233. $this-&gt;assertField('crud', array('c', 'r', 'u', 'd'));
  234. $this-&gt;setField('crud', array('r'));
  235. $this-&gt;click('Enable Privileges');
  236. }</strong>
  237. }
  238. </pre>
  239. Instead of setting the field to a single value, we give it a list
  240. of values.
  241. We do the same when testing expected values.
  242. We can then write other test code to confirm the effect of this, perhaps
  243. by logging in as that user and attempting an update.
  244. </p>
  245. <p>
  246. <a class="target" name="raw">
  247. <h2>Raw posting</h2>
  248. </a>
  249. </p>
  250. <p>
  251. If you want to test a form handler, but have not yet written
  252. or do not have access to the form itself, you can create a
  253. form submission by hand.
  254. <pre>
  255. class SimpleFormTests extends WebTestCase {
  256. ...<strong>
  257. function testAttemptedHack() {
  258. $this-&gt;post(
  259. 'http://www.my-site.com/add_user.php',
  260. array('type' =&gt; 'superuser'));
  261. $this-&gt;assertNoText('user created');
  262. }</strong>
  263. }
  264. </pre>
  265. By adding data to the <span class="new_code">WebTestCase::post()</span>
  266. method, we are attempting to fetch the page as a form submission.
  267. </p>
  268. </div>
  269. References and related information...
  270. <ul>
  271. <li>
  272. SimpleTest project page on <a href="http://sourceforge.net/projects/simpletest/">SourceForge</a>.
  273. </li>
  274. <li>
  275. SimpleTest download page on <a href="http://www.lastcraft.com/simple_test.php">LastCraft</a>.
  276. </li>
  277. <li>
  278. The <a href="http://simpletest.sourceforge.net/">developer's API for SimpleTest</a>
  279. gives full detail on the classes and assertions available.
  280. </li>
  281. </ul>
  282. <div class="menu_back">
  283. <div class="menu">
  284. <a href="index.html">SimpleTest</a>
  285. |
  286. <a href="overview.html">Overview</a>
  287. |
  288. <a href="unit_test_documentation.html">Unit tester</a>
  289. |
  290. <a href="group_test_documentation.html">Group tests</a>
  291. |
  292. <a href="mock_objects_documentation.html">Mock objects</a>
  293. |
  294. <a href="partial_mocks_documentation.html">Partial mocks</a>
  295. |
  296. <a href="reporter_documentation.html">Reporting</a>
  297. |
  298. <a href="expectation_documentation.html">Expectations</a>
  299. |
  300. <a href="web_tester_documentation.html">Web tester</a>
  301. |
  302. <span class="chosen">Testing forms</span>
  303. |
  304. <a href="authentication_documentation.html">Authentication</a>
  305. |
  306. <a href="browser_documentation.html">Scriptable browser</a>
  307. </div>
  308. </div>
  309. <div class="copyright">
  310. Copyright<br>Marcus Baker 2006
  311. </div>
  312. </body>
  313. </html>