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)
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)
def get_linkedin_viewer_count(username=None, password=None): from twill import get_browser from twill.commands import add_extra_header, go, fv, submit, reset_browser reset_browser() from twill.errors import TwillException add_extra_header( 'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36' ) go("https://www.linkedin.com/nhome/") # Added because LinkedIn login page no longer names the login form. b = get_browser() form_num = '' for n, f in enumerate(b.get_all_forms()): try: b.get_form_field(f, "session_key") b.get_form_field(f, "session_password") form_num = str(n + 1) except TwillException: pass if form_num == '': log.error( 'Failed to parse LinkedIn login page - page format may have changed.' ) raise LinkedInFailure() #fv("login", 'session_password', 'LetsTryPrime') #fv("login", 'session_key', '*****@*****.**') fv(form_num, 'session_key', username) fv(form_num, 'session_password', password) submit() go('http://www.linkedin.com/wvmx/profile?trk=nav_responsive_sub_nav_wvmp') try: for i in get_browser().result.lxml\ .get_element_by_id('viewers_list-content')\ .iterchildren(): user_listing = simplejson.loads(i.text.replace('\\u002d', '-')) except Exception as e: log.error('Failed to extract user_listing from page: {error}'.format( error=e)) raise LinkedInFailure() try: current_count = user_listing['content']['wvmx_profile_viewers'][ 'viewersCount'] return current_count except KeyError: log.error( 'Profile view struct in unknown format: {user_listing}'.format( user_listing=user_listing)) raise LinkedInFailure()
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
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
def test_Api(self): print "This is a test to check that the api endpoints are working properly." # assert self.app.config['MONGODB_SETTINGS'] == {'db': 'corr-integrate', 'host': '127.0.0.1', 'port': 27017} browser = twill.get_browser() browser.go("http://0.0.0.0:%d/api/v1/project0/"%self.app.config['LIVESERVER_PORT']) self.assertTrue(browser.get_code() in (401, 404))
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 not form: print 'no such form', formname return regexp = re.compile(regexp) matches = [ctl for ctl in form.controls if regexp.search(str(ctl.name))] if matches: print '-- matches %d, values %d' % (len(matches), len(values)) n = 0 for control in matches: state.clicked(form, control) if control.readonly: continue try: twill.utils.set_form_control_value(control, values[n]) except IndexError, e: twill.utils.set_form_control_value(control, values[-1]) n += 1 print 'set %d values total' % (n, )
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
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: print 'no such form', formname return regexp = re.compile(regexp) matches = [ctl for ctl in form.controls if regexp.search(str(ctl.name))] if matches: print '-- 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) print 'set %d values total' % (n, )
def test_Logic(self): print "This is a test to check that the api endpoints are working properly." # assert self.app.config['MONGODB_SETTINGS'] == {'db': 'corr-integrate', 'host': '0.0.0.0', 'port': 27017} browser = twill.get_browser() browser.go("http://0.0.0.0:%d/api/v1/project0/"%self.app.config['LIVESERVER_PORT']) self.assertTrue(browser.get_code() in (401, 404))
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)
def _formvalue_by_regexp_setall(formname, fieldname, value): state = twill.get_browser() form = state.get_form(formname) if not form: print 'no such form', formname return regexp = re.compile(fieldname) matches = [ctl for ctl in form.controls if regexp.search(str(ctl.name))] if matches: print '-- 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) print 'set %d values total' % (n, )
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'])
def __init__(self, app, host='127.0.0.1', port=5000, scheme='http'): self.app = app self.host = host self.port = port self.scheme = scheme self.browser = twill.get_browser()
def __init__(self): Web2UnitTest.__init__(self) self.b = get_browser() self.b_data = StringIO() set_output(self.b_data) # list of links that return a http_code other than 200 # with the key being the URL and the value the http code self.brokenLinks = dict() # List of links visited (key) with the depth self.urlList = dict() # List of urls for each model self.model_url = dict() # This string must exist in the URL for it to be followed # Useful to avoid going to linked sites self.homeURL = self.url # 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=", "/admin/default/", ) # 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.maxDepth = 2 # sanity check
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
def __init__(self, username, password, run_main=False): self._username = username self._password = password self._browser = get_browser() self._log_in() if run_main: self.run()
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)
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))
def test(): url = twilltestlib.get_url() # test empty page get_title namespaces.new_local_dict() twill.commands.reset_browser() browser = twill.get_browser() try: browser.get_title() assert 0, "should never get here" except TwillException: pass ### now test a few special cases commands.go(url) commands.go('/login') # test no matching forms try: commands.fv('2', 'submit', '1') assert 0 except TwillAssertionError: pass # test regexp match commands.fv('1', '.*you', '1') # test ambiguous match to value commands.go('/testform') commands.fv('1', 'selecttest', 'val') commands.fv('1', 'selecttest', 'value1') commands.fv('1', 'selecttest', 'selvalue1') commands.formclear('1') commands.showforms() try: commands.fv('1', 'selecttest', 'value') assert 0 except TwillException: pass # test ambiguous match to name commands.go('/testform') try: commands.fv('1', 'item_', 'value') assert 0 except Exception: pass try: commands.formfile('1', 'selecttest', 'null') assert 0 except Exception: pass commands.go('http://www.google.com/') browser.get_title() # test the twill script. twilltestlib.execute_twill_script('test-form.twill', initial_url=url)
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()
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 not form: logger.error("no such form", 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, values %d", len(matches), len(values)) n = 0 for control in matches: state.clicked(form, control) if control.readonly: continue try: twill.utils.set_form_control_value(control, values[n]) except IndexError, e: twill.utils.set_form_control_value(control, values[-1]) n += 1 logger.info("set %d values total", n)
def catalog_find(searchterm, urlfrag, notfind=False): """Just like Twill find, but issues a searchpage-search search""" global_dict, local_dict = namespaces.get_twill_glocals() # This will navigate us away from the place the Twill script is # sitting. Thus, stash away to the current URL, then navigate # back to that URL after searching. br = get_browser() start_url = br.get_url() esc_searchterm = urllib.quote(searchterm) url = "/searchresults.html?body=" + esc_searchterm commands.go(url) # Now do the test. With the fragment of the URL that was # provided, we can do a double-check, to make sure the # searchresults provide that. if notfind: commands.notfind(urlfrag) else: commands.find(urlfrag) # Finally, send them back to the original URL. commands.go(start_url)
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)
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)
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()
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,)))
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")
def __init__(self): Web2UnitTest.__init__(self) self.b = get_browser() self.b_data = StringIO() set_output(self.b_data) # list of links that return a http_code other than 200 # with the key being the URL and the value the http code self.brokenLinks = dict() # List of links visited (key) with the depth self.urlList = dict() # List of urls for each model self.model_url = dict() # This string must exist in the URL for it to be followed # Useful to avoid going to linked sites self.homeURL = self.url # 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=", "/admin/default/", ) # 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.maxDepth = 2 # sanity check
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,)))
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")
def openPage(page) : global browser if not browser : browser = twill.get_browser() redirect_output("/dev/null") browser.go("http://localhost:%s%s" % (PORT, page)) return browser.get_html()
def test_Logic(self): # time.sleep(30) browser = twill.get_browser() browser.go("http://0.0.0.0:%d/" % self.app.config['LIVESERVER_PORT']) self.assertTrue(browser.get_code() in (200, 201)) html = browser.get_html() # self.assertTrue(html.count('slashdot') > 150) print "This is a test to check that the api endpoints are working properly."
def openPage(page): global browser if not browser: browser = twill.get_browser() redirect_output("/dev/null") browser.go("http://localhost:%s%s" % (PORT, page)) return browser.get_html()
def _pre_setup(self): super(TwillTestCase, self)._pre_setup() twill.set_output(StringIO.StringIO()) twill.commands.clear_cookies() twill.add_wsgi_intercept(self.twill_host, self.twill_port, lambda: self.app) self.browser = twill.get_browser()
def get_issues(base_url, username, password): b = get_browser() _login(b, base_url, username, password) b.go("%s/issues.atom?assigned_to_id=me&set_filter=1" % base_url) rss_str = b.get_html() return feedparser.parse(rss_str)
def test_Logic(self): # time.sleep(30) browser = twill.get_browser() browser.go("http://0.0.0.0:%d/"%self.app.config['LIVESERVER_PORT']) self.assertTrue(browser.get_code() in (200, 201)) html = browser.get_html() # self.assertTrue(html.count('slashdot') > 150) print "This is a test to check that the api endpoints are working properly."
def __create_session(self): browser = get_browser() browser.go('http://www.erstenachhilfe.de/user?destination=node%2F767') tw.fv('2', 'edit-name', LOGIN) tw.fv('2', 'edit-pass', PASSWORD) tw.showforms() tw.submit('op') return browser
def setUp(self): #True if dev, False if dep. Change this alone to test on the other page #If on dep, need to go to django admin and delete test_reg_user1,2,3 after self.cur = True if (self.cur): self.pg = "http://localhost:8000" copy("../project/savegame.db", "../project/savegame.db.copy") else: self.pg = "http://savegame.eudisduran.com" self.b = get_browser()
def test_method_not_allowed(): """The "method not allowed" handler return a 405.""" def app(environ, start_response): environ['selector.methods'] = ['GET', 'PUT'] return method_not_allowed(environ, start_response) twill.add_wsgi_intercept('not-found-host', 80, lambda: app) browser = twill.get_browser() browser.go('http://not-found-host/') assert browser.result.page.startswith("405 Method Not Allowed") assert browser.result.http_code == 405
def get_linkedin_viewer_count(username=None, password=None): from twill import get_browser from twill.commands import add_extra_header, go, fv, submit, reset_browser reset_browser() from twill.errors import TwillException add_extra_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36') go("https://www.linkedin.com/nhome/") # Added because LinkedIn login page no longer names the login form. b = get_browser() form_num = '' for n, f in enumerate(b.get_all_forms()): try: b.get_form_field(f, "session_key") b.get_form_field(f, "session_password") form_num = str(n+1) except TwillException: pass if form_num == '': log.err('Failed to parse LinkedIn login page - page format may have changed.') raise LinkedInFailure() #fv("login", 'session_password', 'LetsTryPrime') #fv("login", 'session_key', '*****@*****.**') fv(form_num, 'session_key', username) fv(form_num, 'session_password', password) submit() go('http://www.linkedin.com/wvmx/profile?trk=nav_responsive_sub_nav_wvmp') try: for i in get_browser().result.lxml\ .get_element_by_id('viewers_list-content')\ .iterchildren(): user_listing = simplejson.loads(i.text.replace('\\u002d','-')) except Exception as e: log.err('Failed to extract user_listing from page: {error}'.format(error=e)) raise LinkedInFailure() try: current_count = user_listing['content']['wvmx_profile_viewers']['viewersCount'] return current_count except KeyError: log.err('Profile view struct in unknown format: {user_listing}'.format(user_listing=user_listing)) raise LinkedInFailure()
def testi18n(): test = """ debug commands 1 go /test_i18n.html code 200 find 'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3\xb8n' """ twill.execute_string(test, initial_url='http://localhost:4444') browser = twill.get_browser() print dir(browser)
def xpath(expr, result): """Given an XPath expression, see if it returns the result""" br = get_browser() xhtml = br.get_html() et = document_fromstring(xhtml) xpath_result = str(et.xpath(expr)) if xpath_result!=result: msg = "XPath expression '%s' returned %s instead of %s" raise TwillAssertionError(msg % (expr, xpath_result, result))
def exit_if_empty(): """ >> exit_if_empty Exit the script currently running, if there are no deferred messages on the current page. """ state = twill.get_browser() form = state.get_form("1") if not form: print "No messages; exiting." raise SystemExit
def before_all(context): context.baseurl = 'http://127.0.0.1:8000' twill.set_output(StringIO.StringIO()) twill.commands.clear_cookies() context.app = beer_app.create_app() context.app.config['TESTING'] = True # fn=(lambda : context.app), it help create the WSGI app object only once, # because function passed into wsgi_intercept is called # once for each intercepted connection # more here: http://ivory.idyll.org/articles/twill-and-wsgi_intercept.html twill.add_wsgi_intercept('127.0.0.1', 8000, lambda : context.app) context.browser = twill.get_browser()
def _twill_selector_browser(id_): """Uses host to avoid collisions in the globalness of twill.""" host = "testhost-%s" % id_ s = Selector() twill.add_wsgi_intercept(host, 80, lambda: s) b = twill.get_browser() def go(path): b.go('http://%s%s' % (host, path)) headers = b._browser._response._headers.headers print s return (b.result.http_code, headers, b.result.page) return dict(selector=s, browser=b, go=go)
def test_slashdot(self): browser = twill.get_browser() browser.go('http://slashdot.org/') self.assertTrue(browser.get_code() in (200, 201)) link = browser.find_link('Science') browser.follow_link(link) form = browser.get_form(2) form.set_value('aardvark', name='fhfilter') browser.clicked(form, None) browser.submit() self.assertEqual(browser.get_code(), 200)
def download_with_login(url, login_url, login=None, password=None, ext='', username_field='username', password_field='password', form_id=1): ''' Download a URI from a website using Django by loging-in first 1. Logs in using supplied login & password (if provided) 2. Create a temp file on disk using extension if provided 3. Write content of URI into file ''' # log-in to Django site if login and password: tw.go(login_url) tw.formvalue('%s' % form_id, username_field, login) tw.formvalue('%s' % form_id, password_field, password) tw.submit() # retrieve URI try: tw.go(url) tw.code('200') except TwillAssertionError: code = get_browser().get_code() # ensure we don't keep credentials tw.reset_browser() raise DownloadFailed(u"Unable to download %(url)s. " u"Received HTTP #%(code)s." % { 'url': url, 'code': code }) buff = StringIO.StringIO() twill.set_output(buff) try: tw.show() finally: twill.set_output(None) tw.reset_browser() # write file on disk suffix = '.%s' % ext if ext else '' fileh, filename = tempfile.mkstemp(suffix=suffix) os.write(fileh, buff.getvalue()) os.close(fileh) buff.close() return filename
def create_app(self): try: browser = twill.get_browser() browser.go("http://0.0.0.0:5100/") import api api.app.config['LIVESERVER_PORT'] = 5110 api.app.config['TESTING'] = True api.app.config['MONGODB_SETTINGS'] = {'db': 'corr-integrate','host': '0.0.0.0','port': 27017} except: import api api.app.config['LIVESERVER_PORT'] = 5100 api.app.config['TESTING'] = True api.app.config['MONGODB_SETTINGS'] = {'db': 'corr-integrate','host': '0.0.0.0','port': 27017} return api.app
def make_random_bets(count=None): if count is None: count = 10 games = range(131, 142) types = ['favored', 'underdog', 'under', 'over'] amounts = [10 * i for i in range(1, 5)] users = ['al', 'bob', 'carol', 'dave'] #make_bet(albrowser, 131, 'favored', 20) for bet in range(count): user = random.choice(users) browser = get_browser() login(browser, user, 'p22wd') game = random.choice(games) type = random.choice(types) amount = random.choice(amounts) make_bet(browser, game, type, amount)
def is_disabled(formname, name, value=None): """Raise an exception if the named control is not disabled. If the value argument is passed, the test applies only to options with that value. """ browser = twill.get_browser() form = browser.get_form(formname) if not form: raise TwillAssertionError("no matching forms!") control = browser.get_form_field(form, name) if value: if not (control.disabled or control.get_item_disabled(value)): raise TwillAssertionError("%r value %r not disabled!" % (name, value)) return if not control.disabled: raise TwillAssertionError("input %r not disabled!" % name)