Best Python code snippet using tempest_python
drudge_parser.py
Source:drudge_parser.py
...76 def handle_starttag(self, tag, attrs):77 if tag in ('img', 'iframe'):78 # This is going to be media content, usually images and79 # sometimes embedded youtube clips.80 src = self._find_attr_value(attrs, 'src')81 if src and not any(x in src for x in self.SRC_BLACKLIST):82 self.last['images'].append(src)83 elif tag == 'a':84 href = self._find_attr_value(attrs, 'href')85 if href:86 if not (self.skip or any(x in href for x in self.HREF_BLACKLIST)):87 # We found an article and want to grab it.88 self.want = True89 self.last['location'] = self.location90 self.last['articles'].append({91 'href': href.strip(),92 'title': '',93 })94 elif tag == 'td':95 # they use 3 even columns96 if ('width', '33%') in attrs:97 self.skip = False98 if self.location in [Location.MAIN_HEADLINE, Location.TOP_STORY]:99 self.location = Location.COLUMN1100 elif self.location == Location.COLUMN1:101 self.location = Location.COLUMN2102 elif self.location == Location.COLUMN2:103 self.location = Location.COLUMN3104 self._reset_last()105 elif tag == 'hr':106 # We have crossed into a new section, snap off the related107 # articles/images108 self._reset_last()109 def handle_comment(self, data):110 combined = data.replace(" ", "")111 if "TOP LEFT" in data:112 self.location = Location.TOP_STORY113 self._reset_last()114 elif "MAIN HEADLINE" in data:115 self.location = Location.MAIN_HEADLINE116 self._reset_last()117 elif combined.startswith("LINKS") and combined.endswith("COLUMN"):118 self.skip = True119 def handle_endtag(self, tag):120 self.want = False121 def handle_data(self, data):122 if self.want:123 title = self.last['articles'][-1]['title']124 if len(title) > 0:125 title += ' '126 title += data.strip()127 self.last['articles'][-1]['title'] = title128 def _reset_last(self):129 if self.last['images'] or self.last['articles']:130 self.articles.append(self.last)131 self.last = {'images': [], 'articles': [], 'location': None}132 def _find_attr_value(self, attrs, target_attr):133 """Find an attribute value by the name. None will be134 returned if the value was not found.135 :param attrs: The list of (name, value) tuples (usually from136 handle_starttag137 :param target_attr: The attribute name to find (e.g. 'id' for138 the html ID)139 """140 for name, value in attrs:141 if name == target_attr and value:142 return value143 return None144def scrape_page():145 req = Request('https://drudgereport.com', headers={"User-Agent": "Mozilla/5.0"})146 with urlopen(req) as page:...
test_dashboard_basic_ops.py
Source:test_dashboard_basic_ops.py
...31 for attrpair in attrs:32 if attrpair[0] == 'value':33 return attrpair[1]34 return None35 def _find_attr_value(self, attrs, attr_name):36 for attrpair in attrs:37 if attrpair[0] == attr_name:38 return attrpair[1]39 return None40 def handle_starttag(self, tag, attrs):41 if tag == 'input':42 if self._find_name(attrs, 'csrfmiddlewaretoken'):43 self.csrf_token = self._find_value(attrs)44 if self._find_name(attrs, 'region'):45 self.region = self._find_value(attrs)46 if tag == 'form':47 self.login = self._find_attr_value(attrs, 'action')48class TestDashboardBasicOps(manager.ScenarioTest):49 """The test suite for dashboard basic operations50 This is a basic scenario test:51 * checks that the login page is available52 * logs in as a regular user53 * checks that the user home page loads without error54 """55 @classmethod56 def skip_checks(cls):57 super(TestDashboardBasicOps, cls).skip_checks()58 if not CONF.service_available.horizon:59 raise cls.skipException("Horizon support is required")60 @classmethod61 def setup_credentials(cls):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!