Adding testbrowser Form Inputs
What I've always wanted was a way to reproduce the effects of the dynamic manipulation in the testbrowser. When I was recently bitten by a bug that I didn't catch for lack of functional coverage of a ReferenceField, I dug into the ClientForm package that zope.testbrowser uses and found the HTMLForm.new_control() method. This is the method that ClientForm uses to construct the controls that zope.testbrowser provides lookup for.
Since Firefox allows you to inspect the DOM either by selecting the elements and using "View selection source" or with Web Developer's DOM inspector, you can ferret out what the dynamic manipulation created and use that information to construct the HTMLForm.new_control() call that will reproduce that. The following example would add the foo object to the related items on another object's edit form.
>>> form = browser.getForm(name="edit_form") >>> form.mech_form.new_control( ... type='checkbox', ... name='relatedItems:list', ... attrs=dict(checked='checked', value=foo.UID()))
See the ClientForm source for more details.
BTW, it seems the the ReferenceBrowserWidget includes an empty string on submission of the form. This empty string case seems to be handled in the field's set() method and not in the widget where it seems like it should be. Not knowing this, I implemented a custom mutator with a bug that I didn't catch because I didn't have test coverage on the form submission. Now I do. :)