forked from fake-name/ChromeController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
275 lines (212 loc) · 6.49 KB
/
test.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
import logging
import os.path
import gc
import pprint
import time
import astor
import WebRequest
import ChromeController.manager as mgr
import ChromeController
def test_delete_cookies():
crbin = "google-chrome"
cr = ChromeController.ChromeRemoteDebugInterface(binary=crbin, dbg_port=9232)
canClear = cr.Network_canClearBrowserCookies()
print("Can clear cookies: ", canClear)
assert canClear['result']['result'] == True
# print(cr)
resp = cr.Emulation_setVisibleSize(1500, 1000)
# print("Viewport size", resp)
print("Navigating to whatarecookies.com")
try:
resp = cr.blocking_navigate_and_get_source("http://www.whatarecookies.com/cookietest.asp", timeout=10)
except Exception as e:
raise e
print("Sampling cookies #1")
cooks1 = cr.Network_getAllCookies()['result']['cookies']
print("Doing cookie clear")
res = cr.Network_clearBrowserCookies()
print("Cookie clear result: ", res)
print("Sampling cookies #2")
cooks2 = cr.Network_getAllCookies()['result']['cookies']
print("Navigating to goat.com")
try:
resp = cr.blocking_navigate_and_get_source("http://goat.com", timeout=10)
except Exception as e:
raise e
cooks6 = cr.get_cookies()
print("Cookies: ", cooks6)
for cook in cooks6:
cr.set_cookie(cook)
print("Set cookies")
# print("Doing cookie clear")
# res = cr.Network_clearBrowserCookies()
# print("Cookie clear result: ", res)
# print("sleeping 15")
# time.sleep(15)
# print("Sampling cookies #3")
# cooks3 = cr.Network_getAllCookies()['result']['cookies']
# print("Doing cookie clear")
# res = cr.Network_clearBrowserCookies()
# print("Cookie clear result: ", res)
# print("sleeping 15")
# time.sleep(15)
# print("Sampling cookies #4")
# cooks4 = cr.Network_getAllCookies()['result']['cookies']
# print("Doing cookie clear")
# res = cr.clear_cookies()
# print("Cookie clear result: ", res)
# print("sleeping 15")
# time.sleep(15)
# print("Sampling cookies #4")
# cooks5 = cr.Network_getAllCookies()['result']['cookies']
# print()
# print("Uncleared Cookies (#1):")
# for cookie1 in cooks1:
# print(" ", cookie1)
# print()
# print("Cleared cookies (#2):")
# for cookie2 in cooks2:
# print(" ", cookie2)
# print()
# print("Navigated and cleared cookies after delay (#3):")
# for cookie3 in cooks3:
# print(" ", cookie3)
# print()
# print("Navigated and cleared cookies after another delay (#4):")
# for cookie4 in cooks3:
# print(" ", cookie4)
def test():
print("Starting!")
ua = dict(WebRequest.getUserAgent())
# print(ua)
crbin = os.path.abspath("./vendored/headless_shell")
cr = ChromeController.ChromeRemoteDebugInterface(binary=crbin, dbg_port=9232)
cooks1 = cr.get_cookies()
resp = cr.update_headers(ua)
print("Set user agent: ", resp)
# print("Set extra headers: ", resp)
# print(cr)
resp = cr.Emulation_setVisibleSize(1500, 1000)
# print("Viewport size", resp)
print("Doing first navigation...")
try:
resp = cr.blocking_navigate_and_get_source("http://www.whatarecookies.com/cookietest.asp", timeout=10)
except Exception as e:
have = cr.drain_transport()
for msg in have:
pprint.pprint(msg)
raise e
# resp = cr.blocking_navigate_and_get_source("http://10.1.1.8:33507/index")
# resp = cr.blocking_navigate_and_get_source("http://10.1.1.8:33507/index")
cr.click_link_containing_url("/test")
# print("Page.navigate", resp['content'])
img = cr.take_screeshot()
with open("screenshot.png", "wb") as fp:
fp.write(img)
# resp = cr.synchronous_command("Page.captureScreenshot", {})
# print("Page.captureScreenshot", resp)
ctnt = cr.get_rendered_page_source()
# print("Source:")
# print(ctnt)
cooks1 = cr.get_cookies()
print("Doing second navigation...")
resp = cr.blocking_navigate_and_get_source("http://goat.com", timeout=10)
# cr.Network_clearBrowserCookies()
print("Getting cookies!")
cooks2 = cr.get_cookies()
cooks3 = cr.get_cookies(all_cookies=False)
cr.Network_clearBrowserCookies()
cooks4 = cr.get_cookies()
print()
print("Pre-clearing cookies:")
for cookie in cooks1:
print(cookie)
print(cookie._rest)
print()
print("Global Cookies:")
for cookie in cooks2:
print(cookie)
print(cookie._rest)
print()
print("Local only cookies:")
for cookie in cooks3:
print(cookie)
print(cookie._rest)
print()
print("Cleared cookies:")
for cookie in cooks4:
print(cookie)
print(cookie._rest)
# for cook in cooks1:
# ret = cr.set_cookie(cook)
# print(ret)
# print()
# print("Reinstated cookies:")
# cooks3 = cr.get_cookies()
# for cookie in cooks3:
# print(cookie)
# print(cookie._rest)
wait_time = 5
for x in range(wait_time):
data = cr.drain_transport()
# pprint.pprint(data)
print("Sleeping: ", wait_time - x)
# print("Draining!")
# pprint.pprint(cr.drain_transport())
def test_cycle():
crbin = "google-chrome"
for x in range(30):
print("Starting loop %s" % x)
with ChromeController.ChromeContext(crbin) as cr:
print("Looping:", x)
print(cr)
cr.blocking_navigate('http://www.google.com')
print("Deleted")
print("Ending loop %s" % x)
def test_tabs():
crbin = "google-chrome"
for x in range(30):
with ChromeController.ChromeContext(binary="google-chrome") as cr:
print("Context manager entered")
tabl = [cr.new_tab(), cr.new_tab(), cr]
print("Tabs:", tabl)
print("Transport:")
print(tabl[0].transport)
# cr.blocking_navigate("http://www.google.com", timeout=10)
print("Loop")
for idx, tab in enumerate(tabl):
print("Fetching using tab %s -> %s" % (idx, tab))
tab.blocking_navigate("http://www.google.com", timeout=10)
print("Complete")
def test_url():
crbin = "google-chrome"
with ChromeController.ChromeContext(crbin) as cr:
cr.blocking_navigate("http://www.google.com", timeout=10)
print("Current URL:", cr.get_current_url())
# cr.close()
def test_title():
crbin = "google-chrome"
with ChromeController.ChromeContext(crbin) as cr:
cr.blocking_navigate("http://www.google.com", timeout=10)
print("Current URL:", cr.get_current_url())
print(cr.get_page_url_title())
# cr.close()
def test_rendered_fetch():
crbin = "google-chrome"
cr = ChromeController.ChromeRemoteDebugInterface(binary=crbin)
resp = cr.blocking_navigate("https://www.catatopatch.com/appraise-chapter-15", timeout=10)
print("Current URL:", cr.get_current_url())
rcnt = cr.get_rendered_page_source()
print("content:", type(rcnt))
# cr.close()
if __name__ == '__main__':
import logSetup
logSetup.initLogging(logging.DEBUG)
# test()
test_delete_cookies()
# test_title()
# test_tabs()
# test_cycle()
# test_rendered_fetch()
# test_url()
# docstring_dbg()