/
README.txt
1141 lines (906 loc) · 33.6 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
================
The Test Browser
================
The ``zope.testbrowser.browser`` module exposes a ``Browser`` class that
simulates a web browser similar to Mozilla Firefox or IE.
>>> from zope.testbrowser.browser import Browser
>>> browser = Browser()
This version of the browser object can be used to access any web site just as
you would do using a normal web browser.
There is also a special version of the ``Browser`` class used to do functional
testing of Zope 3 applications, it can be imported from
``zope.testbrowser.testing`` or just ``zope.testbrowser`` directly:
>>> from zope.testbrowser import Browser
>>> browser = Browser()
An ititial page to load can be passed to the ``Browser`` constructor:
>>> browser = Browser('http://localhost/@@/testbrowser/simple.html')
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
The browser can send arbitrary headers; this is helpful for setting the
"Authorization" header or a language value, so that your tests format values
the way you expect in your tests, if you rely on zope.i18n locale-based
formatting or a similar approach.
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
>>> browser.addHeader('Accept-Language', 'en-US')
An existing browser instance can also `open` web pages:
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
Once you have opened a web page initially, best practice for writing
testbrowser doctests suggests using 'click' to navigate further (as discussed
below), except in unusual circumstances.
The test browser complies with the IBrowser interface; see
``zope.testbrowser.interfaces`` for full details on the interface.
>>> from zope.testbrowser import interfaces
>>> from zope.interface.verify import verifyObject
>>> verifyObject(interfaces.IBrowser, browser)
True
Page Contents
-------------
The contents of the current page are available:
>>> print browser.contents
<html>
<head>
<title>Simple Page</title>
</head>
<body>
<h1>Simple Page</h1>
</body>
</html>
Making assertions about page contents is easy.
>>> '<h1>Simple Page</h1>' in browser.contents
True
Utilizing the doctest facilities, it also possible to do:
>>> browser.contents
'...<h1>Simple Page</h1>...'
Note: Unfortunately, ellipsis (...) cannot be used at the beginning of the
output (this is a limitation of doctest).
Checking for HTML
-----------------
Not all URLs return HTML. Of course our simple page does:
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.isHtml
True
But if we load an image (or other binary file), we do not get HTML:
>>> browser.open('http://localhost/@@/testbrowser/zope3logo.gif')
>>> browser.isHtml
False
HTML Page Title
----------------
Another useful helper property is the title:
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.title
'Simple Page'
If a page does not provide a title, it is simply ``None``:
>>> browser.open('http://localhost/@@/testbrowser/notitle.html')
>>> browser.title
However, if the output is not HTML, then an error will occur trying to access
the title:
>>> browser.open('http://localhost/@@/testbrowser/zope3logo.gif')
>>> browser.title
Traceback (most recent call last):
...
BrowserStateError: not viewing HTML
Headers
-------
As you can see, the `contents` of the browser does not return any HTTP
headers. The headers are accessible via a separate attribute, which is an
``httplib.HTTPMessage`` instance (httplib is a part of Python's standard
library):
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.headers
<httplib.HTTPMessage instance...>
The headers can be accesed as a string:
>>> print browser.headers
Status: 200 Ok
Content-Length: ...
Content-Type: text/html;charset=utf-8
X-Content-Type-Warning: guessed from content
X-Powered-By: Zope (www.zope.org), Python (www.python.org)
Or as a mapping:
>>> browser.headers['content-type']
'text/html;charset=utf-8'
Navigation and Link Objects
---------------------------
If you want to simulate clicking on a link, get the link and `click` on it.
In the `navigate.html` file there are several links set up to demonstrate the
capabilities of the link objects and their `click` method.
The simplest way to get a link is via the anchor text. In other words
the text you would see in a browser (text and url searches are substring
searches):
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.contents
'...<a href="navigate.html?message=By+Link+Text">Link Text</a>...'
>>> link = browser.getLink('Link Text')
>>> link
<Link text='Link Text'
url='http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text'>
Link objects comply with the ILink interface.
>>> verifyObject(interfaces.ILink, link)
True
Links expose several attributes for easy access.
>>> link.text
'Link Text'
>>> link.tag # links can also be image maps.
'a'
>>> link.url # it's normalized
'http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text'
>>> link.attrs
{'href': 'navigate.html?message=By+Link+Text'}
Links can be "clicked" and the brower will navigate to the refrenced URL.
>>> link.click()
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text'
>>> browser.contents
'...Message: <em>By Link Text</em>...'
When finding a link by its text, whitespace is normalized.
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.contents
'...> Link Text \n with Whitespace\tNormalization (and parens) </...'
>>> link = browser.getLink('Link Text with Whitespace Normalization '
... '(and parens)')
>>> link
<Link text='Link Text with Whitespace Normalization (and parens)'...>
>>> link.text
'Link Text with Whitespace Normalization (and parens)'
>>> link.click()
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text+with+Normalization'
>>> browser.contents
'...Message: <em>By Link Text with Normalization</em>...'
Note that clicking a link object after its browser page has expired will
geterate an error.
>>> link.click()
Traceback (most recent call last):
...
ExpiredError
You can also find the link by its URL,
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.contents
'...<a href="navigate.html?message=By+URL">Using the URL</a>...'
>>> browser.getLink(url='?message=By+URL').click()
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+URL'
>>> browser.contents
'...Message: <em>By URL</em>...'
or its id:
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.contents
'...<a href="navigate.html?message=By+Id"
id="anchorid">By Anchor Id</a>...'
>>> browser.getLink(id='anchorid').click()
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+Id'
>>> browser.contents
'...Message: <em>By Id</em>...'
You thought we were done here? Not so quickly. The `getLink` method also
supports image maps, though not by specifying the coordinates, but using the
area's id:
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> link = browser.getLink(id='zope3')
>>> link.tag
'area'
>>> link.click()
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=Zope+3+Name'
>>> browser.contents
'...Message: <em>Zope 3 Name</em>...'
Getting a nonexistent link raises an exception.
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.getLink('This does not exist')
Traceback (most recent call last):
...
LinkNotFoundError
Other Navigation
----------------
Like in any normal browser, you can reload a page:
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
>>> browser.reload()
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
You can also go back:
>>> browser.open('http://localhost/@@/testbrowser/notitle.html')
>>> browser.url
'http://localhost/@@/testbrowser/notitle.html'
>>> browser.goBack()
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
Controls
--------
One of the most important features of the browser is the ability to inspect
and fill in values for the controls of input forms. To do so, let's first open
a page that has a bunch of controls:
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
Obtaining a Control
~~~~~~~~~~~~~~~~~~~
You look up browser controls with the 'getControl' method. The default first
argument is 'label', and looks up the form on the basis of any associated
label.
>>> control = browser.getControl('Text Control')
>>> control
<Control name='text-value' type='text'>
>>> browser.getControl(label='Text Control') # equivalent
<Control name='text-value' type='text'>
If you request a control that doesn't exist, the code raises a LookupError:
>>> browser.getControl('Does Not Exist')
Traceback (most recent call last):
...
LookupError: label 'Does Not Exist'
If you request a control with an ambiguous lookup, the code raises an
AmbiguityError.
>>> browser.getControl('Ambiguous Control')
Traceback (most recent call last):
...
AmbiguityError: label 'Ambiguous Control'
Ambiguous controls may be specified using an index value. We use the control's
value attribute to show the two controls; this attribute is properly introduced
below.
>>> browser.getControl('Ambiguous Control', index=0)
<Control name='ambiguous-control-name' type='text'>
>>> browser.getControl('Ambiguous Control', index=0).value
'First'
>>> browser.getControl('Ambiguous Control', index=1).value
'Second'
Label searches are against stripped, whitespace-normalized, no-tag versions of
the text. Text applied to searches is also stripped and whitespace normalized.
The search finds results if the text search finds the whole words of your
text in a label. Thus, for instance, a search for 'Add' will match the label
'Add a Client' but not 'Address'. Case is honored.
>>> browser.getControl('Label Needs Whitespace Normalization')
<Control name='label-needs-normalization' type='text'>
>>> browser.getControl('label needs whitespace normalization')
Traceback (most recent call last):
...
LookupError: label 'label needs whitespace normalization'
>>> browser.getControl(' Label Needs Whitespace ')
<Control name='label-needs-normalization' type='text'>
>>> browser.getControl('Whitespace')
<Control name='label-needs-normalization' type='text'>
>>> browser.getControl('hitespace')
Traceback (most recent call last):
...
LookupError: label 'hitespace'
>>> browser.getControl('[non word characters should not confuse]')
<Control name='non-word-characters' type='text'>
Multiple labels can refer to the same control (simply because that is possible
in the HTML 4.0 spec).
>>> browser.getControl('Multiple labels really')
<Control name='two-labels' type='text'>
>>> browser.getControl('really are possible')
<Control name='two-labels' type='text'>
>>> browser.getControl('really') # OK: ambiguous labels, but not ambiguous control
<Control name='two-labels' type='text'>
A label can be connected with a control using the 'for' attribute and also by
containing a control.
>>> browser.getControl(
... 'Labels can be connected by containing their respective fields')
<Control name='contained-in-label' type='text'>
Get also accepts one other search argument, 'name'. Only one of 'label' and
'name' may be used at a time. The 'name' keyword searches form field names.
>>> browser.getControl(name='text-value')
<Control name='text-value' type='text'>
>>> browser.getControl(name='ambiguous-control-name')
Traceback (most recent call last):
...
AmbiguityError: name 'ambiguous-control-name'
>>> browser.getControl(name='does-not-exist')
Traceback (most recent call last):
...
LookupError: name 'does-not-exist'
>>> browser.getControl(name='ambiguous-control-name', index=1).value
'Second'
Combining 'label' and 'name' raises a ValueError, as does supplying neither of
them.
>>> browser.getControl(label='Ambiguous Control', name='ambiguous-control-name')
Traceback (most recent call last):
...
ValueError: Supply one and only one of "label" and "name" as arguments
>>> browser.getControl()
Traceback (most recent call last):
...
ValueError: Supply one and only one of "label" and "name" as arguments
Radio and checkbox fields are unusual in that their labels and names may point
to different objects: names point to logical collections of radio buttons or
checkboxes, but labels may only be used for individual choices within the
logical collection. This means that obtaining a radio button by label gets a
different object than obtaining the radio collection by name. Select options
may also be searched by label.
>>> browser.getControl(name='radio-value')
<ListControl name='radio-value' type='radio'>
>>> browser.getControl('Zwei')
<ItemControl name='radio-value' type='radio' optionValue='2'>
>>> browser.getControl('One')
<ItemControl name='multi-checkbox-value' type='checkbox' optionValue='1'>
>>> browser.getControl('Tres')
<ItemControl name='single-select-value' type='select' optionValue='3'>
Characteristics of controls and subcontrols are discussed below.
Control Objects
~~~~~~~~~~~~~~~
Controls provide IControl.
>>> ctrl = browser.getControl('Text Control')
>>> ctrl
<Control name='text-value' type='text'>
>>> verifyObject(interfaces.IControl, ctrl)
True
They have several useful attributes:
- the name as which the control is known to the form:
>>> ctrl.name
'text-value'
- the value of the control, which may also be set:
>>> ctrl.value
'Some Text'
>>> ctrl.value = 'More Text'
>>> ctrl.value
'More Text'
- the type of the control:
>>> ctrl.type
'text'
- a flag describing whether the control is disabled:
>>> ctrl.disabled
False
- and a flag to tell us whether the control can have multiple values:
>>> ctrl.multiple
False
Additionally, controllers for select, radio, and checkbox provide IListControl.
These fields have four other attributes and an additional method:
>>> ctrl = browser.getControl('Multiple Select Control')
>>> ctrl
<ListControl name='multi-select-value' type='select'>
>>> ctrl.disabled
False
>>> ctrl.multiple
True
>>> verifyObject(interfaces.IListControl, ctrl)
True
- 'options' lists all available value options.
>>> ctrl.options
['1', '2', '3']
- 'displayOptions' lists all available options by label. The 'label'
attribute on an option has precedence over its contents, which is why
our last option is 'Third' in the display.
>>> ctrl.displayOptions
['Un', 'Deux', 'Third']
- 'displayValue' lets you get and set the displayed values of the control
of the select box, rather than the actual values.
>>> ctrl.value
[]
>>> ctrl.displayValue
[]
>>> ctrl.displayValue = ['Un', 'Deux']
>>> ctrl.displayValue
['Un', 'Deux']
>>> ctrl.value
['1', '2']
- subcontrols gives you a list of the subcontrol objects in the control
(subcontrols are discussed below).
>>> ctrl.controls
[<ItemControl name='multi-select-value' type='select' optionValue='1'>,
<ItemControl name='multi-select-value' type='select' optionValue='2'>,
<ItemControl name='multi-select-value' type='select' optionValue='3'>]
- The get method lets you get subcontrols by their label or their value.
>>> ctrl.getControl('Un')
<ItemControl name='multi-select-value' type='select' optionValue='1'>
>>> ctrl.getControl('Deux')
<ItemControl name='multi-select-value' type='select' optionValue='2'>
>>> ctrl.getControl('Trois') # label attribute
<ItemControl name='multi-select-value' type='select' optionValue='3'>
>>> ctrl.getControl('Third') # contents
<ItemControl name='multi-select-value' type='select' optionValue='3'>
>>> browser.getControl('Third') # ambiguous in the browser, so useful
Traceback (most recent call last):
...
AmbiguityError: label 'Third'
Finally, submit controls provide ISubmitControl, and image controls provide
IImageSubmitControl, which extents ISubmitControl. These both simply add a
'click' method. For image submit controls, you may also provide a coordinates
argument, which is a tuple of (x, y). These submit the forms, and are
demonstrated below as we examine each control individually.
ItemControl Objects
~~~~~~~~~~~~~~~~~~~
As introduced briefly above, using labels to obtain elements of a logical
radio button or checkbox collection returns item controls, which are parents.
Manipulating the value of these controls affects the parent control.
>>> browser.getControl(name='radio-value').value
['2']
>>> browser.getControl('Zwei').optionValue # read-only.
'2'
>>> browser.getControl('Zwei').selected
True
>>> verifyObject(interfaces.IItemControl, browser.getControl('Zwei'))
True
>>> browser.getControl('Ein').selected = True
>>> browser.getControl('Ein').selected
True
>>> browser.getControl('Zwei').selected
False
>>> browser.getControl(name='radio-value').value
['1']
>>> browser.getControl('Ein').selected = False
>>> browser.getControl(name='radio-value').value
[]
>>> browser.getControl('Zwei').selected = True
Checkbox collections behave similarly, as shown below.
Controls with subcontrols--
Various Controls
~~~~~~~~~~~~~~~~
The various types of controls are demonstrated here.
- Text Control
The text control we already introduced above.
- Password Control
>>> ctrl = browser.getControl('Password Control')
>>> ctrl
<Control name='password-value' type='password'>
>>> verifyObject(interfaces.IControl, ctrl)
True
>>> ctrl.value
'Password'
>>> ctrl.value = 'pass now'
>>> ctrl.value
'pass now'
>>> ctrl.disabled
False
>>> ctrl.multiple
False
- Hidden Control
>>> ctrl = browser.getControl(name='hidden-value')
>>> ctrl
<Control name='hidden-value' type='hidden'>
>>> verifyObject(interfaces.IControl, ctrl)
True
>>> ctrl.value
'Hidden'
>>> ctrl.value = 'More Hidden'
>>> ctrl.disabled
False
>>> ctrl.multiple
False
- Text Area Control
>>> ctrl = browser.getControl('Text Area Control')
>>> ctrl
<Control name='textarea-value' type='textarea'>
>>> verifyObject(interfaces.IControl, ctrl)
True
>>> ctrl.value
'\n Text inside\n area!\n '
>>> ctrl.value = 'A lot of\n text.'
>>> ctrl.disabled
False
>>> ctrl.multiple
False
- File Control
The minimum setup required for file controls is to assign a file-like
object to the control's ``value`` attribute:
>>> ctrl = browser.getControl('File Control')
>>> ctrl
<Control name='file-value' type='file'>
>>> verifyObject(interfaces.IControl, ctrl)
True
>>> ctrl.value is None
True
>>> import cStringIO
>>> ctrl.value = cStringIO.StringIO('File contents')
The file control's content type and file name can also be set:
>>> ctrl.filename = 'test.txt'
>>> ctrl.content_type = 'text/plain'
The file control (like the other controls) also knows if it is disabled
or if it can have multiple values.
>>> ctrl.disabled
False
>>> ctrl.multiple
False
- Selection Control (Single-Valued)
>>> ctrl = browser.getControl('Single Select Control')
>>> ctrl
<ListControl name='single-select-value' type='select'>
>>> verifyObject(interfaces.IListControl, ctrl)
True
>>> ctrl.value
['1']
>>> ctrl.value = ['2']
>>> ctrl.disabled
False
>>> ctrl.multiple
False
>>> ctrl.options
['1', '2', '3']
>>> ctrl.displayOptions
['Uno', 'Dos', 'Third']
>>> ctrl.displayValue
['Dos']
>>> ctrl.displayValue = ['Tres']
>>> ctrl.displayValue
['Third']
>>> ctrl.displayValue = ['Dos']
>>> ctrl.displayValue
['Dos']
>>> ctrl.displayValue = ['Third']
>>> ctrl.displayValue
['Third']
>>> ctrl.value
['3']
- Selection Control (Multi-Valued)
This was already demonstrated in the introduction to control objects above.
- Checkbox Control (Single-Valued; Unvalued)
>>> ctrl = browser.getControl(name='single-unvalued-checkbox-value')
>>> ctrl
<ListControl name='single-unvalued-checkbox-value' type='checkbox'>
>>> verifyObject(interfaces.IListControl, ctrl)
True
>>> ctrl.value
True
>>> ctrl.value = False
>>> ctrl.disabled
False
>>> ctrl.multiple
True
>>> ctrl.options
[True]
>>> ctrl.displayOptions
['Single Unvalued Checkbox']
>>> ctrl.displayValue
[]
>>> verifyObject(
... interfaces.IItemControl,
... browser.getControl('Single Unvalued Checkbox'))
True
>>> browser.getControl('Single Unvalued Checkbox').optionValue
'on'
>>> browser.getControl('Single Unvalued Checkbox').selected
False
>>> ctrl.displayValue = ['Single Unvalued Checkbox']
>>> ctrl.displayValue
['Single Unvalued Checkbox']
>>> browser.getControl('Single Unvalued Checkbox').selected
True
>>> browser.getControl('Single Unvalued Checkbox').selected = False
>>> browser.getControl('Single Unvalued Checkbox').selected
False
>>> ctrl.displayValue
[]
>>> browser.getControl(
... name='single-disabled-unvalued-checkbox-value').disabled
True
- Checkbox Control (Single-Valued, Valued)
>>> ctrl = browser.getControl(name='single-valued-checkbox-value')
>>> ctrl
<ListControl name='single-valued-checkbox-value' type='checkbox'>
>>> verifyObject(interfaces.IListControl, ctrl)
True
>>> ctrl.value
['1']
>>> ctrl.value = []
>>> ctrl.disabled
False
>>> ctrl.multiple
True
>>> ctrl.options
['1']
>>> ctrl.displayOptions
['Single Valued Checkbox']
>>> ctrl.displayValue
[]
>>> verifyObject(
... interfaces.IItemControl,
... browser.getControl('Single Valued Checkbox'))
True
>>> browser.getControl('Single Valued Checkbox').selected
False
>>> browser.getControl('Single Valued Checkbox').optionValue
'1'
>>> ctrl.displayValue = ['Single Valued Checkbox']
>>> ctrl.displayValue
['Single Valued Checkbox']
>>> browser.getControl('Single Valued Checkbox').selected
True
>>> browser.getControl('Single Valued Checkbox').selected = False
>>> browser.getControl('Single Valued Checkbox').selected
False
>>> ctrl.displayValue
[]
- Checkbox Control (Multi-Valued)
>>> ctrl = browser.getControl(name='multi-checkbox-value')
>>> ctrl
<ListControl name='multi-checkbox-value' type='checkbox'>
>>> verifyObject(interfaces.IListControl, ctrl)
True
>>> ctrl.value
['1', '3']
>>> ctrl.value = ['1', '2']
>>> ctrl.disabled
False
>>> ctrl.multiple
True
>>> ctrl.options
['1', '2', '3']
>>> ctrl.displayOptions
['One', 'Two', 'Three']
>>> ctrl.displayValue
['One', 'Two']
>>> ctrl.displayValue = ['Two']
>>> ctrl.value
['2']
>>> browser.getControl('Two').optionValue
'2'
>>> browser.getControl('Two').selected
True
>>> verifyObject(interfaces.IItemControl, browser.getControl('Two'))
True
>>> browser.getControl('Three').selected = True
>>> browser.getControl('Three').selected
True
>>> browser.getControl('Two').selected
True
>>> ctrl.value
['2', '3']
>>> browser.getControl('Two').selected = False
>>> ctrl.value
['3']
>>> browser.getControl('Three').selected = False
>>> ctrl.value
[]
- Radio Control
This is how you get a radio button based control:
>>> ctrl = browser.getControl(name='radio-value')
This shows the existing value of the control, as it was in the
HTML recieved from the server:
>>> ctrl.value
['2']
We can then unselect it:
>>> ctrl.value = []
>>> ctrl.value
[]
We can also reselect it:
>>> ctrl.value = ['2']
>>> ctrl.value
['2']
displayValue shows the text the user would see next to the
control:
>>> ctrl.displayValue
['Zwei']
This is just unit testing:
>>> ctrl
<ListControl name='radio-value' type='radio'>
>>> verifyObject(interfaces.IListControl, ctrl)
True
>>> ctrl.disabled
False
>>> ctrl.multiple
False
>>> ctrl.options
['1', '2', '3']
>>> ctrl.displayOptions
['Ein', 'Zwei', 'Drei']
>>> ctrl.displayValue = ['Ein']
>>> ctrl.value
['1']
>>> ctrl.displayValue
['Ein']
The radio control subcontrols were illustrated above.
- Image Control
>>> ctrl = browser.getControl(name='image-value')
>>> ctrl
<ImageControl name='image-value' type='image'>
>>> verifyObject(interfaces.IImageSubmitControl, ctrl)
True
>>> ctrl.value
''
>>> ctrl.disabled
False
>>> ctrl.multiple
False
- Submit Control
>>> ctrl = browser.getControl(name='submit-value')
>>> ctrl
<SubmitControl name='submit-value' type='submit'>
>>> browser.getControl('Submit This') # value of submit button is a label
<SubmitControl name='submit-value' type='submit'>
>>> browser.getControl('Standard Submit Control') # label tag is legal
<SubmitControl name='submit-value' type='submit'>
>>> browser.getControl('Submit') # multiple labels, but same control
<SubmitControl name='submit-value' type='submit'>
>>> verifyObject(interfaces.ISubmitControl, ctrl)
True
>>> ctrl.value
'Submit This'
>>> ctrl.disabled
False
>>> ctrl.multiple
False
Using Submitting Controls
~~~~~~~~~~~~~~~~~~~~~~~~~
Both the submit and image type should be clickable and submit the form:
>>> browser.getControl('Text Control').value = 'Other Text'
>>> browser.getControl('Submit').click()
>>> print browser.contents
<html>
...
<em>Other Text</em>
<input type="text" name="text-value" id="text-value" value="Some Text" />
...
<em>Submit This</em>
<input type="submit" name="submit-value" id="submit-value" value="Submit This" />
...
</html>
Note that if you click a submit object after the associated page has expired,
you will get an error.
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> ctrl = browser.getControl('Submit')
>>> ctrl.click()
>>> ctrl.click()
Traceback (most recent call last):
...
ExpiredError
All the above also holds true for the image control:
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> browser.getControl('Text Control').value = 'Other Text'
>>> browser.getControl(name='image-value').click()
>>> print browser.contents
<html>
...
<em>Other Text</em>
<input type="text" name="text-value" id="text-value" value="Some Text" />
...
<em>1</em>
<em>1</em>
<input type="image" name="image-value" id="image-value"
src="zope3logo.gif" />
...
</html>
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> ctrl = browser.getControl(name='image-value')
>>> ctrl.click()
>>> ctrl.click()
Traceback (most recent call last):
...
ExpiredError
But when sending an image, you can also specify the coordinate you clicked:
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> browser.getControl(name='image-value').click((50,25))
>>> print browser.contents
<html>
...
<em>50</em>
<em>25</em>
<input type="image" name="image-value" id="image-value"
src="zope3logo.gif" />
...
</html>
Forms
-----
Because pages can have multiple forms with like-named controls, it is sometimes
neccesary to access forms by name or id. The browser's `forms` attribute can
be used to do so. The key value is the form's name or id. If more than one
form has the same name or id, the first one will be returned.
>>> browser.open('http://localhost/@@/testbrowser/forms.html')
>>> form = browser.getForm(name='one')
Form instances conform to the IForm interface.
>>> verifyObject(interfaces.IForm, form)
True
The form exposes several attributes related to forms:
- The name of the form:
>>> form.name
'one'
- The id of the form:
>>> form.id
'1'
- The action (target URL) when the form is submitted:
>>> form.action
'http://localhost/@@/testbrowser/forms.html'
- The method (HTTP verb) used to transmit the form data:
>>> form.method
'POST'
- The encoding type of the form data:
>>> form.enctype
'multipart/form-data'