zope.testbrowser的扩展
z3c.etestbrowser的Python项目详细描述
Zope 3 TestBrowser的扩展
这个包旨在提供zope 3的扩展版本 测试浏览器 。尤其是那些将依赖性引入 外部产品,如lxml。
内容
- 用于Zope 3 TestBrowser的扩展
-
扩展:lxml支持
- 扩展测试浏览器
- 要求
- etree支持
- 严格的XML
- lxml unicode支持
- 无效的XML/HTML响应
- html/xml规范化
- 3.0.1(2019-03-05)
- 3.0(2019-03-04)
- 2.0.1(2015-11-09)
- 2.0.0(2011-10-13)
- 1.5.0(2010-08-22)
- 1.4.0(2010-07-08)
- 1.3.1(2010-01-18)
- 1.3.0(2009-07-23)
- 1.2.0(2008-05-29)
扩展:lxml支持
所有HTML页面都被解析并作为元素树提供。
这个包提供了对zope.testbrowser的一些扩展。这些不是 包含在核心中,因为它们有额外的依赖项,例如 lxml
要求
< Buff行情>
-
lxml
etree支持
扩展测试浏览器允许将请求的结果解析为 etree使用lxml(如果内容类型是text/html或text/xml)。
这有助于使用xpath对web页面执行更详细的分析 以及相关的XML技术。
示例:
< Buff行情>>>> from z3c.etestbrowser.testing import ExtendedTestBrowser >>> browser = ExtendedTestBrowser() >>> browser.open("http://localhost/") >>> print(browser.contents) <!DOCTYPE ...> ... </html> >>> browser.etree <Element html at ...> >>> browser.etree.xpath('//body') [<Element body at ...>]
严格的XML
可以强制测试浏览器使用XML解析器:
< Buff行情>>>> browser.xml_strict False >>> browser.xml_strict = True >>> browser.open("http://localhost/") >>> browser.etree <Element {http://www.w3.org/1999/xhtml}html at ...> >>> browser.etree.xpath( ... '//html:body', namespaces={'html': 'http://www.w3.org/1999/xhtml'}) [<Element {http://www.w3.org/1999/xhtml}body at ...>]
支持lxml unicode
libxml2的几个变体可以解释utf-8编码的字符串 不正确。我们有办法解决这个问题。让我们看看 包含德语元音变调:
< Buff行情>>>> browser.xml_strict = False >>> browser.open('http://localhost/lxml.html') >>> browser.etree.xpath("//span")[0].text == u'K\xfcgelblitz.' True
无效的xml/html响应
包含无效xml/html正文的响应将在以下情况下导致错误: 访问etree或normalized_contents属性,但将为 一般测试浏览器使用:
< Buff行情>>>> browser.open("http://localhost/empty.html") >>> browser.contents '' >>> browser.etree Traceback (most recent call last): ValueError: ... >>> browser.normalized_contents Traceback (most recent call last): ValueError: ...
HTML/XML规范化
扩展测试浏览器允许HTML和XML的规范化输出,这使得 当一些不重要的细节比如 空白正在改变:
< Buff行情>>>> browser.open('http://localhost/funny.html') >>> print(browser.contents) <html> <head> <title>Foo</title> </head> <body> <h1> Title </h1> </body> </html> <BLANKLINE>
与
< Buff行情>>>> print(browser.normalized_contents) <html> <head> <title>Foo</title> </head> <body> <h1> Title </h1> </body> </html>
不推荐对zope.testbrowser.wsgi的特殊支持
在 z3c.etestbrowser.wsgi 中也有一个变体,可用于 zope.testbrowser的wsgi变体。已经没有必要了,因为。 z3c.etestbrowser.testing 现在讲wsgi。它将在下一个 主要版本。
示例:
< Buff行情>>>> import z3c.etestbrowser.wsgi >>> browser = z3c.etestbrowser.wsgi.Browser(wsgi_app=wsgi_app) >>> browser.open("http://localhost/") >>> print(browser.contents) <!DOCTYPE ...> ... </html> >>> browser.etree <Element html at ...> >>> browser.etree.xpath('//body') [<Element body at ...>]
z3c.etestbrowser.browser 模块公开了一个扩展的测试浏览器 类,该类模拟类似于Mozilla Firefox或IE的Web浏览器。
< Buff行情>>>> from z3c.etestbrowser.browser import ExtendedTestBrowser >>> browser = ExtendedTestBrowser()
它可以发送任意头;这有助于设置语言值, 因此,如果您依赖于 基于zope.i18n语言环境的格式化或类似方法。
< Buff行情>>>> browser.addHeader('Accept-Language', 'en-US')
浏览器可以打开网页 < Buff行情>
>>> from z3c.etestbrowser.testing import ExtendedTestBrowser >>> browser = ExtendedTestBrowser() >>> browser.open("http://localhost/") >>> print(browser.contents) <!DOCTYPE ...> ... </html> >>> browser.etree <Element html at ...> >>> browser.etree.xpath('//body') [<Element body at ...>]0 哎哟!谷歌不允许机器人使用他们的搜索引擎。哦,好吧。