Skip to content

Commit

Permalink
Finished up controls tests and make sure image input controls are
Browse files Browse the repository at this point in the history
recognized as submittable buttons and made sure that the coords are sent
correctly.
  • Loading branch information
strichter committed Jul 27, 2005
1 parent fd46269 commit 566888c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 38 deletions.
118 changes: 84 additions & 34 deletions testbrowser/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ simulates a web browser similar to Mozilla Firefox or IE.

The browser can `open` web pages:

>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'
>>> browser.open('http://localhost/@@/testbrowser/simple.html')
>>> browser.url
'http://localhost/@@/testbrowser/simple.html'


Page Contents
Expand All @@ -30,6 +30,7 @@ The contents of the current page are available:
<h1>Simple Page</h1>
</body>
</html>
<BLANKLINE>

Making assertions about page contents is easy.

Expand Down Expand Up @@ -107,6 +108,7 @@ The headers can be accesed as a string:
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)
<BLANKLINE>

Or as a mapping:

Expand Down Expand Up @@ -300,6 +302,13 @@ The key is matched against the value, id and name of the control. The
>>> browser.controls['password-value']
'pass now'

If we request a control that doesn't exist, an exception is raised.

>>> browser.controls['does_not_exist']
Traceback (most recent call last):
...
KeyError: 'does_not_exist'


Control Objects
~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -524,53 +533,94 @@ There are various types of controls. They are demonstrated here.
>>> ctrl.options
['1', '2', '3']

- Image Control

Forms
-----
>>> ctrl = browser.getControl('image-value')
>>> ctrl
Control(name='image-value', type='image')
>>> ctrl.value
''
>>> ctrl.disabled
False
>>> ctrl.readonly
False
>>> ctrl.multiple
>>> ctrl.options
Traceback (most recent call last):
...
AttributeError: options

>>> browser.open('http://localhost/++etc++site/default/RootErrorReportingUtility/@@configure.html')
- Submit Control

>>> ctrl = browser.getControl('submit-value')
>>> ctrl
Control(name='submit-value', type='submit')
>>> ctrl.value
'Submit'
>>> ctrl.disabled
False
>>> ctrl.readonly
True
>>> ctrl.multiple
>>> ctrl.options
Traceback (most recent call last):
...
AttributeError: options

The current page has a form on it, let's look at some of the controls:

>>> browser.controls['keep_entries']
'20'
>>> browser.controls['copy_to_zlog']
False
Using Submitting Controls
~~~~~~~~~~~~~~~~~~~~~~~~~

If we request a control that doesn't exist, an exception is raised.
Both, the submit and image type, should be clickable and submit the form:

>>> browser.controls['does_not_exist']
Traceback (most recent call last):
>>> browser.controls['text-value'] = 'Other Text'
>>> browser.click('Submit')
>>> print browser.contents
<html>
...
KeyError: 'does_not_exist'
<em>Other Text</em>
<input type="text" name="text-value" value="Some Text" />
...
<em>Submit</em>
<input type="submit" name="submit-value" value="Submit" />
...
</html>

We want to change some of the form values and submit.
And also with the image value:

>>> browser.controls['keep_entries'] = '40'
>>> browser.controls['copy_to_zlog'] = True
>>> browser.click('Save Changes')
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> browser.controls['text-value'] = 'Other Text'
>>> browser.click(name='image-value')
>>> print browser.contents
<html>
...
<em>Other Text</em>
<input type="text" name="text-value" value="Some Text" />
...
<em>1</em>
<em>1</em>
<input type="image" name="image-value" src="zope3logo.gif" />
...
</html>

Are our changes reflected on the resulting page?
But when sending an image, you can also specify the coordinate you clicked:

>>> browser.controls['keep_entries']
'40'
>>> browser.controls['copy_to_zlog']
True
>>> browser.open('http://localhost/@@/testbrowser/controls.html')
>>> browser.click(name='image-value', coord=(50,25))
>>> print browser.contents
<html>
...
<em>50</em>
<em>25</em>
<input type="image" name="image-value" src="zope3logo.gif" />
...
</html>

The `controls` object also has an `update()` method similar to that of
a dictionary:

>>> browser.controls.update(dict(keep_entries='30', copy_to_zlog=False))
>>> browser.click('Save Changes')
>>> browser.controls['keep_entries']
'30'
>>> browser.controls['copy_to_zlog']
False
Forms
-----


Finding Specific 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
Expand Down
4 changes: 4 additions & 0 deletions testbrowser/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def addHeader(self, key, value):

def click(self, text=None, url=None, id=None, name=None, coord=(1,1)):
"""See zope.app.testing.testbrowser.interfaces.IBrowser"""
# Determine whether the click is a form submit and click the submit
# button if this is the case.
form, control = self._findControl(text, id, name, type='submit')
if control is None:
form, control = self._findControl(text, id, name, type='image')
if control is not None:
self._clickSubmit(form, control, coord)
self._changed()
Expand Down
11 changes: 9 additions & 2 deletions testbrowser/ftests/controls.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ <h1>Controls Tests</h1>
<radio name="radio-value" value="2" selected="selected" />
<radio name="radio-value" value="3" />

<input type="image" name="image" src="zope3logo.gif" />
<input type="submit" name="submit" value="Submit" />
<em tal:condition="request/image-value.x|nothing"
tal:content="request/image-value.x" />
<em tal:condition="request/image-value.y|nothing"
tal:content="request/image-value.y" />
<input type="image" name="image-value" src="zope3logo.gif" />

<em tal:condition="request/submit-value|nothing"
tal:content="request/submit-value" />
<input type="submit" name="submit-value" value="Submit" />
</form>

</body>
Expand Down
5 changes: 3 additions & 2 deletions testbrowser/ftests/testdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@


def test_suite():
return FunctionalDocFileSuite('../README.txt',
optionflags=doctest.NORMALIZE_WHITESPACE+doctest.ELLIPSIS)
return FunctionalDocFileSuite(
'../README.txt',
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS)

if __name__ == '__main__':
unittest.main(defaultTest='test_suite')

0 comments on commit 566888c

Please sign in to comment.