本文整理汇总了Python中twill.get_browser函数的典型用法代码示例。如果您正苦于以下问题:Python get_browser函数的具体用法?Python get_browser怎么用?Python get_browser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_browser函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_exception
def handle_exception(msg, e):
maybe_print_stack()
log_error("Caught exception at %s" %
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
html = twill.get_browser().get_html()
if options.dump_file == '-':
print html
else:
dump_file_name = os.path.expanduser(options.dump_file)
try:
if html is not None:
if options.show_error_in_browser:
# If we are showing it in the browser, lets get the
# paths right (even if if means changing the HTML a
# little)
base_href = '\n<!-- added by flunc: --><base href="%s">' % twill.get_browser().get_url()
match = re.search('<head.*?>', html, re.I|re.S)
if match:
html = html[:match.end()] + base_href + html[match.end():]
else:
html = base_href + html
f = open(dump_file_name, 'wb')
f.write(html)
f.close()
log_info("saved error html to: %s" % dump_file_name)
except IOError, e:
log_warn("Unable to save error HTML to: %s" % dump_file_name)
示例2: test_middleware_composer
def test_middleware_composer():
"""Middleware stack should alter return in order.."""
def make_middleware(txt):
def middleware(app):
def wrappedapp(environ, start_response):
res = app(environ, start_response)
res.append(txt)
return res
return wrappedapp
return middleware
# Environ predicates
t = lambda x: True
f = lambda x: False
rules = [(t, make_middleware('a')),
(f, make_middleware('b')),
(t, make_middleware('c')),
(f, make_middleware('d')),
(t, make_middleware('e'))]
def app(environ, start_response):
start_response("200 OK", [('Content-type', 'text/plain')])
return ["ok "]
composed = selector.MiddlewareComposer(app, rules)
twill.add_wsgi_intercept('simple-host', 80, lambda: composed)
browser = twill.get_browser()
browser.go('http://simple-host/endpoint')
assert browser.result.page.startswith("ok eca")
assert browser.result.http_code == 200
示例3: main
def main():
login, password = get_credentials()
# log-in to Django site
if login and password:
tw.go(LOGIN_URL)
tw.formvalue('1', 'username', login)
tw.formvalue('1', 'password', password)
tw.submit()
if isinstance(DATA_URL, basestring):
urls = [DATA_URL]
else:
urls = list(DATA_URL)
# retrieve URIs
for url in urls:
try:
tw.go(url)
tw.code('200')
tw.show()
except TwillAssertionError:
code = get_browser().get_code()
print (u"Unable to access %(url)s. "
u"Received HTTP #%(code)s."
% {'url': url, 'code': code})
tw.reset_browser()
示例4: fv_multi_match
def fv_multi_match(formname, regexp, *values):
"""
>> fv_multi_match <formname> <field regexp> <value> [<value> [<value>..]]
Set value of each consecutive matching form field with the next specified
value. If there are no more values, use the last for all remaining form
fields
"""
state = twill.get_browser()
form = state.get_form(formname)
if form is None:
print(('no such form', formname))
return
regexp = re.compile(regexp)
matches = [ ctl for ctl in form.inputs if regexp.search(str(ctl.get("name"))) ]
if matches:
print(('-- matches %d, values %d' % (len(matches), len(values))))
n = 0
for control in matches:
state.clicked(form, control)
if 'readonly' in list(control.attrib.keys()):
continue
try:
twill.utils.set_form_control_value(control, values[n])
except IndexError as e:
twill.utils.set_form_control_value(control, values[-1])
n += 1
print(('set %d values total' % (n,)))
示例5: has_multiple_values
def has_multiple_values(formname, name):
browser = twill.get_browser()
form = browser.get_form(formname)
if not form:
raise TwillAssertionError("no matching forms!")
control = browser.get_form_field(form, name)
return hasattr(control, 'items') and len(control.items) > 1
示例6: _formvalue_by_regexp_setall
def _formvalue_by_regexp_setall(formname, fieldname, value):
state = twill.get_browser()
form = state.get_form(formname)
if not form:
logger.error('no such form %s', formname)
return
regexp = re.compile(fieldname)
matches = [ ctl for ctl in form.controls if regexp.search(str(ctl.name)) ]
if matches:
logger.error('-- matches %d', len(matches))
n = 0
for control in matches:
state.clicked(form, control)
if control.readonly:
continue
n += 1
twill.utils.set_form_control_value(control, value)
logger.error('set %d values total', n)
示例7: __init__
def __init__(self):
Web2UnitTest.__init__(self)
self.b = get_browser()
self.b_data = StringIO()
set_output(self.b_data)
self.clearRecord()
# This string must exist in the URL for it to be followed
# Useful to avoid going to linked sites
self.homeURL = self.url
# Link used to identify a URL to a ticket
self.url_ticket = "/admin/default/ticket/"
# Tuple of strings that if in the URL will be ignored
# Useful to avoid dynamic URLs that trigger the same functionality
self.include_ignore = ("_language=",
"logout",
"appadmin",
"admin",
"delete",
)
# tuple of strings that should be removed from the URL before storing
# Typically this will be some variables passed in via the URL
self.strip_url = ("?_next=",
)
self.reportOnly = False
self.maxDepth = 16 # sanity check
self.setThreshold(10)
self.setUser("[email protected]/eden")
self.total_visited = 0
self.broken_links_count = 0
示例8: test_not_found
def test_not_found():
"""The "not found" handler return a 404."""
twill.add_wsgi_intercept('not-found-host', 80, lambda: not_found)
browser = twill.get_browser()
browser.go('http://not-found-host/')
assert browser.result.page.startswith("404 Not Found")
assert browser.result.http_code == 404
示例9: make_intranets
def make_intranets(intranets_name):
""" Make the offices root hierarchy, deleting if it exists"""
global_dict, local_dict = namespaces.get_twill_glocals()
global_dict['intranets_name'] = intranets_name
# Check to see if we have that community, if so, delete it.
commands.go('/' + intranets_name)
br = get_browser()
status = br.get_code()
if status != 404:
# The community shouldn't exist, and so we should get 404
# looking for it. If no 404, then it exists and we should
# delete it.
url = "/%s/delete.html?confirm=1" % intranets_name
commands.go(url)
# Now, make the community and make sure it exists
commands.go("/add_community.html")
commands.fv("save", "title", intranets_name)
desc = "Test intranets root created for Twill test case named '%s'"
commands.fv("save", "description", desc % test_name)
commands.submit()
commands.find("Add Existing")
示例10: main
def main():
joker = SpawnKillerThread()
try:
SpawnDistributedCluster()
print "Waiting for two JBoss instances to start up in cluster mode..."
time.sleep(10)
joker.start()
#commands.debug('http', 1)
#commands.debug('commands',1)
b = get_browser()
import socket
myip = "http://" + socket.gethostbyname(socket.gethostname())
b.go(myip + "/session-basket")
b.submit(5)
wordBatch = listOfRandomWords()
for idx, w in enumerate(wordBatch):
print "Adding word %d: '%s' to the list..." % (idx, w)
commands.formvalue("1", "newItem:j_idt11", w)
b.submit(4)
commands.code(200)
commands.find(w)
time.sleep(0.25)
print wordBatch, len(wordBatch)
b.submit(5)
commands.code(200)
commands.reload()
for w in wordBatch:
commands.find(w)
finally:
joker.stopKilling = True
joker.join()
KillJBoss(0)
KillJBoss(1)
示例11: __init__
def __init__(self):
self.twill_browser = twill.get_browser()
if not settings.get('verbose', True):
twill.set_output(open(os.devnull, 'w'))
#twill.browser.OUT = open(os.devnull, 'w')
# Handle HTTP authentication
if settings.get('http_auth_username', None) and settings.get('http_auth_password', None):
base64string = base64.encodestring('%s:%s' % (settings['http_auth_username'], settings['http_auth_password'])).replace('\n', '')
twill.commands.add_auth("wiki", settings['mediawiki_url'], settings['http_auth_username'], settings['http_auth_password'])
#self.twill_browser._session.headers.update([("Authorization", "Basic %s" % base64string)])
twill.commands.add_extra_header("Authorization", "Basic %s" % base64string)
# Handle Mediawiki authentication
if settings.get('mediawiki_username', None) and settings.get('mediawiki_password', None):
login_url = urlparse.urljoin(settings['mediawiki_url'], '/index.php?title=Special:UserLogin')
self.openurl(login_url)
self._set_form_value('userlogin', 'wpName', settings.get('mediawiki_username'))
self._set_form_value('userlogin', 'wpPassword', settings.get('mediawiki_password'))
self.twill_browser.submit()
self.openurl(settings['mediawiki_url'])
示例12: test_Mongodb
def test_Mongodb(self):
print "This is a test to check that the api endpoints are working properly."
# time.sleep(30)
browser = twill.get_browser()
browser.go("http://localhost:27017/")
self.assertTrue(browser.get_code() in (200, 201))
示例13: fv_match
def fv_match(formname, regexp, value):
"""
>> fv_match <formname> <field regexp> <value>
Set value of *all* form fields with a name that matches the given
regular expression.
(Unlike 'formvalue' or 'fv', this will not complain about multiple
matches!)
"""
state = twill.get_browser()
form = state.get_form(formname)
if form is None:
print(('no such form', formname))
return
regexp = re.compile(regexp)
matches = [ ctl for ctl in form.inputs if regexp.search(str(ctl.get("name"))) ]
if matches:
print(('-- matches %d' % (len(matches),)))
n = 0
for control in matches:
state.clicked(form, control)
if 'readonly' in list(control.attrib.keys()):
continue
n += 1
twill.utils.set_form_control_value(control, value)
print(('set %d values total' % (n,)))
示例14: setAgent
def setAgent(self, agentAcronym):
# Decide on the agent that will be used to power the smoke test
if agentAcronym == "g":
self.agent = "Ghost"
try:
from ghost import Ghost
self.ghost = Ghost(wait_timeout = 360)
except ImportError:
raise NameError("Ghost not installed")
from using_ghost import login, visit
else:
self.agent = "Twill"
try:
from twill import get_browser
from twill import set_output
except ImportError:
raise NameError("Twill not installed")
try:
import mechanize
except ImportError:
raise NameError("Mechanize not installed")
self.b = get_browser()
self.b_data = StringIO()
set_output(self.b_data)
from using_twill import login, visit
self.visit = MethodType(visit, self)
self.login = MethodType(login, self)
示例15: fv_match
def fv_match(formname, regexp, value):
"""
>> fv_match <formname> <field regexp> <value>
Set value of *all* form fields with a name that matches the given
regular expression.
(Unlike 'formvalue' or 'fv', this will not complain about multiple
matches!)
"""
state = twill.get_browser()
form = state.get_form(formname)
if not form:
logger.error("no such form %s", formname)
return
regexp = re.compile(regexp)
matches = [ctl for ctl in form.controls if regexp.search(str(ctl.name))]
if matches:
logger.info("-- matches %d", len(matches))
n = 0
for control in matches:
state.clicked(form, control)
if control.readonly:
continue
n += 1
twill.utils.set_form_control_value(control, value)
logger.info("set %d values total", n)