-
Notifications
You must be signed in to change notification settings - Fork 33
/
tests.py
executable file
·369 lines (272 loc) · 11.3 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
import time
import io
from os.path import join, dirname, abspath
import phantompy as ph
CURRENT_DIR = dirname(abspath(__file__))
TEST_FILE = join(CURRENT_DIR, "misc", "test.html")
TEST_WITH_LINK = join(CURRENT_DIR, "misc", "test_with_link.html")
TEST_FILE_SIMPLE = join(CURRENT_DIR, "misc", "test_simple.html")
JQUERY_FILE = join(CURRENT_DIR, "misc", "jquery-1.9.1.min.js")
TEST_FILE_WITH_FRAMES = join(CURRENT_DIR, "misc", "test_with_frames.html")
def setUpModule():
ctx = ph.context.context()
ctx.conf.set_max_pages_in_cache(0)
ctx.conf.set_object_cache_capacity(0, 0, 0)
ctx.clear_memory_caches()
def tearDownModule():
ph.context.destroy_context()
class TestCase(unittest.TestCase):
pass
class WebPageTests(TestCase):
def test_load_page(self):
frame = ph.open(TEST_FILE)
html_data = frame.to_html()
self.assertNotEqual(len(html_data), 0)
def test_evaluate_js_01(self):
"""
Evaluate js method can return strings.
"""
frame = ph.open(TEST_FILE)
result = frame.evaluate('document.getElementsByTagName("h1")[0].innerHTML')
self.assertEqual(result, "Title")
def test_evaluate_js_02(self):
"""
Evaluate js method can return dicts and
arrays.
"""
frame = ph.open(TEST_FILE)
result = frame.evaluate('$("#footer").data()')
self.assertEqual(result, {'id': 1.0})
#def test_page_cookies(self):
# frame = ph.open("http://www.facebook.com")
# ctx = ph.context.context()
# cookies = ctx.get_all_cookies()
# self.assertNotEqual(len(cookies), 0)
# self.assertIn("value", cookies[0])
# self.assertIn("name", cookies[0])
# self.assertIn("domain", cookies[0])
# self.assertIn("expires", cookies[0])
# cookies = frame.page.get_cookies()
# self.assertIn("value", cookies[0])
# self.assertIn("name", cookies[0])
# self.assertIn("domain", cookies[0])
# self.assertIn("expires", cookies[0])
# ctx.clear_cookies()
def test_cookies(self):
ctx = ph.context.context()
ctx.set_cookie("kk", "ll", ".niwi.be")
cookies = ctx.get_all_cookies()
self.assertEqual(len(cookies), 1)
self.assertEqual(cookies[0]['name'], 'kk')
ctx.clear_cookies()
def test_page_all_requests(self):
frame = ph.open(TEST_FILE)
self.assertIn('http://code.jquery.com/jquery-1.9.1.min.js', frame.page.get_requested_urls())
self.assertEqual(len(frame.page.get_requested_urls()), 2)
def test_retrieve_page_background_responses(self):
frame = ph.open(TEST_FILE)
response = frame.page.get_response_by_url('http://code.jquery.com/jquery-1.9.1.min.js')
self.assertNotEqual(response["size"], 0)
self.assertEqual(response["status"], 200)
self.assertNotEqual(len(response["headers"]), 0)
def test_conf_access(self):
ctx = ph.context.context()
ctx.conf.load_images = False
ctx.conf.javascript = False
ctx.conf.offline_storage_quota = 2000
self.assertFalse(ctx.conf.load_images)
self.assertFalse(ctx.conf.javascript)
self.assertEqual(ctx.conf.offline_storage_quota, 2000)
ctx.conf.load_images = True
ctx.conf.javascript = True
ctx.conf.offline_storage_quota = 0
self.assertTrue(ctx.conf.load_images)
self.assertTrue(ctx.conf.javascript)
self.assertEqual(ctx.conf.offline_storage_quota, 0)
def test_inject_jquery(self):
frame = ph.open(TEST_FILE_SIMPLE)
with io.open(JQUERY_FILE, "rb") as f:
frame.evaluate(f.read())
result = frame.evaluate("$('title').html()")
self.assertEqual(result, "simple title")
def test_page_navigation_01(self):
frame = ph.open(TEST_WITH_LINK)
frame.evaluate('document.querySelector("a").click()', expect_load=True)
self.assertTrue(frame.url.endswith("test_with_link_dst.html"))
def test_context_process_events(self):
ctx = ph.context.context()
ctx.process_events(300)
class WebElementTests(TestCase):
def test_get_title_element(self):
p = ph.open(TEST_FILE)
elements = p.cssselect("title")
self.assertEqual(len(elements), 1)
self.assertEqual(elements[0].name, "TITLE")
self.assertEqual(elements[0].inner_html(), "simple title")
def test_get_null_element(self):
p = ph.open(TEST_FILE)
elements = p.cssselect("foo")
self.assertEqual(len(elements), 0)
def test_cssseelect_first(self):
p = ph.open(TEST_FILE)
element = p.cssselect_first("#footer")
self.assertTrue(element.has_attr("id"))
with self.assertRaises(ValueError):
element = p.cssselect_first("#footer2")
def test_get_inner_html(self):
p = ph.open(TEST_FILE)
elements = p.cssselect("#footer")
self.assertEqual(len(elements), 1)
html = elements[0].inner_html()
self.assertEqual(html.strip(), "<span>some footer</span>")
def test_get_inner_text(self):
p = ph.open(TEST_FILE)
elements = p.cssselect("#footer")
self.assertEqual(len(elements), 1)
text = elements[0].inner_text()
self.assertEqual(text.strip(), "some footer")
def test_inner_cssselect(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
elements = element.cssselect("span")
self.assertEqual(len(elements), 1)
def test_has_attrs(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer span")[0]
self.assertFalse(element.has_attrs())
element = p.cssselect("#footer")[0]
self.assertTrue(element.has_attrs())
self.assertTrue(element.has_attr("id"))
self.assertFalse(element.has_attr("class"))
def test_set_attr(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
element.set_attr('foo', 'bar')
self.assertTrue(element.has_attr("foo"))
self.assertIn("foo", element.get_attrs())
def test_set_attrs(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
element.set_attrs({"foo": "bar"})
self.assertTrue(element.has_attr("foo"))
self.assertIn("foo", element.get_attrs())
def test_has_class(self):
p = ph.open(TEST_FILE)
elements = p.cssselect(".section")
self.assertEqual(len(elements), 2)
self.assertTrue(elements[0].has_class("barsection"))
self.assertTrue(elements[1].has_class("foosection"))
self.assertFalse(elements[1].has_class("barsection"))
def test_remove_class(self):
p = ph.open(TEST_FILE)
elements = p.cssselect(".barsection")
self.assertEqual(len(elements), 1)
elements[0].remove_class("barsection")
elements = p.cssselect(".barsection")
self.assertEqual(len(elements), 0)
def test_append_01(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
self.assertEqual(len(element.cssselect("span")), 1)
element.append("<span>Fooo</span>")
self.assertEqual(len(element.cssselect("span")), 2)
def test_append_02(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
self.assertEqual(len(element.cssselect("div")), 0)
tmpelement = p.cssselect(".barsection")[0]
element.append(tmpelement)
self.assertEqual(len(element.cssselect("div")), 1)
self.assertEqual(len(p.cssselect("div")), 4)
def test_append_after(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
self.assertEqual(len(element.cssselect("span")), 1)
self.assertEqual(len(p.cssselect("span")), 1)
element.append_after("<span>Fooo</span>")
self.assertEqual(len(element.cssselect("span")), 1)
self.assertEqual(len(p.cssselect("span")), 2)
def test_replace_01(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
element.replace("<section>kk</section>")
self.assertEqual(len(p.cssselect("#footer")), 0)
def test_replace_02(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
element.replace(p.cssselect(".barsection")[0])
self.assertEqual(len(p.cssselect("#footer")), 0)
def test_remove_all_childs(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
self.assertEqual(len(element.cssselect("*")), 1)
element.remove_childs()
self.assertEqual(len(element.cssselect("*")), 0)
def test_remove_from_document(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#footer")[0]
element.remove()
self.assertEqual(len(p.cssselect("#footer")), 0)
self.assertTrue(element.is_none())
def test_wrap(self):
p = ph.open(TEST_FILE)
self.assertEqual(len(p.cssselect("section > #footer")), 0)
element = p.cssselect("#footer")[0]
element.wrap("<section />")
self.assertEqual(len(p.cssselect("section > #footer")), 1)
def test_next(self):
p = ph.open(TEST_FILE)
element = p.cssselect(".barsection")[0]
next_element = element.next()
self.assertTrue(next_element.has_class("foosection"))
element = p.cssselect("#div-with-attrs")[0].next()
self.assertTrue(element.is_none())
def test_prev(self):
p = ph.open(TEST_FILE)
element = p.cssselect(".foosection")[0]
prev_element = element.prev()
self.assertTrue(prev_element.has_class("barsection"))
element = p.cssselect("h1")[0].prev()
self.assertTrue(element.is_none())
def test_get_attrs(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#div-with-attrs")[0]
attrs = element.get_attrs()
self.assertIn("data-id", attrs)
self.assertEqual(attrs, {'data-id': '1', 'data-url': '/foo', 'id': 'div-with-attrs'})
def test_get_attr(self):
p = ph.open(TEST_FILE)
element = p.cssselect_first("#div-with-attrs")
self.assertEqual(element.get_attr("data-id"), "1")
self.assertEqual(element.get_attr("data-foo", default=None), None)
with self.assertRaises(AttributeError):
attr = element.get_attr("data-foo")
with self.assertRaises(ValueError):
attr = element.get_attr("data-id", foo="bar")
def test_get_classes(self):
p = ph.open(TEST_FILE)
elements = p.cssselect(".section")
classes = elements[0].get_classes()
self.assertEqual(len(classes), 2)
self.assertEqual(classes, ['section', 'barsection'])
def test_remove_attr(self):
p = ph.open(TEST_FILE)
element = p.cssselect("#div-with-attrs")[0]
attrs = element.get_attrs()
self.assertIn("data-id", attrs)
element.remove_attr("data-id")
attrs = element.get_attrs()
self.assertNotIn("data-id", attrs)
self.assertFalse(element.has_attr("data-id"))
class WebPageImageTests(TestCase):
def test_capture_page(self):
p = ph.open(TEST_FILE)
img = p.to_image()
blob = img.to_blob()
self.assertEqual(len(blob), 10931)
if __name__ == "__main__":
unittest.main()