forked from sadnoodles/chromeremote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
113 lines (91 loc) · 3.27 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import time
from chromeremote import ChromeDevToolsConnection, ChromeTab
def print_ret(kwargs):
assert kwargs.has_key('current_tab')
return
class TestConnection(unittest.TestCase):
def setUp(self):
self.conn = ChromeDevToolsConnection('127.0.0.1', 9222)
def test_new_close_tab(self):
resp = self.conn.new()
self.assertEqual(resp.status_code, 200)
ret = resp.json()
time.sleep(1)
resp = self.conn.close(ret["id"])
self.assertEqual(resp.status_code, 200)
self.assertIn("Target is closing", resp.text)
def test_get_tabs(self):
tabs = self.conn.get_tabs()
self.assertIsInstance(tabs, list)
self.assertIsInstance(tabs[0], dict)
self.assertEqual(tabs[0].has_key('id'), True)
class TestTab(unittest.TestCase):
def setUp(self):
# Ensure Chrome dev tools is open on 127.0.0.1:9222
self.tab = ChromeTab('127.0.0.1', 9222)
self.tab.open_tab()
def test_register_event(self):
self.tab.register_event("Network.responseReceived", print_ret)
self.assertEqual(
self.tab._events_callbacks["Network.responseReceived"], print_ret)
def test_handle_event(self):
called = []
def event_func(kwargs):
called.append(kwargs["some"])
self.tab.register_event("Network.responseReceived", event_func)
self.tab.handle_event_callback(
"Network.responseReceived", {"some": "thing"})
self.assertEqual(called[0], "thing")
def test_handle_event_error(self):
called = []
# **kwarg cause error, use func(kwarg) pleas.
def event_func(**kwargs):
called.append(kwargs["some"])
self.tab.register_event("Network.responseReceived", event_func)
self.tab.handle_event_callback(
"Network.responseReceived", {"some": "thing"})
def test_handle_message_callback(self):
result = self.tab.Page.enable(callback=print_ret)
self.tab.handle_message_callback(result.message_id, {"hello": "you"})
self.assertEqual(result.ready, True)
self.assertEqual(self.tab._message_callbacks.has_key(
result.message_id), False)
def test_register_message_callback(self):
result = self.tab.Page.enable(callback=print_ret)
in_map = self.tab._message_callbacks[result.message_id]
self.assertEqual(
in_map.callback,
print_ret
)
self.assertEqual(
in_map.func_name,
"Page.enable"
)
def test_close_thread(self):
tab = ChromeTab('127.0.0.1', 9222)
tab.open_tab()
tab.start()
time.sleep(1)
tab.Page.enable()
time.sleep(2)
tab.kill()
time.sleep(1)
self.assertEqual(tab.isAlive(), False)
def test_async_return(self):
tab = ChromeTab('127.0.0.1', 9222)
tab.open_tab()
tab.start()
import time
tab.open_tab()
result = tab.Page.enable()
time.sleep(8)
tab.kill()
self.assertEqual(result.ready, True)
self.assertIsInstance(result.result, dict)
def tearDown(self):
self.tab.kill()
if __name__ == '__main__':
unittest.main()