def test_web_page_content(url, username="******", password="", expected_text="</html>", unexpected_text="", expected_link_target=None, expected_link_label=None, require_validate_p=None): """Test the content of a web page. 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. """ from invenio.utils.w3c_validator import (w3c_validate, w3c_errors_to_str, CFG_TESTS_REQUIRE_HTML_VALIDATION) if require_validate_p is None: require_validate_p = CFG_TESTS_REQUIRE_HTML_VALIDATION 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) try: browser.open(url) except mechanize.HTTPError as msg: if msg.code != 401: raise msg error_messages.append('ERROR: Page %s (login %s) not accessible. ' '%s' % (url, username, msg)) 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)) from invenio.config import CFG_LOGDIR open('%s/w3c-markup-validator.log' % CFG_LOGDIR, 'a').write(error_text) raise InvenioTestUtilsBrowserException(error_text) except InvenioTestUtilsBrowserException as msg: error_messages.append( 'ERROR: Page %s (login %s) led to an error: %s.' % (url, username, msg)) try: # logout after tests: from invenio.config import CFG_SITE_SECURE_URL browser.open(CFG_SITE_SECURE_URL + "/youraccount/logout") browser.response().read() browser.close() except UnboundLocalError: pass except mechanize.HTTPError: # Raises 401 if you were not logged in before. pass if CFG_TESTUTILS_VERBOSE >= 9: print("%s test_web_page_content(), tested page `%s', login `%s', " "expected text `%s', errors `%s'." % (time.strftime("%Y-%m-%d %H:%M:%S -->", time.localtime()), url, username, expected_text, ",".join(error_messages))) return error_messages
def test_web_page_content(url, username="******", password="", expected_text="</html>", unexpected_text="", expected_link_target=None, expected_link_label=None, require_validate_p=None): """Test the content of a web page. 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. """ from invenio.utils.w3c_validator import (w3c_validate, w3c_errors_to_str, CFG_TESTS_REQUIRE_HTML_VALIDATION) if require_validate_p is None: require_validate_p = CFG_TESTS_REQUIRE_HTML_VALIDATION 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) try: browser.open(url) except mechanize.HTTPError as msg: if msg.code != 401: raise msg error_messages.append('ERROR: Page %s (login %s) not accessible. ' '%s' % (url, username, msg)) 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)) from invenio.config import CFG_LOGDIR open('%s/w3c-markup-validator.log' % CFG_LOGDIR, 'a').write(error_text) raise InvenioTestUtilsBrowserException(error_text) except InvenioTestUtilsBrowserException as msg: error_messages.append( 'ERROR: Page %s (login %s) led to an error: %s.' % (url, username, msg)) try: # logout after tests: from invenio.config import CFG_SITE_SECURE_URL browser.open(CFG_SITE_SECURE_URL + "/youraccount/logout") browser.response().read() browser.close() except UnboundLocalError: pass except mechanize.HTTPError: # Raises 401 if you were not logged in before. pass if CFG_TESTUTILS_VERBOSE >= 9: print("%s test_web_page_content(), tested page `%s', login `%s', " "expected text `%s', errors `%s'." % (time.strftime("%Y-%m-%d %H:%M:%S -->", time.localtime()), url, username, expected_text, ",".join(error_messages))) return error_messages
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))
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))