示例#1
0
def test_web_page_content(url,
                          username="******",
                          password="",
                          expected_text="</html>",
                          unexpected_text="",
                          expected_link_target=None,
                          expected_link_label=None,
                          require_validate_p=CFG_TESTS_REQUIRE_HTML_VALIDATION):
    """Test whether web page URL as seen by user USERNAME contains
       text EXPECTED_TEXT and, eventually, contains a link to
       EXPECTED_LINK_TARGET (if set) labelled EXPECTED_LINK_LABEL (if
       set).  The EXPECTED_TEXT is checked via substring matching, the
       EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL via exact string
       matching.

       EXPECTED_TEXT, EXPECTED_LINK_LABEL and EXPECTED_LINK_TARGET can
       either be strings or list of strings (in order to check multiple
       values inside same page).

       Before doing the tests, login as USERNAME with password
       PASSWORD.  E.g. interesting values for USERNAME are "guest" or
       "admin".

       Return empty list in case of no problems, otherwise list of error
       messages that may have been encountered during processing of
       page.
    """
    try:
        import mechanize
    except ImportError:
        raise InvenioTestUtilsBrowserException('ERROR: Cannot import mechanize.')
    if '--w3c-validate' in sys.argv:
        require_validate_p = True
        sys.stderr.write('Required validation\n')

    error_messages = []
    try:
        browser = get_authenticated_mechanize_browser(username, password)
        browser.open(url)
        url_body = browser.response().read()

        # now test for EXPECTED_TEXT:
        # first normalize expected_text
        if isinstance(expected_text, str):
            expected_texts = [expected_text]
        else:
            expected_texts = expected_text
        # then test
        for cur_expected_text in expected_texts:
            try:
                url_body.index(cur_expected_text)
            except ValueError:
                raise InvenioTestUtilsBrowserException, \
                      'ERROR: Page %s (login %s) does not contain %s, but contains %s' % \
                      (url, username, cur_expected_text, url_body)

        # now test for UNEXPECTED_TEXT:
        # first normalize unexpected_text
        if isinstance(unexpected_text, str):
            if unexpected_text:
                unexpected_texts = [unexpected_text]
            else:
                unexpected_texts = []
        else:
            unexpected_texts = unexpected_text
        # then test
        for cur_unexpected_text in unexpected_texts:
            try:
                url_body.index(cur_unexpected_text)
                raise InvenioTestUtilsBrowserException, \
                      'ERROR: Page %s (login %s) contains %s.' % \
                      (url, username, cur_unexpected_text)
            except ValueError:
                pass

        # now test for EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL:
        if expected_link_target or expected_link_label:
            # first normalize expected_link_target and expected_link_label
            if isinstance(expected_link_target, str) or \
                   expected_link_target is None:
                expected_link_targets = [expected_link_target]
            else:
                expected_link_targets = expected_link_target
            if isinstance(expected_link_label, str) or \
                   expected_link_label is None:
                expected_link_labels = [expected_link_label]
            else:
                expected_link_labels = expected_link_label
            max_links = max(len(expected_link_targets), len(expected_link_labels))
            expected_link_labels = chain(expected_link_labels, repeat(None))
            expected_link_targets = chain(expected_link_targets, repeat(None))
            # then test
            for dummy in range(0, max_links):
                cur_expected_link_target = expected_link_targets.next()
                cur_expected_link_label = expected_link_labels.next()
                try:
                    browser.find_link(url=cur_expected_link_target,
                                      text=cur_expected_link_label)
                except mechanize.LinkNotFoundError:
                    raise InvenioTestUtilsBrowserException, \
                          'ERROR: Page %s (login %s) does not contain link to %s entitled %s.' % \
                          (url, username, cur_expected_link_target, cur_expected_link_label)

        # now test for validation if required
        if require_validate_p:
            valid_p, errors, warnings = w3c_validate(url_body)
            if not valid_p:
                error_text = 'ERROR: Page %s (login %s) does not validate:\n %s' % \
                                  (url, username, w3c_errors_to_str(errors, warnings))
                open('%s/w3c-markup-validator.log' % CFG_LOGDIR, 'a').write(error_text)
                raise InvenioTestUtilsBrowserException, error_text


    except mechanize.HTTPError, msg:
        error_messages.append('ERROR: Page %s (login %s) not accessible. %s' % \
                              (url, username, msg))
示例#2
0
def test_web_page_content(url,
                          username="******",
                          password="",
                          expected_text="</html>",
                          unexpected_text="",
                          expected_link_target=None,
                          expected_link_label=None,
                          require_validate_p=CFG_TESTS_REQUIRE_HTML_VALIDATION):
    """Test whether web page URL as seen by user USERNAME contains
       text EXPECTED_TEXT and, eventually, contains a link to
       EXPECTED_LINK_TARGET (if set) labelled EXPECTED_LINK_LABEL (if
       set).  The EXPECTED_TEXT is checked via substring matching, the
       EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL via exact string
       matching.

       EXPECTED_TEXT, EXPECTED_LINK_LABEL and EXPECTED_LINK_TARGET can
       either be strings or list of strings (in order to check multiple
       values inside same page).

       Before doing the tests, login as USERNAME with password
       PASSWORD.  E.g. interesting values for USERNAME are "guest" or
       "admin".

       Return empty list in case of no problems, otherwise list of error
       messages that may have been encountered during processing of
       page.
    """
    try:
        import mechanize
    except ImportError:
        raise InvenioTestUtilsBrowserException('ERROR: Cannot import mechanize.')
    if '--w3c-validate' in sys.argv:
        require_validate_p = True
        sys.stderr.write('Required validation\n')

    error_messages = []
    try:
        browser = get_authenticated_mechanize_browser(username, password)
        browser.open(url)
        url_body = browser.response().read()

        # now test for EXPECTED_TEXT:
        # first normalize expected_text
        if isinstance(expected_text, str):
            expected_texts = [expected_text]
        else:
            expected_texts = expected_text
        # then test
        for cur_expected_text in expected_texts:
            try:
                url_body.index(cur_expected_text)
            except ValueError:
                raise InvenioTestUtilsBrowserException, \
                      'ERROR: Page %s (login %s) does not contain %s, but contains %s' % \
                      (url, username, cur_expected_text, url_body)

        # now test for UNEXPECTED_TEXT:
        # first normalize unexpected_text
        if isinstance(unexpected_text, str):
            if unexpected_text:
                unexpected_texts = [unexpected_text]
            else:
                unexpected_texts = []
        else:
            unexpected_texts = unexpected_text
        # then test
        for cur_unexpected_text in unexpected_texts:
            try:
                url_body.index(cur_unexpected_text)
                raise InvenioTestUtilsBrowserException, \
                      'ERROR: Page %s (login %s) contains %s.' % \
                      (url, username, cur_unexpected_text)
            except ValueError:
                pass

        # now test for EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL:
        if expected_link_target or expected_link_label:
            # first normalize expected_link_target and expected_link_label
            if isinstance(expected_link_target, str) or \
                   expected_link_target is None:
                expected_link_targets = [expected_link_target]
            else:
                expected_link_targets = expected_link_target
            if isinstance(expected_link_label, str) or \
                   expected_link_label is None:
                expected_link_labels = [expected_link_label]
            else:
                expected_link_labels = expected_link_label
            max_links = max(len(expected_link_targets), len(expected_link_labels))
            expected_link_labels = chain(expected_link_labels, repeat(None))
            expected_link_targets = chain(expected_link_targets, repeat(None))
            # then test
            for dummy in range(0, max_links):
                cur_expected_link_target = expected_link_targets.next()
                cur_expected_link_label = expected_link_labels.next()
                try:
                    browser.find_link(url=cur_expected_link_target,
                                      text=cur_expected_link_label)
                except mechanize.LinkNotFoundError:
                    raise InvenioTestUtilsBrowserException, \
                          'ERROR: Page %s (login %s) does not contain link to %s entitled %s.' % \
                          (url, username, cur_expected_link_target, cur_expected_link_label)

        # now test for validation if required
        if require_validate_p:
            valid_p, errors, warnings = w3c_validate(url_body)
            if not valid_p:
                error_text = 'ERROR: Page %s (login %s) does not validate:\n %s' % \
                                  (url, username, w3c_errors_to_str(errors, warnings))
                open('%s/w3c-markup-validator.log' % CFG_LOGDIR, 'a').write(error_text)
                raise InvenioTestUtilsBrowserException, error_text


    except mechanize.HTTPError, msg:
        error_messages.append('ERROR: Page %s (login %s) not accessible. %s' % \
                              (url, username, msg))
示例#3
0
文件: web.py 项目: dinosk/inspire
        for cur_expected_link_target, cur_expected_link_label \
                       in zip(expected_link_targets, expected_link_labels):
            try:
                browser.find_link(url=cur_expected_link_target,
                                  text=cur_expected_link_label)
            except mechanize.LinkNotFoundError:
                raise InvenioTestUtilsBrowserException(
                      'ERROR: Page %s (login %s) does not contain link'
                      ' to %s entitled %s.' %
                      (url, username, cur_expected_link_target,
                       cur_expected_link_label)
                )

    # Test for validation if required
    if require_validate_p:
        valid_p, errors, warnings = w3c_validate(url_body)
        if not valid_p:
            error_text = 'ERROR: Page %s (login %s) does not' \
                         ' validate:\n %s' % \
                         (url, username,
                          w3c_errors_to_str(errors, warnings))
            raise InvenioTestUtilsBrowserException(error_text)

    if CFG_TESTUTILS_VERBOSE >= 9:
        print "%s check_web_page_content(), tested page `%s', login `%s'," \
              " expected text `%s', unexpected text `%s'." % \
              (time.strftime("%Y-%m-%d %H:%M:%S -->", time.localtime()),
               url, username, expected_text, unexpected_text)

    return browser
示例#4
0
文件: web.py 项目: nkalodimas/inspire
        for cur_expected_link_target, cur_expected_link_label \
                       in zip(expected_link_targets, expected_link_labels):
            try:
                browser.find_link(url=cur_expected_link_target,
                                  text=cur_expected_link_label)
            except mechanize.LinkNotFoundError:
                raise InvenioTestUtilsBrowserException(
                      'ERROR: Page %s (login %s) does not contain link'
                      ' to %s entitled %s.' %
                      (url, username, cur_expected_link_target,
                       cur_expected_link_label)
                )

    # Test for validation if required
    if require_validate_p:
        valid_p, errors, warnings = w3c_validate(url_body)
        if not valid_p:
            error_text = 'ERROR: Page %s (login %s) does not' \
                         ' validate:\n %s' % \
                         (url, username,
                          w3c_errors_to_str(errors, warnings))
            raise InvenioTestUtilsBrowserException(error_text)

    if CFG_TESTUTILS_VERBOSE >= 9:
        print "%s check_web_page_content(), tested page `%s', login `%s'," \
              " expected text `%s', unexpected text `%s'." % \
              (time.strftime("%Y-%m-%d %H:%M:%S -->", time.localtime()),
               url, username, expected_text, unexpected_text)

    return browser
示例#5
0
def test_web_page_content(
    url,
    username="******",
    password="",
    expected_text="</html>",
    unexpected_text="",
    expected_link_target=None,
    expected_link_label=None,
    require_validate_p=CFG_TESTS_REQUIRE_HTML_VALIDATION,
):
    """Test whether web page URL as seen by user USERNAME contains
       text EXPECTED_TEXT and, eventually, contains a link to
       EXPECTED_LINK_TARGET (if set) labelled EXPECTED_LINK_LABEL (if
       set).  The EXPECTED_TEXT is checked via substring matching, the
       EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL via exact string
       matching.

       EXPECTED_TEXT, EXPECTED_LINK_LABEL and EXPECTED_LINK_TARGET can
       either be strings or list of strings (in order to check multiple
       values inside same page).

       Before doing the tests, login as USERNAME with password
       PASSWORD.  E.g. interesting values for USERNAME are "guest" or
       "admin".

       Return empty list in case of no problems, otherwise list of error
       messages that may have been encountered during processing of
       page.
    """

    if "--w3c-validate" in sys.argv:
        require_validate_p = True
        sys.stderr.write("Required validation\n")

    error_messages = []
    try:
        import mechanize
    except ImportError:
        return ["ERROR: Cannot import mechanize."]
    browser = mechanize.Browser()
    browser.set_handle_robots(False)  # ignore robots.txt, since we test gently
    try:
        # firstly login:
        if username == "guest":
            pass
        else:
            browser.open(CFG_SITE_SECURE_URL + "/youraccount/login")
            browser.select_form(nr=0)
            browser["p_un"] = username
            browser["p_pw"] = password
            browser.submit()
            username_account_page_body = browser.response().read()
            try:
                username_account_page_body.index("You are logged in as %s." % username)
            except ValueError:
                raise InvenioTestUtilsBrowserException, "ERROR: Cannot login as %s." % username

        # secondly read page body:
        browser.open(url)
        url_body = browser.response().read()

        # now test for EXPECTED_TEXT:
        # first normalize expected_text
        if isinstance(expected_text, str):
            expected_texts = [expected_text]
        else:
            expected_texts = expected_text
        # then test
        for cur_expected_text in expected_texts:
            try:
                url_body.index(cur_expected_text)
            except ValueError:
                raise InvenioTestUtilsBrowserException, "ERROR: Page %s (login %s) does not contain %s, ut contains %s" % (
                    url,
                    username,
                    cur_expected_text,
                    url_body,
                )

        # now test for UNEXPECTED_TEXT:
        # first normalize unexpected_text
        if isinstance(unexpected_text, str):
            if unexpected_text:
                unexpected_texts = [unexpected_text]
            else:
                unexpected_texts = []
        else:
            unexpected_texts = unexpected_text
        # then test
        for cur_unexpected_text in unexpected_texts:
            try:
                url_body.index(cur_unexpected_text)
                raise InvenioTestUtilsBrowserException, "ERROR: Page %s (login %s) contains %s." % (
                    url,
                    username,
                    cur_unexpected_text,
                )
            except ValueError:
                pass

        # now test for EXPECTED_LINK_TARGET and EXPECTED_LINK_LABEL:
        if expected_link_target or expected_link_label:
            # first normalize expected_link_target and expected_link_label
            if isinstance(expected_link_target, str) or expected_link_target is None:
                expected_link_targets = [expected_link_target]
            else:
                expected_link_targets = expected_link_target
            if isinstance(expected_link_label, str) or expected_link_label is None:
                expected_link_labels = [expected_link_label]
            else:
                expected_link_labels = expected_link_label
            max_links = max(len(expected_link_targets), len(expected_link_labels))
            expected_link_labels = chain(expected_link_labels, repeat(None))
            expected_link_targets = chain(expected_link_targets, repeat(None))
            # then test
            for dummy in range(0, max_links):
                cur_expected_link_target = expected_link_targets.next()
                cur_expected_link_label = expected_link_labels.next()
                try:
                    browser.find_link(url=cur_expected_link_target, text=cur_expected_link_label)
                except mechanize.LinkNotFoundError:
                    raise InvenioTestUtilsBrowserException, "ERROR: Page %s (login %s) does not contain link to %s entitled %s." % (
                        url,
                        username,
                        cur_expected_link_target,
                        cur_expected_link_label,
                    )

        # now test for validation if required
        if require_validate_p:
            valid_p, errors, warnings = w3c_validate(url_body)
            if not valid_p:
                error_text = "ERROR: Page %s (login %s) does not validate:\n %s" % (
                    url,
                    username,
                    w3c_errors_to_str(errors, warnings),
                )
                open("%s/w3c-markup-validator.log" % CFG_LOGDIR, "a").write(error_text)
                raise InvenioTestUtilsBrowserException, error_text

    except mechanize.HTTPError, msg:
        error_messages.append("ERROR: Page %s (login %s) not accessible. %s" % (url, username, msg))