How to use load_page method in locust

Best Python code snippet using locust

test_models.py

Source:test_models.py Github

copy

Full Screen

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 ]...

Full Screen

Full Screen

Sider_photo.py

Source:Sider_photo.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

loading_page.py

Source:loading_page.py Github

copy

Full Screen

...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)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful