def _find_element(self, tag, href_attr, href_extract, content, id, href_pattern, index, verbose): content_pat = utils.make_pattern(content) id_pat = utils.make_pattern(id) href_pat = utils.make_pattern(href_pattern) def printlog(s): if verbose: print(s) found_links = [] total_links = 0 for element in self.html.find_all(tag): el_html = str(element) el_content = element.decode_contents() attrs = element if verbose: printlog("Element: %r" % el_html) if not attrs.get(href_attr): printlog(" Skipped: no %s attribute" % href_attr) continue el_href = attrs[href_attr] if href_extract: m = href_extract.search(el_href) if not m: printlog(" Skipped: doesn't match extract pattern") continue el_href = m.group(1) attrs["uri"] = el_href if el_href.startswith("#"): printlog(" Skipped: only internal fragment href") continue if el_href.startswith("javascript:"): printlog(" Skipped: cannot follow javascript:") continue total_links += 1 if content_pat and not content_pat(el_content): printlog(" Skipped: doesn't match description") continue if id_pat and not id_pat(attrs.get("id", "")): printlog(" Skipped: doesn't match id") continue if href_pat and not href_pat(el_href): printlog(" Skipped: doesn't match href") continue printlog(" Accepted") found_links.append((el_html, el_content, attrs)) if not found_links: raise IndexError("No matching elements found (from %s possible)" % total_links) if index is None: if len(found_links) > 1: raise IndexError("Multiple links match: %s" % ", ".join([repr(anc) for anc, d, attr in found_links])) found_link = found_links[0] else: try: found_link = found_links[index] except IndexError: raise IndexError( "Only %s (out of %s) links match; index %s out of range" % (len(found_links), total_links, index) ) return found_link
def _find_element(self, tag, href_attr, href_extract, content, id, href_pattern, index, verbose): content_pat = utils.make_pattern(content) id_pat = utils.make_pattern(id) href_pat = utils.make_pattern(href_pattern) def printlog(s): if verbose: print(s) found_links = [] total_links = 0 for element in self.html.find_all(tag): el_html = str(element) el_content = element.decode_contents() attrs = element if verbose: printlog('Element: %r' % el_html) if not attrs.get(href_attr): printlog(' Skipped: no %s attribute' % href_attr) continue el_href = attrs[href_attr] if href_extract: m = href_extract.search(el_href) if not m: printlog(" Skipped: doesn't match extract pattern") continue el_href = m.group(1) attrs['uri'] = el_href if el_href.startswith('#'): printlog(' Skipped: only internal fragment href') continue if el_href.startswith('javascript:'): printlog(' Skipped: cannot follow javascript:') continue total_links += 1 if content_pat and not content_pat(el_content): printlog(" Skipped: doesn't match description") continue if id_pat and not id_pat(attrs.get('id', '')): printlog(" Skipped: doesn't match id") continue if href_pat and not href_pat(el_href): printlog(" Skipped: doesn't match href") continue printlog(" Accepted") found_links.append((el_html, el_content, attrs)) if not found_links: raise IndexError("No matching elements found (from %s possible)" % total_links) if index is None: if len(found_links) > 1: raise IndexError( "Multiple links match: %s" % ', '.join([repr(anc) for anc, d, attr in found_links])) found_link = found_links[0] else: try: found_link = found_links[index] except IndexError: raise IndexError( "Only %s (out of %s) links match; index %s out of range" % (len(found_links), total_links, index)) return found_link
def _find_element(self, tag, href_attr, href_extract, content, id, href_pattern, html_pattern, index, verbose): content_pat = utils.make_pattern(content) id_pat = utils.make_pattern(id) href_pat = utils.make_pattern(href_pattern) html_pat = utils.make_pattern(html_pattern) body = self.testbody _tag_re = re.compile(r'<%s\s+(.*?)>(.*?)</%s>' % (tag, tag), re.I + re.S) _script_re = re.compile(r'<script.*?>.*?</script>', re.I | re.S) bad_spans = [] for match in _script_re.finditer(body): bad_spans.append((match.start(), match.end())) def printlog(s): if verbose: print(s) found_links = [] total_links = 0 for match in _tag_re.finditer(body): found_bad = False for bad_start, bad_end in bad_spans: if (match.start() > bad_start and match.end() < bad_end): found_bad = True break if found_bad: continue el_html = match.group(0) el_attr = match.group(1) el_content = match.group(2) attrs = utils.parse_attrs(el_attr) if verbose: printlog('Element: %r' % el_html) if not attrs.get(href_attr): printlog(' Skipped: no %s attribute' % href_attr) continue el_href = attrs[href_attr] if href_extract: m = href_extract.search(el_href) if not m: printlog(" Skipped: doesn't match extract pattern") continue el_href = m.group(1) attrs['uri'] = el_href if el_href.startswith('#'): printlog(' Skipped: only internal fragment href') continue if el_href.startswith('javascript:'): printlog(' Skipped: cannot follow javascript:') continue total_links += 1 if content_pat and not content_pat(el_content): printlog(" Skipped: doesn't match description") continue if id_pat and not id_pat(attrs.get('id', '')): printlog(" Skipped: doesn't match id") continue if href_pat and not href_pat(el_href): printlog(" Skipped: doesn't match href") continue if html_pat and not html_pat(el_html): printlog(" Skipped: doesn't match html") continue printlog(" Accepted") found_links.append((el_html, el_content, attrs)) if not found_links: raise IndexError( "No matching elements found (from %s possible)" % total_links) if index is None: if len(found_links) > 1: raise IndexError( "Multiple links match: %s" % ', '.join([repr(anc) for anc, d, attr in found_links])) found_link = found_links[0] else: try: found_link = found_links[index] except IndexError: raise IndexError( "Only %s (out of %s) links match; index %s out of range" % (len(found_links), total_links, index)) return found_link
def call_FUT(self, obj): from webtest.utils import make_pattern return make_pattern(obj)