Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
<h2>
11
<a href="index.html">SimpleTest</a>
12
</h2>
13
<ul>
14
<li>
15
<a href="overview.html">Overview</a>
16
</li>
17
<li>
18
<a href="unit_test_documentation.html">Unit tester</a>
19
</li>
20
<li>
21
<a href="group_test_documentation.html">Group tests</a>
22
</li>
23
<li>
24
<a href="mock_objects_documentation.html">Mock objects</a>
25
</li>
26
<li>
27
<a href="partial_mocks_documentation.html">Partial mocks</a>
28
</li>
29
<li>
30
<a href="reporter_documentation.html">Reporting</a>
31
</li>
32
<li>
33
<a href="expectation_documentation.html">Expectations</a>
34
</li>
35
<li>
36
<a href="web_tester_documentation.html">Web tester</a>
37
</li>
38
<li>
39
<span class="chosen">Testing forms</span>
40
</li>
41
<li>
42
<a href="authentication_documentation.html">Authentication</a>
43
</li>
44
<li>
45
<a href="browser_documentation.html">Scriptable browser</a>
46
</li>
47
</ul>
48
</div>
49
</div>
50
<h1>Form testing documentation</h1>
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
 
106
    function testDefaultValue() {
107
        $this-&gt;get('http://www.my-site.com/');
108
        $this-&gt;assertField('a', 'A default');<strong>
109
        $this-&gt;setField('a', 'New value');
110
        $this-&gt;click('Go');</strong>
111
    }
112
}
113
</pre>
114
                Because we didn't specify a method attribute on the form tag, and
115
                didn't specify an action either, the test case will follow
116
                the usual browser behaviour of submitting the form data as a <em>GET</em>
117
                request back to the same location.
118
                SimpleTest tries to emulate typical browser behaviour as much as possible,
119
                rather than attempting to catch missing attributes on tags.
120
                This is because the target of the testing framework is the PHP application
121
                logic, not syntax or other errors in the HTML code.
122
                For HTML errors, other tools such as
123
                <a href="http://www.w3.org/People/Raggett/tidy/">HTMLTidy</a> should be used.
124
            </p>
125
            <p>
126
                If a field is not present in any form, or if an option is unavailable,
127
                then <span class="new_code">WebTestCase::setField()</span> will return
128
                <span class="new_code">false</span>.
129
                For example, suppose we wish to verify that a "Superuser"
130
                option is not present in this form...
131
<pre>
132
&lt;strong&gt;Select type of user to add:&lt;/strong&gt;
133
&lt;select name="type"&gt;
134
    &lt;option&gt;Subscriber&lt;/option&gt;
135
    &lt;option&gt;Author&lt;/option&gt;
136
    &lt;option&gt;Administrator&lt;/option&gt;
137
&lt;/select&gt;
138
</pre>
139
                Which looks like...
140
            </p>
141
            <p>
142
                <form class="demo">
143
                    <strong>Select type of user to add:</strong>
144
                    <select name="type">
145
                        <option>Subscriber</option>
146
                        <option>Author</option>
147
                        <option>Administrator</option>
148
                    </select>
149
                </form>
150
            </p>
151
            <p>
152
                The following test will confirm it...
153
<pre>
154
class SimpleFormTests extends WebTestCase {
155
    ...
156
    function testNoSuperuserChoiceAvailable() {<strong>
157
        $this-&gt;get('http://www.lastcraft.com/form_testing_documentation.php');
158
        $this-&gt;assertFalse($this-&gt;setField('type', 'Superuser'));</strong>
159
    }
160
}
161
</pre>
162
                The selection will not be changed on a failure to set
163
                a widget value.
164
            </p>
165
            <p>
166
                Here is the full list of widgets currently supported...
167
                <ul>
168
                    <li>Text fields, including hidden and password fields.</li>
169
                    <li>Submit buttons including the button tag, although not yet reset buttons</li>
170
                    <li>Text area. This includes text wrapping behaviour.</li>
171
                    <li>Checkboxes, including multiple checkboxes in the same form.</li>
172
                    <li>Drop down selections, including multiple selects.</li>
173
                    <li>Radio buttons.</li>
174
                    <li>Images.</li>
175
                </ul>
176
            </p>
177
            <p>
178
                Although most standard HTML widgets are catered for by <em>SimpleTest</em>'s
179
                built in parser, it is unlikely that JavaScript will be implemented
180
                anytime soon.
181
            </p>
182
 
183
        <p>
184
<a class="target" name="multiple">
185
<h2>Fields with multiple values</h2>
186
</a>
187
</p>
188
            <p>
189
                SimpleTest can cope with two types of multivalue controls: Multiple
190
                selection drop downs, and multiple checkboxes with the same name
191
                within a form.
192
                The multivalue nature of these means that setting and testing
193
                are slightly different.
194
                Using checkboxes as an example...
195
<pre>
196
&lt;form class="demo"&gt;
197
    &lt;strong&gt;Create privileges allowed:&lt;/strong&gt;
198
    &lt;input type="checkbox" name="crud" value="c" checked&gt;&lt;br&gt;
199
    &lt;strong&gt;Retrieve privileges allowed:&lt;/strong&gt;
200
    &lt;input type="checkbox" name="crud" value="r" checked&gt;&lt;br&gt;
201
    &lt;strong&gt;Update privileges allowed:&lt;/strong&gt;
202
    &lt;input type="checkbox" name="crud" value="u" checked&gt;&lt;br&gt;
203
    &lt;strong&gt;Destroy privileges allowed:&lt;/strong&gt;
204
    &lt;input type="checkbox" name="crud" value="d" checked&gt;&lt;br&gt;
205
    &lt;input type="submit" value="Enable Privileges"&gt;
206
&lt;/form&gt;
207
</pre>
208
                Which renders as...
209
            </p>
210
            <p>
211
                <form class="demo">
212
                    <strong>Create privileges allowed:</strong>
213
                    <input type="checkbox" name="crud" value="c" checked>
214
<br>
215
                    <strong>Retrieve privileges allowed:</strong>
216
                    <input type="checkbox" name="crud" value="r" checked>
217
<br>
218
                    <strong>Update privileges allowed:</strong>
219
                    <input type="checkbox" name="crud" value="u" checked>
220
<br>
221
                    <strong>Destroy privileges allowed:</strong>
222
                    <input type="checkbox" name="crud" value="d" checked>
223
<br>
224
                    <input type="submit" value="Enable Privileges">
225
                </form>
226
            </p>
227
            <p>
228
                If we wish to disable all but the retrieval privileges and
229
                submit this information we can do it like this...
230
<pre>
231
class SimpleFormTests extends WebTestCase {
232
    ...<strong>
233
    function testDisableNastyPrivileges() {
234
        $this-&gt;get('http://www.lastcraft.com/form_testing_documentation.php');
235
        $this-&gt;assertField('crud', array('c', 'r', 'u', 'd'));
236
        $this-&gt;setField('crud', array('r'));
237
        $this-&gt;click('Enable Privileges');
238
    }</strong>
239
}
240
</pre>
241
                Instead of setting the field to a single value, we give it a list
242
                of values.
243
                We do the same when testing expected values.
244
                We can then write other test code to confirm the effect of this, perhaps
245
                by logging in as that user and attempting an update.
246
            </p>
247
            <p>
248
                <a class="target" name="raw">
249
<h2>Raw posting</h2>
250
</a>
251
            </p>
252
            <p>
253
                If you want to test a form handler, but have not yet written
254
                or do not have access to the form itself, you can create a
255
                form submission by hand.
256
<pre>
257
class SimpleFormTests extends WebTestCase {
258
    ...<strong>
259
    function testAttemptedHack() {
260
        $this-&gt;post(
261
                'http://www.my-site.com/add_user.php',
262
                array('type' =&gt; 'superuser'));
263
        $this-&gt;assertNoText('user created');
264
    }</strong>
265
}
266
</pre>
267
                By adding data to the <span class="new_code">WebTestCase::post()</span>
268
                method, we are attempting to fetch the page as a form submission.
269
            </p>
270
 
271
    </div>
272
<div class="copyright">
273
            Copyright<br>Marcus Baker, Jason Sweat, Perrick Penet 2004
274
        </div>
275
</body>
276
</html>