Best Python code snippet using locust
test_models.py
Source:test_models.py
1import unittest2from unittest.mock import patch, Mock3from scraper.models import WebPage, WebGraph4from scraper.util import find_urls, find_emails5def patch_requests_get(pass_mock=False):6 def _wrapper(func):7 def _test_method(self, res_mock):8 response = Mock()9 response.content = b"<html></html>"10 response.headers = {"Content-Type": "text/html"}11 res_mock.return_value = response12 if pass_mock:13 return func(self, res_mock)14 else:15 return func(self)16 return _test_method17 return _wrapper18@patch("requests.get")19class TestWebPage(unittest.TestCase):20 21 @patch_requests_get()22 def test_for_creating_webpage_from_repr(self):23 p1 = WebPage("http://www.test.page.com", load_page=False)24 p2 = eval(repr(p1))25 self.assertEqual(p2.url, p1.url)26 self.assertFalse(p2.loaded)27 @patch_requests_get()28 def test_pages_with_the_same_url_have_similar_hash(self):29 p1 = WebPage("http://www.test.page.com")30 p2 = WebPage("http://www.test.page.com")31 self.assertEqual(hash(p1), hash(p2))32 @patch_requests_get()33 def test_pages_with_the_same_url_are_equal(self):34 p1 = WebPage("http://www.test.page.com")35 p2 = WebPage("http://www.test.page.com")36 self.assertEqual(p1, p2)37 @patch_requests_get()38 def test_raises_error_when_changing_url(self):39 p1 = WebPage("http://www.you.can.change.me")40 with self.assertRaises(AttributeError):41 p1.url = "http://www.I.can.do.everything"42 @patch_requests_get(True)43 def test_constructor_loads_page_by_default(self, get_mock):44 p1 = WebPage("http://localhost:5000/")45 get_mock.assert_called_once_with("http://localhost:5000/", params=None)46 @patch_requests_get(True)47 def test_for_turning_off_loading_page_in_constructor(self, get_mock):48 p1 = WebPage("http://localhost:5000", load_page=False)49 self.assertFalse(get_mock.called)50 @patch_requests_get()51 def test_reload_sets_content_and_headers_attribute(self):52 page = WebPage("http://localhost:5000")53 self.assertEqual(page.content, b"<html></html>")54 self.assertEqual(page.headers, {"Content-Type": "text/html"})55 @patch_requests_get()56 @patch("requests.head")57 def test_reload_only_headers(self, head_mock):58 response = Mock()59 response.content = b""60 response.headers = {"Content-Type": "text/html"}61 head_mock.return_value = response62 page = WebPage("http://localhost:5000", load_page=False)63 page.reload(head_request=True)64 self.assertEqual(page.content, b"")65 self.assertEqual(page.headers, {"Content-Type": "text/html"})66@patch("scraper.models.requests.get")67class FindEmailsAndUrlsTest(unittest.TestCase):68 @patch_requests_get(True)69 def test_for_extracting_urls(self, get_mock):70 response = Mock()71 response.content = b"""72 <html>73 <body>74 <a href='test.html'>Test</a>75 <a href="mailto:test@gil.com">E-Mail</a>76 Contact: test@one.two77 </body>78 </html>79 """80 response.encoding = "utf-8"81 response.headers = {"Content-Type": "text/html"}82 get_mock.return_value = response83 page = WebPage("http://localhost:5000", load_page=False)84 page.reload()85 links = find_urls(page)86 self.assertTrue(len(links), 1)87 self.assertEqual(links[0], "test.html")88 @patch_requests_get(True)89 def test_for_extracting_emails(self, get_mock):90 response = Mock()91 response.content = b"""92 <html>93 <body>94 <a href='test.html'>Test</a>95 <a href="mailto:test@gil.com">E-Mail</a>96 Contact: test@one.two97 </body>98 </html>99 """100 response.encoding = "utf-8"101 response.headers = {"Content-Type": "text/html"}102 get_mock.return_value = response103 page = WebPage("http://localhost:5000", load_page=False)104 page.reload()105 emails = find_emails(page)106 self.assertTrue(len(emails), 2)107 self.assertCountEqual(emails, ["test@gil.com", "test@one.two"])108class WebGraphTest(unittest.TestCase):109 110 def test_for_adding_relation_between_pages(self):111 p1 = WebPage("test1", load_page=False)112 p2 = WebPage("test2", load_page=False)113 wg = WebGraph()114 wg.add_relation(p1, p2, directed=False)115 self.assertIn(p2, wg.graph[p1])116 self.assertIn(p1, wg.graph[p2])117 def test_add_relation_does_not_recreate_pages(self):118 p1 = WebPage("test1", load_page=False)119 p2 = WebPage("test2", load_page=False)120 wg = WebGraph()121 wg.add_page(p1)122 wg.add_page(p2)123 wg.add_relation(p1, p2, directed=False)124 self.assertEqual(len(wg), 2)125 def test_get_page_creates_new_page_for_new_url(self):126 wg = WebGraph()127 page = wg.get_page("http://localhost:5000/")128 self.assertEqual(page.url, "http://localhost:5000/")129 def test_get_page_returns_None_create_new_is_turn_off(self):130 wg = WebGraph()131 page = wg.get_page("http://localhost:5000/", create_new=False)132 self.assertIsNone(page)133 def test_get_page_returns_correct_page(self):134 wg = WebGraph()135 wg.add_page(WebPage("http://localhost:5000/test", load_page=False))136 wg.add_page(WebPage("http://localhost:5000/home", load_page=False))137 page = wg.get_page("http://localhost:5000/test", create_new=False)138 self.assertIsNotNone(page)139 def test_get_page_with_getitem_operator(self):140 wg = WebGraph()141 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))142 p2 = wg.add_page(WebPage("http://localhost:5000/home", load_page=False)) 143 self.assertEqual(wg.get_page("http://localhost:5000/test"), p1)144 self.assertEqual(wg.get_page(p2), p2)145 def test_add_page_adds_new_page(self):146 wg = WebGraph()147 page = wg.add_page(WebPage("http://localhost:5000/test", 148 load_page=False))149 self.assertEqual(page, wg.get_page("http://localhost:5000/test"))150 def test_add_page_called_with_parent_adds_relation_between_pages(self):151 wg = WebGraph()152 root = WebPage(url="http://localhost:5000/", load_page=False)153 page = wg.add_page("http://localhost:5000/test", parent=root)154 self.assertIn(page, wg.graph[root])155 def test_for_presence_of_page_in_graph(self):156 wg = WebGraph()157 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))158 p2 = WebPage("http://localhost:5000/home", load_page=False)159 self.assertTrue("http://localhost:5000/test" in wg)160 self.assertTrue(p1 in wg)161 self.assertFalse(p2 in wg)162 def test_find_path_returns_None_when_page_not_in_graph(self):163 wg = WebGraph()164 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))165 p2 = WebPage("http://localhost:5000/home", load_page=False)166 self.assertIsNone(wg.find_path(p1, p2))167 def test_path_between_directly_related_pages_contains_only_them(self):168 wg = WebGraph()169 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))170 p2 = wg.add_page(WebPage("http://localhost:5000/home", load_page=False))171 wg.add_relation(p1, p2, directed=False)172 path = wg.find_path(p1, p2)173 self.assertCountEqual(path, (p1, p2))174 def test_find_path_finds_indirect_paths(self):175 wg = WebGraph()176 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))177 p2 = wg.add_page(WebPage("http://localhost:5000/home", load_page=False))178 p3 = wg.add_page(WebPage("http://localhost:5000/new", load_page=False))179 wg.add_relation(p1, p2, directed=False)180 wg.add_relation(p2, p3, directed=False)181 self.assertEqual(len(wg), 3)182 path = wg.find_path(p1, p3)183 self.assertCountEqual(path, (p1, p2, p3))184 def test_find_path_returns_None_when_no_path(self):185 wg = WebGraph()186 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))187 p2 = wg.add_page(WebPage("http://localhost:5000/home", load_page=False))188 p3 = wg.add_page(WebPage("http://localhost:5000/next", load_page=False))189 p4 = wg.add_page(WebPage("http://localhost:5000/prev", load_page=False))190 wg.add_relation(p1, p2, directed=False)191 wg.add_relation(p3, p4, directed=False)192 path = wg.find_path(p1, p4)193 self.assertIsNone(path)194 def test_find_paths_finds_the_shortest_path(self):195 wg = WebGraph()196 p1 = wg.add_page(WebPage("http://localhost:5000/test", load_page=False))197 p2 = wg.add_page(WebPage("http://localhost:5000/home", load_page=False))198 p3 = wg.add_page(WebPage("http://localhost:5000/next", load_page=False))199 p4 = wg.add_page(WebPage("http://localhost:5000/prev", load_page=False))200 p5 = wg.add_page(WebPage("http://localhost:5000/back", load_page=False))201 wg.add_relation(p1, p2, directed=False)202 wg.add_relation(p2, p3, directed=False)203 wg.add_relation(p3, p4, directed=False)204 wg.add_relation(p4, p5, directed=False)205 wg.add_relation(p1, p4, directed=False)206 path = wg.find_path(p1, p5)207 self.assertCountEqual(path, (p1, p4, p5))208 def test_find_nearest_neighbours_returns_closest_pages(self):209 wg = WebGraph()210 p = [wg.add_page(WebPage("fake %d" % i, load_page=False)) for i in range(7)]211 '''212 / 1 - 2 - 3213 0 |214 \ 4 - 5 - 6215 '''216 wg.add_relation(p[0], p[1], directed=False)217 wg.add_relation(p[1], p[2], directed=False)218 wg.add_relation(p[2], p[3], directed=False)219 wg.add_relation(p[0], p[4], directed=False)220 wg.add_relation(p[4], p[5], directed=False)221 wg.add_relation(p[5], p[6], directed=False)222 wg.add_relation(p[3], p[6], directed=False)223 pages = wg.find_nearest_neighbours(p[0], max_dist=2)224 pages = [ page for page, dist in pages ]...
Sider_photo.py
Source:Sider_photo.py
1# # import urllib2# # import urllib.request3# # import re4# # def load_page(url):5# # request=urllib.request.Request(url)6# # response=urllib.request.urlopen(request)7# # data=response.read()8# # return data9# # def get_image(html):10# # regx=r'http://[\S]*jpg'11# # pattern=re.compile(regx)12# # get_image=re.findall(pattern,repr(html))13# #14# # num=115# # for img in get_image:16# # image=load_page(img)17# # with open('D:\\Photo\\%s.jpg' %num,'wb')as fb:18# # fb.write(image)19# # print("æ£å¨ä¸è½½å¾ç第%så¼ å¾ç"%num)20# # num=num+121# # print("ä¸è½½å®æ!!")22# # url='http://p.weather.com.cn/2017/06/2720826.shtml#p=7'23# # html=load_page(url)24# # get_image(html)25#26#27# import urllib28# import urllib.request29# import re30#31# #å è½½ç½é¡µ32# def load_page(url):33# request=urllib.request.Request(url)34# response=urllib.request.urlopen(request)35# data=response.read()36# return data37#38# #åå¨å¾ç39# def get_image(html):40# repx=r'http://[\S]*jpg'41# pattern=re.compile(repx)42# get_image=re.findall(pattern,repr(html))43# #设置å¾çå称44# num=145#46# for img in get_image:47# image=load_page(img)48# with open('D:\\Photo\\%s.jpg'%num,'wb') as fb:49# fb.write(image)50# print("æ£å¨ä¸è½½ç¬¬%så¼ å¾ç"%num)51# num=num+152# print("ä¸è½½å®æ!!!")53#54# #è°ç¨55# url='http://p.weather.com.cn/2017/06/2720826.shtml#p=7'56# html=load_page(url)57# get_image(html)58# import urllib59# import urllib.request60# import re61#62# #å 载页é¢63# def load_page(url):64# request=urllib.request.Request(url)65# response=urllib.request.urlopen(request)66# data=response.read()67# return data68# #xä¸è½½å¾ç69# def get_image(html):70# regx=r'http://[\S]*jpg'71# a=re.compile(regx)72# get_image=re.findall(a,repr(html))73#74# num=175# for img in get_image:76# b=load_page(img)77# with open('D:\\Photo\\%s.jpg'%num,'wb')as fb:78# fb.write(b)79# print("æ£å¨ä¸è½½ç¬¬%så¼ å¾ç"%num)80# num=num+181# print("ä¸è½½å®æï¼ï¼ï¼")82#83# #è°ç¨84# url='http://p.weather.com.cn/2017/06/2720826.shtml#p=7'85# html=load_page(url)86# get_image(html)87import urllib88import urllib.request89import re90#å è½½å¾ç91def load_page(url):92 request=urllib.request.Request(url)93 response=urllib.request.urlopen(request)94 data=response.read()95 return data96#ä¸è½½å¾ç97def get_image(html):98 #æ£åå¹é
ç½é¡µ99 regx=r'http://[\S]*jpg'100 pattern=re.compile(regx)101 get_image=re.findall(pattern,repr(html))102 num=1103 for img in get_image:104 image = load_page(img)105 with open('D:\\Photo\\%s.jpg'%num,'wb')as fb:106 fb.write(image)107 print('æ£å¨ä¸è½½ç¬¬%så¼ å¾ç'%num)108 num=num+1109 print("ä¸è½½å®æ")110#è°ç¨111url='http://p.weather.com.cn/2017/06/2720826.shtml#p=7'112html=load_page(url)...
loading_page.py
Source:loading_page.py
...5#6# WARNING: Any manual changes made to this file will be lost when pyuic5 is7# run again. Do not edit this file unless you know what you are doing.8from PyQt5 import QtCore, QtGui, QtWidgets9class Ui_load_page(object):10 def setupUi(self, load_page):11 load_page.setObjectName("load_page")12 load_page.resize(601, 467)13 self.gridLayout_2 = QtWidgets.QGridLayout(load_page)14 self.gridLayout_2.setObjectName("gridLayout_2")15 self.loading_text = QtWidgets.QLabel(load_page)16 font = QtGui.QFont()17 font.setFamily("Arial")18 font.setPointSize(14)19 self.loading_text.setFont(font)20 self.loading_text.setObjectName("loading_text")21 self.gridLayout_2.addWidget(self.loading_text, 0, 0, 1, 1, QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)22 self.retranslateUi(load_page)23 QtCore.QMetaObject.connectSlotsByName(load_page)...
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!!