Package: zc.testbrowser
Overview========The zc.testbrowser package provides web user agents (browsers) withprogrammatic interfaces designed to be used for testing web applications,especially in conjunction with doctests.There are currently two type of testbrowser provided.  One for accessing websites via HTTP (zc.testbrowser.browser) and one that controls a Firefox webbrowser (zc.testbrowser.real).  All flavors of testbrowser have the same API.This project originates in the Zope 3 community, but is not Zope-specific (thezc namespace package stands for "Zope Corporation").Changes=======1.0a5 (2008-05-??)------------------- Bugfix: Fixed setting the value on text area for the real testbrowser. Thanks to Taylor Sittler for the bug report and fix.- Bugfix: Fixed handling of named anchor links in mechanize implementation.- Bugfix: Added test for named anchor links.- Bugfix: Added tests for the ``click()`` method of item controls.- Bugfix: Implemented item control's ``click()`` method for the real browserimplementation. Also handles disabled controls correctly now.- Bugfix: Correctly implement ``tb_set_checked()`` Javascript function for allcases of item controls.- Bugfix: Added <DOCTYPE and <html> node to browser.contents of real.Browser.- Feature: Improved test handler for HTTP to handle simple forms and multipartforms.- Feature: Improved test of file upload element; now the passed in file contentswill be submitted and the submission is tested.- Feature: Correctly implemented the ``add_file()`` method of the controlobject for the real testbrowser implementation.- Feature: Support for Firefox 3 and Mozlab 0.1.9. Testbrowser does not workwith Mozlab 0.1.8 anymore due to API changes.1.0a4 (2008-03-06)------------------- Bugfix: Fix some packaging bugs.1.0a3 (2008-03-06)------------------- Restructure: Break out some interfaces so the various Browserimplementations can tell fewer lies.- Restructure: Make waiting for asynchronous events (like page loads) explicit.1.0a2 (2008-02-15)------------------- Bugfix: removed pdb invocation on javascript error- Bugfix: text content types where wrapped in html tags in ``testbrowser.real``- Bugfix: ``getLink()`` did not wait until page is loaded in``testbrowser.real``1.0a1 (2007-09-28)------------------- First release under new name (non Zope-specific code extracted fromzope.testbrowser).. contents::Detailed Documentation======================Before being of much interest, we need to open a web page.  ``Browser``instances have a ``base`` attribute that sets the URL from which ``open``-edURLs are relative.  This lets you target tests at servers running in various,or even variable locations (like using randomly chosen ports).>>> browser = Browser()>>> browser.base = 'http://localhost:%s/' % TEST_PORT>>> browser.open('index.html')>>> browser.url'http://localhost:.../index.html'Once you have opened a web page initially, best practice for writingtestbrowser doctests suggests using `click` to navigate further (as discussedbelow), except in unusual circumstances.The test browser complies with the IBrowser interface; see``zc.testbrowser.interfaces`` for full details on the interface.>>> import zc.testbrowser.interfaces>>> from zope.interface.verify import verifyObject>>> zc.testbrowser.interfaces.IBrowser.providedBy(browser)True>>> verifyObject(zc.testbrowser.interfaces.IBrowser, browser)TruePage Contents-------------The contents of the current page are available:>>> browser.contents'...<h1>Simple Page</h1>...'Note: Unfortunately, ellipsis (...) cannot be used at the beginning of theoutput (this is a limitation of doctest).Making assertions about page contents is easy.>>> '<h1>Simple Page</h1>' in browser.contentsTrueThe doctype and <html> node are also part of the contents:>>> browser.open('doctype.html')>>> print browser.contents<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>...Checking for HTML-----------------Not all URLs return HTML.  Of course our simple page does:>>> browser.isHtmlTrueBut if we load an image (or other binary file), we do not get HTML:>>> browser.open('zope3logo.gif')>>> browser.isHtmlFalseText types are also handled.>>> browser.open('test.txt')>>> browser.isHtmlFalse>>> print browser.contentsSome text>>> browser.open('test.css')>>> browser.isHtmlFalse>>> print browser.contents* {margin: 0;padding: 0;}HTML Page Title----------------Another useful helper property is the title:>>> browser.open('index.html')>>> browser.title'Simple Page'If a page does not provide a title, it is simply ``None``:>>> browser.open('notitle.html')>>> browser.titleHowever, if the output is not HTML, then an error will occur trying to accessthe title:>>> browser.open('zope3logo.gif')>>> browser.titleTraceback (most recent call last):...BrowserStateError: not viewing HTMLNavigation and Link Objects---------------------------If you want to simulate clicking on a link, get the link and call its `click`method.  In the `navigate.html` file there are several links set up todemonstrate the capabilities of the link objects and their `click` method.The simplest way to get a link is via the anchor text.  In other wordsthe text you would see in a browser:>>> browser.open('navigate.html')>>> browser.contents'...<a href="target.html">Link Text</a>...'>>> link = browser.getLink('Link Text')>>> link<Link text='Link Text' url='http://localhost:.../target.html'>Link objects comply with the `ILink` interface.>>> verifyObject(zc.testbrowser.interfaces.ILink, link)TrueLinks expose several attributes for easy access.>>> link.text'Link Text'Links can be "clicked" and the browser will navigate to the referenced URL.>>> link.click()>>> browser.wait()>>> browser.url'http://localhost:.../target.html'>>> browser.contents'...This page is the target of a link...'Note that whenever you click on a link that causes a page to be reloaded, youneed to wait for the browser to finish loading the page. You do this bycalling the `wait()` method of the browser. When you click on a link thatsimply brings you to a named anchor, `wait()` should not be called, since thepage is not reloaded and the result is immediate:>>> browser.open('navigate.html')>>> link = browser.getLink('Link to Named Target')>>> link<Link text='Link to Named Target'url='http://localhost:.../navigate.html#target'>>>> link.click()>>> link.url'http://localhost:.../navigate.html#target'When finding a link by its text, whitespace is normalized.>>> browser.open('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.wait()>>> browser.url'http://localhost:.../target.html'When a link text matches more than one link, by default the first one ischosen. You can, however, specify the index of the link and thus retrieve alater matching link:>>> browser.open('navigate.html')>>> browser.getLink('Link Text')<Link text='Link Text' ...>>>> browser.getLink('Link Text', index=1)<Link text='Link Text with Whitespace Normalization (and parens)' ...>Note that clicking a link object after its browser page has expired willgenerate an error.>>> link.click()Traceback (most recent call last):...ExpiredErrorYou can also find links by URL,>>> browser.open('navigate.html')>>> browser.getLink(url='target.html').click()>>> browser.wait()>>> browser.url'http://localhost:.../target.html'or its id:>>> browser.open('navigate.html')>>> browser.contents'...<a href="target.html" id="anchorid">By Anchor Id</a>...'>>> browser.getLink(id='anchorid').click()>>> browser.wait()>>> browser.url'http://localhost:.../target.html'You thought we were done here? Not so quickly.  The `getLink` method alsosupports image maps, though not by specifying the coordinates, but using thearea's id:>>> browser.open('navigate.html')>>> link = browser.getLink(id='zope3')>>> link.click()>>> browser.wait()>>> browser.url'http://localhost:.../target.html'Getting a nonexistent link raises an exception.>>> browser.open('navigate.html')>>> browser.getLink('This does not exist')Traceback (most recent call last):...LinkNotFoundErrorOther Navigation----------------Like in any normal browser, you can reload a page:>>> browser.open('index.html')>>> browser.url'http://localhost:.../index.html'>>> browser.reload()>>> browser.url'http://localhost:.../index.html'You can also go back:>>> browser.open('notitle.html')>>> browser.url'http://localhost:.../notitle.html'>>> browser.goBack()>>> browser.url'http://localhost:.../index.html'Controls--------One of the most important features of the browser is the ability to inspectand fill in values for the controls of input forms.  To do so, let's first opena page that has a bunch of controls:>>> browser.open('controls.html')Obtaining a Control~~~~~~~~~~~~~~~~~~~You look up browser controls with the `getControl` method.  The default firstargument is `label`, and looks up the form on the basis of any associatedlabel.>>> 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'This is also true if an option in a control is ambiguous in relation tothe control itself.>>> browser.getControl('Sub-control Ambiguity')Traceback (most recent call last):...AmbiguityError: label 'Sub-control Ambiguity'Ambiguous controls may be specified using an index value.  We use the control'svalue attribute to show the two controls; this attribute is properly introducedbelow.>>> 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'>>> browser.getControl('Sub-control Ambiguity', index=0)<ListControl name='ambiguous-subcontrol' type='select'>>>> browser.getControl('Sub-control Ambiguity', index=1).optionValue'ambiguous'Label searches are against stripped, whitespace-normalized, no-tag versions ofthe text. Text applied to searches is also stripped and whitespace normalized.The search finds results if the text search finds the whole words of yourtext 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 possiblein 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'>>>> # OK: ambiguous labels, but not ambiguous control>>> browser.getControl('really')<Control name='two-labels' type='text'>A label can be connected with a control using the `for` attribute and also bycontaining 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 ofthem.>>> 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 argumentsRadio and checkbox fields are unusual in that their labels and names may pointto different objects: names point to logical collections of radio buttons orcheckboxes, but labels may only be used for individual choices within thelogical collection.  This means that obtaining a radio button by label gets adifferent object than obtaining the radio collection by name.  Select optionsmay 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' selected=True>>>> browser.getControl('One')<ItemControl name='multi-checkbox-value' type='checkbox'optionValue='1' selected=True>>>> browser.getControl('Tres')<ItemControl name='single-select-value' type='select'optionValue='3' selected=False>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(zc.testbrowser.interfaces.IControl, ctrl)TrueThey 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.disabledFalse- and a flag to tell us whether the control can have multiple values:>>> ctrl.multipleFalseAdditionally, controllers for select, radio, and checkbox provide`IListControl`.  These fields have four other attributes and an additionalmethod:>>> ctrl = browser.getControl('Multiple Select Control')>>> ctrl<ListControl name='multi-select-value' type='select'>>>> ctrl.disabledFalse>>> ctrl.multipleTrue>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True- 'options' lists all available value options.>>> [unicode(o) for o in ctrl.options][u'1', u'2', u'3']- 'displayOptions' lists all available options by label.  The 'label'attribute on an option has precedence over its contents, which is whyour last option is 'Third' in the display.>>> ctrl.displayOptions['Un', 'Deux', 'Third']- 'displayValue' lets you get and set the displayed values of the controlof the select box, rather than the actual values.>>> ctrl.value[]>>> ctrl.displayValue[]>>> ctrl.displayValue = ['Un', 'Deux']>>> ctrl.displayValue['Un', 'Deux']>>> [unicode(v) for v in ctrl.value][u'1', u'2']- 'controls' 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' selected=True>,<ItemControl name='multi-select-value' type='select'optionValue='2' selected=True>,<ItemControl name='multi-select-value' type='select'optionValue='3' selected=False>]- The `getControl()` method lets you get subcontrols by their label or theirvalue.>>> ctrl.getControl('Un')<ItemControl name='multi-select-value' type='select'optionValue='1' selected=True>>>> ctrl.getControl('Deux')<ItemControl name='multi-select-value' type='select'optionValue='2' selected=True>>>> ctrl.getControl('Trois') # label attribute<ItemControl name='multi-select-value' type='select'optionValue='3' selected=False>>>> ctrl.getControl('Third') # contents<ItemControl name='multi-select-value' type='select'optionValue='3' selected=False>>>> browser.getControl('Third') # ambiguous in the browser, so usefulTraceback (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 coordinatesargument, which is a tuple of (x, y).  These submit the forms, and aredemonstrated below as we examine each control individually.ItemControl Objects~~~~~~~~~~~~~~~~~~~As introduced briefly above, using labels to obtain elements of a logicalradio button or checkbox collection returns item controls, which are parents.Manipulating the value of these controls affects the parent control.>>> [unicode(v) for v in browser.getControl(name='radio-value').value][u'2']>>> browser.getControl('Zwei').optionValue # read-only.'2'>>> browser.getControl('Zwei').selectedTrue>>> verifyObject(zc.testbrowser.interfaces.IItemControl,...     browser.getControl('Zwei'))True>>> browser.getControl('Ein').selectedFalse>>> browser.getControl('Ein').selected = True>>> browser.getControl('Ein').selectedTrueOf course at this point the previously selected "Zwei" will be unselectedsince only one radio button can be selected.>>> browser.getControl('Zwei').selectedFalse>>> browser.getControl('Zwei').selectedFalse>>> [unicode(v) for v in browser.getControl(name='radio-value').value][u'1']This test is not valid because it is impossible (with the browser) tounselect a radio box ... one radio box (must always remain selected).  Thisused to be a test for mechanize and used to pass because mechanize didn'trealize.  And by running the level 3 tests we are running these testsunder both mechanize and the "real" browser testing.::browser.getControl('Ein').selected = Falsebrowser.getControl('Ein').selectedFalsebrowser.getControl(name='radio-value').value[]>>> browser.getControl('Zwei').selected = TrueCheckbox collections behave similarly, as shown below.Controls with subcontrols--Various Controls~~~~~~~~~~~~~~~~The various types of controls are demonstrated here.- Text ControlThe text control we already introduced above.- Password Control>>> ctrl = browser.getControl('Password Control')>>> ctrl<Control name='password-value' type='password'>>>> verifyObject(zc.testbrowser.interfaces.IControl, ctrl)True>>> ctrl.value'Password'>>> ctrl.value = 'pass now'>>> ctrl.value'pass now'>>> ctrl.disabledFalse>>> ctrl.multipleFalse- Hidden Control>>> ctrl = browser.getControl(name='hidden-value')>>> ctrl<Control name='hidden-value' type='hidden'>>>> verifyObject(zc.testbrowser.interfaces.IControl, ctrl)True>>> ctrl.value'Hidden'>>> ctrl.value = 'More Hidden'>>> ctrl.disabledFalse>>> ctrl.multipleFalse- Text Area Control>>> ctrl = browser.getControl('Text Area Control')>>> ctrl<Control name='textarea-value' type='textarea'>>>> verifyObject(zc.testbrowser.interfaces.IControl, ctrl)True>>> ctrl.value'        Text inside\n        area!\n      '>>> ctrl.value = 'A lot of\n text.'>>> ctrl.value'A lot of\n text.'>>> ctrl.disabledFalse>>> ctrl.multipleFalse- File ControlFile controls are used when a form has a file-upload field.To specify data, call the add_file method, passing:- A file-like object- a content type, and- a file name>>> ctrl = browser.getControl('File Control')>>> ctrl<Control name='file-value' type='file'>>>> verifyObject(zc.testbrowser.interfaces.IControl, ctrl)True>>> ctrl.value is NoneTrue>>> import cStringIO>>> ctrl.add_file(cStringIO.StringIO('File contents'),...               'text/plain', 'test.txt')The file control (like the other controls) also knows if it is disabledor if it can have multiple values.>>> ctrl.disabledFalse>>> ctrl.multipleFalse- Selection Control (Single-Valued)>>> ctrl = browser.getControl('Single Select Control')>>> ctrl<ListControl name='single-select-value' type='select'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> [unicode(v) for v in ctrl.value][u'1']>>> ctrl.value = ['2']>>> ctrl.disabledFalse>>> ctrl.multipleFalse>>> [unicode(o) for o in ctrl.options][u'1', u'2', u'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']>>> [unicode(v) for v in ctrl.value][u'3']>>> ctrl.getControl('Dos')<ItemControl name='single-select-value' type='select'optionValue='2' selected=False>>>> ctrl.getControl('Dos').click()>>> ctrl.getControl('Dos')<ItemControl name='single-select-value' type='select'optionValue='2' selected=True>>>> ctrl.getControl('Tres').click()>>> ctrl.getControl('Dos')<ItemControl name='single-select-value' type='select'optionValue='2' selected=False>>>> ctrl.getControl('Tres')<ItemControl name='single-select-value' type='select'optionValue='3' selected=True>>>> ctrl.getControl('Tres').click()>>> ctrl.getControl('Tres')<ItemControl name='single-select-value' type='select'optionValue='3' selected=False>- Selection Control (Multi-Valued)>>> ctrl = browser.getControl('Multiple Select Control')>>> ctrl<ListControl name='multi-select-value' type='select'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> [unicode(v) for v in ctrl.value][u'1', u'2']>>> ctrl.value = ['1', '3']>>> [unicode(v) for v in ctrl.displayValue][u'Un', u'Third']>>> ctrl.value = []>>> ctrl.getControl('Deux').click()>>> ctrl.getControl('Deux')<ItemControl name='multi-select-value' type='select'optionValue='2' selected=True>>>> ctrl.getControl('Trois').click()>>> ctrl.getControl('Deux')<ItemControl name='multi-select-value' type='select'optionValue='2' selected=True>>>> ctrl.getControl('Trois')<ItemControl name='multi-select-value' type='select'optionValue='3' selected=True>>>> ctrl.getControl('Trois').click()>>> ctrl.getControl('Trois')<ItemControl name='multi-select-value' type='select'optionValue='3' selected=False>- Checkbox Control (Single-Valued; Unvalued)>>> ctrl = browser.getControl(name='single-unvalued-checkbox-value')>>> ctrl<ListControl name='single-unvalued-checkbox-value' type='checkbox'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> ctrl.valueTrue>>> ctrl.value = False>>> ctrl.disabledFalse>>> ctrl.multipleTrue>>> ctrl.options[True]>>> ctrl.displayOptions['Single Unvalued Checkbox']>>> ctrl.displayValue[]>>> verifyObject(...     zc.testbrowser.interfaces.IItemControl,...     browser.getControl('Single Unvalued Checkbox'))True>>> browser.getControl('Single Unvalued Checkbox').optionValue'on'>>> browser.getControl('Single Unvalued Checkbox').selectedFalse>>> ctrl.displayValue = ['Single Unvalued Checkbox']>>> ctrl.displayValue['Single Unvalued Checkbox']>>> subctrl = ctrl.getControl('Single Unvalued Checkbox')>>> subctrl.selectedTrue>>> subctrl.selected = False>>> subctrl.selectedFalse>>> ctrl.displayValue[]>>> subctrl.click()>>> subctrl.selectedTrue>>> subctrl.click()>>> subctrl.selectedFalse>>> disabledCtrl = browser.getControl(...     name='single-disabled-unvalued-checkbox-value')>>> disabledCtrl<ListControl name='single-disabled-unvalued-checkbox-value' type='checkbox'>>>> disabledCtrl.disabledTrue>>> disabledCtrl.getControl('Single Disabled Unvalued Checkbox').click()Traceback (most recent call last):...AttributeError: item is disabled- Checkbox Control (Single-Valued, Valued)>>> ctrl = browser.getControl(name='single-valued-checkbox-value')>>> ctrl<ListControl name='single-valued-checkbox-value' type='checkbox'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> [unicode(v) for v in ctrl.value][u'1']>>> ctrl.value = []>>> ctrl.disabledFalse>>> ctrl.multipleTrue>>> [unicode(o) for o in ctrl.options][u'1']>>> ctrl.displayOptions['Single Valued Checkbox']>>> ctrl.displayValue[]>>> verifyObject(...     zc.testbrowser.interfaces.IItemControl,...     browser.getControl('Single Valued Checkbox'))True>>> browser.getControl('Single Valued Checkbox').selectedFalse>>> browser.getControl('Single Valued Checkbox').optionValue'1'>>> ctrl.displayValue = ['Single Valued Checkbox']>>> ctrl.displayValue['Single Valued Checkbox']>>> browser.getControl('Single Valued Checkbox').selectedTrue>>> browser.getControl('Single Valued Checkbox').selected = False>>> browser.getControl('Single Valued Checkbox').selectedFalse>>> ctrl.displayValue[]>>> subctrl = ctrl.getControl('Single Valued Checkbox')>>> subctrl.selectedFalse>>> subctrl.click()>>> subctrl.selectedTrue>>> subctrl.click()>>> subctrl.selectedFalse- Checkbox Control (Multi-Valued)>>> ctrl = browser.getControl(name='multi-checkbox-value')>>> ctrl<ListControl name='multi-checkbox-value' type='checkbox'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> [unicode(v) for v in ctrl.value][u'1', u'3']>>> ctrl.value = ['1', '2']>>> ctrl.disabledFalse>>> ctrl.multipleTrue>>> [unicode(o) for o in ctrl.options][u'1', u'2', u'3']>>> ctrl.displayOptions['One', 'Two', 'Three']>>> ctrl.displayValue['One', 'Two']>>> ctrl.displayValue = ['Two']>>> [unicode(v) for v in ctrl.value][u'2']>>> browser.getControl('Two').optionValue'2'>>> browser.getControl('Two').selectedTrue>>> verifyObject(zc.testbrowser.interfaces.IItemControl,...     browser.getControl('Two'))True>>> browser.getControl('Three').selected = True>>> browser.getControl('Three').selectedTrue>>> browser.getControl('Two').selectedTrue>>> [unicode(v) for v in ctrl.value][u'2', u'3']>>> browser.getControl('Two').selected = False>>> [unicode(v) for v in ctrl.value][u'3']>>> browser.getControl('Three').selected = False>>> ctrl.value[]>>> subctrl3 = browser.getControl('Three')>>> subctrl3.selectedFalse>>> subctrl3.click()>>> subctrl3.selectedTrue>>> subctrl3.click()>>> subctrl3.selectedFalse>>> browser.getControl('Two').click()>>> browser.getControl('Three').click()>>> browser.getControl('Two').selectedTrue>>> browser.getControl('Three').selectedTrue- Radio ControlThis 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 theHTML received from the server:>>> [unicode(v) for v in ctrl.value][u'2']We can then unselect it:>>> ctrl.value = []>>> ctrl.value[]We can also reselect it:>>> ctrl.value = ['2']>>> [unicode(v) for v in ctrl.value][u'2']displayValue shows the text the user would see next to thecontrol:>>> ctrl.displayValue['Zwei']This is just unit testing:>>> ctrl<ListControl name='radio-value' type='radio'>>>> verifyObject(zc.testbrowser.interfaces.IListControl, ctrl)True>>> ctrl.disabledFalse>>> ctrl.multipleFalse>>> [unicode(o) for o in ctrl.options][u'1', u'2', u'3']>>> ctrl.displayOptions['Ein', 'Zwei', 'Drei']>>> ctrl.displayValue = ['Ein']>>> [unicode(v) for v in ctrl.value][u'1']>>> ctrl.displayValue['Ein']The radio control subcontrols were illustrated above.>>> subctrl = browser.getControl('Ein')>>> subctrl.selectedTrue>>> subctrl2 = browser.getControl('Zwei')>>> subctrl2.selectedFalse>>> subctrl2.click()>>> subctrl2.selectedTrue>>> subctrl.selectedFalse- Image Control>>> ctrl = browser.getControl(name='image-value')>>> ctrl<ImageControl name='image-value' type='image'>>>> verifyObject(zc.testbrowser.interfaces.IImageSubmitControl, ctrl)True>>> ctrl.value''>>> ctrl.disabledFalse>>> ctrl.multipleFalse- 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(zc.testbrowser.interfaces.ISubmitControl, ctrl)True>>> ctrl.value'Submit This'>>> ctrl.disabledFalse>>> ctrl.multipleFalseUsing 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()>>> browser.wait()>>> browser.contents"...'text-value': ['Other Text']..."Note that if you click a submit object after the associated page has expired,you will get an error.>>> browser.open('controls.html')>>> ctrl = browser.getControl('Submit')>>> ctrl.click()>>> ctrl.click()Traceback (most recent call last):...ExpiredErrorAll the above also holds true for the image control:>>> browser.open('controls.html')>>> browser.getControl('Text Control').value = 'Other Text'>>> browser.getControl(name='image-value').click()>>> browser.wait()>>> browser.contents"...'text-value': ['Other Text']...">>> browser.open('controls.html')>>> ctrl = browser.getControl(name='image-value')>>> ctrl.click()>>> ctrl.click()Traceback (most recent call last):...ExpiredErrorBut when sending an image, you can also specify the coordinate you clicked:#    >>> browser.open('controls.html')#    >>> browser.getControl(name='image-value').click((50,25))#    >>> browser.contents#    "...'image-value.x': ['50']...'image-value.y': ['25']..."Another interesting aspect of submitting a form is the correct submission ofthe file data.>>> browser.open('controls.html')>>> ctrl = browser.getControl('File Control')>>> import cStringIO>>> ctrl.add_file(cStringIO.StringIO('File contents'),...               'text/plain', 'test.txt')>>> browser.getControl('Submit This').click()>>> browser.wait()>>> browser.contents"...'file-value': ['File contents'],..."Forms-----Because pages can have multiple forms with like-named controls, it is sometimesnecessary to access forms by name or id.  The browser's `forms` attribute canbe used to do so.  The key value is the form's name or id.  If more than oneform has the same name or id, the first one will be returned.>>> browser.open('forms.html')>>> form = browser.getForm(name='one')Form instances conform to the IForm interface.>>> verifyObject(zc.testbrowser.interfaces.IForm, form)TrueThe form exposes several attributes related to forms:- The name of the form:>>> unicode(form.name)u'one'- The id of the form:>>> unicode(form.id)u'1'- The action (target URL) when the form is submitted:>>> unicode(form.action)u'http://localhost:.../forms.html'- The method (HTTP verb) used to transmit the form data:>>> unicode(form.method)u'POST'- The encoding type of the form data:>>> unicode(form.enctype)u'application/x-www-form-urlencoded'Besides those attributes, you have also a couple of methods.  Like for thebrowser, you can get control objects, but limited to the current form...>>> form.getControl(name='text-value')<Control name='text-value' type='text'>...and submit the form.>>> form.submit('Submit')>>> browser.contents"...'text-value': ['First Text']..."Submitting also works without specifying a control, as shown below, which isit's primary reason for existing in competition with the control submissiondiscussed above.Now let me show you briefly that looking up forms is sometimes important.  Inthe `forms.html` template, we have four forms all having a text control named`text-value`.  Now, if I use the browser's `get` method,>>> browser.open('forms.html')>>> browser.getControl(name='text-value')Traceback (most recent call last):...AmbiguityError: name 'text-value'>>> browser.getControl('Text Control')Traceback (most recent call last):...AmbiguityError: label 'Text Control'I'll always get an ambiguous form field.  I can use the index argument, orwith the `getForm` method I can disambiguate by searching only within a givenform:>>> form = browser.getForm('2')>>> form.getControl(name='text-value').value'Second Text'>>> form.submit('Submit')>>> browser.contents"...'text-value': ['Second Text']...">>> browser.open('forms.html')>>> form = browser.getForm('2')>>> form.getControl('Submit').click()>>> browser.wait()>>> browser.contents"...'text-value': ['Second Text']...">>> browser.open('forms.html')>>> browser.getForm('3').getControl('Text Control').value'Third Text'The last form on the page does not have a name, an id, or a submit button.Working with it is still easy, thanks to a index attribute that guaranteesorder.  (Forms without submit buttons are sometimes useful for JavaScript.)>>> form = browser.getForm(index=3)>>> form.submit()>>> browser.contents"...'text-value': ['Fourth Text']..."If a form is requested that does not exists, an exception will be raised.>>> browser.open('forms.html')>>> form = browser.getForm('does-not-exist')Traceback (most recent call last):LookupErrorIf the HTML page contains only one form, no arguments to `getForm` areneeded:>>> browser.open('oneform.html')>>> browser.getForm()<zc.testbrowser...Form object at ...>If the HTML page contains more than one form, `index` is needed todisambiguate if no other arguments are provided:>>> browser.open('forms.html')>>> browser.getForm()Traceback (most recent call last):ValueError: if no other arguments are given, index is required.Hand-Holding------------Instances of the various objects ensure that users don't set incorrectinstance attributes accidentally.>>> browser.nonexistant = NoneTraceback (most recent call last):...AttributeError: 'Browser' object has no attribute 'nonexistant'>>> form.nonexistant = NoneTraceback (most recent call last):...AttributeError: 'Form' object has no attribute 'nonexistant'>>> control.nonexistant = NoneTraceback (most recent call last):...AttributeError: 'Control' object has no attribute 'nonexistant'>>> link.nonexistant = NoneTraceback (most recent call last):...AttributeError: 'Link' object has no attribute 'nonexistant'Fixed Bugs----------This section includes tests for bugs that were found and then fixed that don'tfit into the more documentation-centric sections above.Spaces in URL~~~~~~~~~~~~~When URLs have spaces in them, they're handled correctly (before the bug wasfixed, you'd get "ValueError: too many values to unpack"):>>> browser.open('navigate.html')>>> browser.getLink('Spaces in the URL').click()>>> browser.wait().goBack() Truncation~~~~~~~~~~~~~~~~~~~~The .goBack() method used to truncate the .contents.>>> browser.open('navigate.html')>>> actual_length = len(browser.contents)>>> browser.open('navigate.html')>>> browser.open('index.html')>>> browser.goBack()>>> len(browser.contents) == actual_lengthTrueAuthors-------Benji York created testbrowser (originally zope.testbrowser) in 2005 with GaryPoster and Stephan Richter making large contributions.The zc.testbrowser.real version was conceptualized by Benji York in 2007 andafter an initial implementation sketch, brought to fruition by StephanRichter, Rocky Burt, Justas Sadzevicius, and others at the Foliage Zope 3sprint in Boston, MA during the week of September 24, 2007.There have been many other contributions from users of testbrowser that aregreatly appreciated.