======================
Detailed Documentation
======================
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``:
>>> from zope.testbrowser.testing import Browser
>>> browser = Browser()
An initial 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 accessed as a string:
>>> print browser.headers
Status: 200 OK
Content-Length: 123
Content-Type: text/html;charset=utf-8
X-Powered-By: Zope (www.zope.org), Python (www.python.org)
Or as a mapping:
>>> browser.headers['content-type']
'text/html;charset=utf-8'
Cookies
-------
When a Set-Cookie header is available, it can be found in the headers, as seen
above. Here, we use a view that will make the server set cookies with the
values we provide.
>>> browser.open('http://localhost/set_cookie.html?name=foo&value=bar')
>>> browser.headers['set-cookie'].replace(';', '')
'foo=bar'
It is also available in the browser's ``cookies`` attribute. This is
an extended mapping interface that allows getting, setting, and deleting the
cookies that the browser is remembering *for the current url*. Here are
a few examples.
>>> browser.cookies['foo']
'bar'
>>> browser.cookies.keys()
['foo']
>>> browser.cookies.values()
['bar']
>>> browser.cookies.items()
[('foo', 'bar')]
>>> 'foo' in browser.cookies
True
>>> 'bar' in browser.cookies
False
>>> len(browser.cookies)
1
>>> print(dict(browser.cookies))
{'foo': 'bar'}
>>> browser.cookies['sha'] = 'zam'
>>> len(browser.cookies)
2
>>> sorted(browser.cookies.items())
[('foo', 'bar'), ('sha', 'zam')]
>>> browser.open('http://localhost/get_cookie.html')
>>> print browser.headers.get('set-cookie')
None
>>> print browser.contents # server got the cookie change
foo: bar
sha: zam
>>> sorted(browser.cookies.items())
[('foo', 'bar'), ('sha', 'zam')]
>>> browser.cookies.clearAll()
>>> len(browser.cookies)
0
Many more examples, and a discussion of the additional methods available, can
be found in cookies.txt.
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 browser will navigate to the referenced 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>...'
When a link text matches more than one link, by default the first one is
chosen. You can, ho
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共38个文件
txt:11个
py:10个
html:9个
资源分类:Python库 所属语言:Python 资源全名:zope.testbrowser-3.6.0a2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
zope.testbrowser-3.6.0a2.tar.gz (38个子文件)
zope.testbrowser-3.6.0a2
PKG-INFO 51KB
README.txt 237B
CHANGES.txt 2KB
bootstrap.py 2KB
src
zope
testbrowser
over_the_wire.txt 1KB
README.txt 39KB
fixed-bugs.txt 3KB
testing.py 6KB
cookies.py 13KB
browser.py 25KB
ftests
controls.html 7KB
forms.html 1KB
radio.html 426B
zope3logo.gif 1KB
notitle.html 55B
oneform.html 373B
__init__.py 1KB
fragment.html 112B
textarea.html 439B
simple.html 109B
navigate.html 1KB
ftesting.zcml 1KB
cookies.txt 26KB
interfaces.py 16KB
DEPENDENCIES.cfg 76B
__init__.py 744B
tests.py 13KB
__init__.py 200B
zope.testbrowser.egg-info
PKG-INFO 51KB
requires.txt 185B
not-zip-safe 1B
SOURCES.txt 1KB
top_level.txt 5B
namespace_packages.txt 5B
dependency_links.txt 1B
setup.cfg 59B
setup.py 2KB
buildout.cfg 525B
共 38 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功