Skip to content

Commit

Permalink
Added comprehensive navigation tests of the click method.
Browse files Browse the repository at this point in the history
  • Loading branch information
strichter committed Jul 27, 2005
1 parent 8b49fad commit d2bbc86
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 12 deletions.
116 changes: 105 additions & 11 deletions testbrowser/README.txt
Expand Up @@ -93,12 +93,10 @@ Headers
-------

As you can see, the `contents` of the browser does not return any HTTP
headers. The headers are accessible via a separate attribute:
headers. The headers are accessible via a separate attribute, which is an
``httplib.HTTPMessage`` instance:

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

The page's headers are also available as an httplib.HTTPMessage instance:

>>> browser.headers
<httplib.HTTPMessage instance...>

Expand All @@ -121,24 +119,119 @@ Or as a mapping:
Navigation
----------

>>> browser.open('http://localhost/++etc++site/default')
If you want to simulate clicking on a link, there is a `click` method. In the
`navigate.html` file there are several links setup to demonstrate the
capabilities of the `click` method.

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

The simplest is to access the link by the text value that is linked, in other
words the linked text you would see in a browser:

>>> print browser.contents
<html>
...
<a href="navigate.html?message=By+Link+Text">Link Text</a>
...

>>> browser.click('Link Text')
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text'
>>> print browser.contents
<html>
...
Message: <em>By Link Text</em>
...

You can also find the link by (1) its URL,

>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> print browser.contents
<html>
...
<a href="navigate.html?message=By+URL">Using the URL</a>
...

>>> browser.click(url='\?message=By\+URL')
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+URL'
>>> print browser.contents
<html>
...
Message: <em>By URL</em>
...

and (2) its id:

>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> print browser.contents
<html>
...
<a href="navigate.html?message=By+Id" id="anchorid">By Anchor Id</a>
...

>>> browser.click(id='anchorid')
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=By+Id'
>>> print browser.contents
<html>
...
Message: <em>By Id</em>
...

But there are more interesting cases. You can also use the `click` method to
submit forms. You can either use the submit button's value by simply
specifying the text:

If you want to simulate clicking on a link, there is a `click` method.
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> print browser.contents
<html>
...
<input type="submit" name="submit-form" value="Submit" />
...

>>> browser.click('RootErrorReportingUtility')
>>> browser.click('Submit')
>>> browser.url
'http://localhost/++etc++site/default/RootErrorReportingUtility'
'http://localhost/@@/testbrowser/navigate.html'
>>> print browser.contents
<html>
...
Message: <em>By Form Submit</em>
...

We'll navigate to a form and fill in some values and the submit the form.
Alternatively, you can specify the name of the control:

>>> browser.click('Configure')
>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.click(name='submit-form')
>>> browser.url
'http://localhost/++etc++site/default/RootErrorReportingUtility/@@configure.html'
'http://localhost/@@/testbrowser/navigate.html'
>>> print browser.contents
<html>
...
Message: <em>By Form Submit</em>
...

You thought we were done here? Not so quickly. The `click` method also
supports image maps, though not by specifying the coordinates, but using the
area's title (or other tag attgributes):

>>> browser.open('http://localhost/@@/testbrowser/navigate.html')
>>> browser.click(id='zope3')
>>> browser.url
'http://localhost/@@/testbrowser/navigate.html?message=Zope+3+Name'
>>> print browser.contents
<html>
...
Message: <em>Zope 3 Name</em>
...


Forms
-----

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


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

>>> browser.controls['keep_entries']
Expand Down Expand Up @@ -176,6 +269,7 @@ a dictionary:
>>> browser.controls['copy_to_zlog']
False


Finding Specific Forms
----------------------

Expand Down
3 changes: 2 additions & 1 deletion testbrowser/browser.py
Expand Up @@ -128,7 +128,8 @@ def click(self, text=None, url=None, id=None, name=None, coord=(1,1)):
# a regular link

if id is not None:
predicate = lambda link: link.attrs.get('id') == id
def predicate(link):
return dict(link.attrs).get('id') == id
self.mech_browser.follow_link(predicate=predicate)
else:
if text is not None:
Expand Down
34 changes: 34 additions & 0 deletions testbrowser/ftests/navigate.html
@@ -0,0 +1,34 @@
<html>
<body>

<h1>Navigation Tests</h1>

<p tal:condition="request/message|nothing">
Message: <em tal:content="request/message">Message</em>
</p>

<a href="navigate.html?message=By+Link+Text">Link Text</a>

<a href="navigate.html?message=By+URL">Using the URL</a>

<a href="navigate.html?message=By+Name" name="anchorname">By Anchor Name</a>

<a href="navigate.html?message=By+Id" id="anchorid">By Anchor Id</a>

<form action="navigate.html" method="post">
<input name="message" value="By Form Submit" />
<input type="submit" name="submit-form" value="Submit" />
</form>

<img src="./zope3logo.gif" usemap="#zope3logo" />
<map name="zope3logo">
<area shape="rect" alt="Zope3"
href="navigate.html?message=Zope+3+Name" id="zope3" title="Zope 3"
coords="44,7,134,33" />
<area shape="circle" alt="Logo"
href="navigate.html?message=Logo" id="logo" title="Logo"
coords="23,21,18" />
</map>

</body>
</html>

0 comments on commit d2bbc86

Please sign in to comment.