Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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()
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
    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))
Esempio n. 7
0
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, )
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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, )
Esempio n. 11
0
 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))
Esempio n. 12
0
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)
Esempio n. 13
0
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, )
Esempio n. 14
0
    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'])
Esempio n. 15
0
    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()
Esempio n. 16
0
    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
Esempio n. 17
0
 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
Esempio n. 18
0
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
Esempio n. 19
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()
Esempio n. 20
0
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 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)
Esempio n. 22
0
    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))
Esempio n. 23
0
    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))
Esempio n. 24
0
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)
Esempio n. 25
0
    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()
Esempio n. 26
0
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()
Esempio n. 27
0
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)
Esempio n. 28
0
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)
Esempio n. 29
0
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)
Esempio n. 30
0
    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)
Esempio n. 31
0
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()
Esempio n. 32
0
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,)))
Esempio n. 33
0
 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")
Esempio n. 34
0
    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
Esempio n. 35
0
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,)))
Esempio n. 36
0
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")
Esempio n. 37
0
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()
Esempio n. 38
0
 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."
Esempio n. 39
0
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()
Esempio n. 40
0
    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()
Esempio n. 41
0
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)
Esempio n. 42
0
 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."
Esempio n. 43
0
    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
Esempio n. 44
0
    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
Esempio n. 45
0
 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()
Esempio n. 46
0
 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()  
Esempio n. 47
0
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
Esempio n. 48
0
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()
Esempio n. 49
0
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
Esempio n. 50
0
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)
Esempio n. 51
0
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))
Esempio n. 52
0
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
Esempio n. 53
0
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()
Esempio n. 54
0
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)
Esempio n. 55
0
	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)
Esempio n. 56
0
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
Esempio n. 57
0
    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
Esempio n. 58
0
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)
Esempio n. 59
0
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)