11import os
2- from selenium import webdriver
3- from selenium .webdriver import DesiredCapabilities
4- import time
2+ import asyncio
53import json
6- import base64
7- from run import proxy
4+ from pyppeteer import launch
85
9- SCRIPT = """var body = document.body,
10- html = document.documentElement;
11- return Math.max( body.scrollHeight, body.offsetHeight,
12- html.clientHeight, html.scrollHeight, html.offsetHeight );"""
6+ network = []
7+ javascript = []
138
149
15- def _chrome_full_screenshot (driver ):
16- def send (cmd , params ):
17- resource = "/session/%s/chromium/send_command_and_get_result" % driver .session_id
18- url = driver .command_executor ._url + resource
19- body = json .dumps ({'cmd' : cmd , 'params' : params })
20- response = driver .command_executor ._request ('POST' , url , body )
21- return response .get ('value' )
10+ async def intercept_network_response (response ):
11+ network .append (str (response .status ) + response .url )
2212
23- def evaluate (script ):
24- response = send ('Runtime.evaluate' , {'returnByValue' : True , 'expression' : script })
25- return response ['result' ]['value' ]
2613
27- metrics = evaluate ( \
28- "({" + \
29- "width: Math.max(window.innerWidth, document.body.scrollWidth, document.documentElement.scrollWidth)|0," + \
30- "height: Math.max(innerHeight, document.body.scrollHeight, document.documentElement.scrollHeight)|0," + \
31- "deviceScaleFactor: window.devicePixelRatio || 1," + \
32- "mobile: typeof window.orientation !== 'undefined'" + \
33- "})" )
34- send ('Emulation.setDeviceMetricsOverride' , metrics )
35- screenshot = send ('Page.captureScreenshot' , {'format' : 'png' , 'fromSurface' : True })
36- send ('Emulation.clearDeviceMetricsOverride' , {})
14+ async def intercept_console (response ):
15+ javascript .append (response .text )
3716
38- return base64 .b64decode (screenshot ['data' ])
17+
18+ async def collect_msgs_and_screenshot (url , ss_path ):
19+ browser = await launch ()
20+ page = await browser .newPage ()
21+
22+ page .on ('response' , intercept_network_response )
23+ page .on ('console' , intercept_console )
24+ page .on ('requestfailed' , intercept_console )
25+ page .on ('pageerror' , intercept_console )
26+ page .on ('error' , intercept_console )
27+
28+ await page .goto (url )
29+ await page .screenshot ({'path' : ss_path , 'fullPage' : True })
30+
31+ await browser .close ()
3932
4033
4134def collect_data (url , output_folder , output_filename ):
@@ -45,36 +38,11 @@ def collect_data(url, output_folder, output_filename):
4538 if not os .path .exists (output_folder ):
4639 os .mkdir (output_folder )
4740
48- options = webdriver .ChromeOptions ()
49- options .add_argument ("--start-maximized" )
50- options .add_argument ("--force-device-scale-factor=2" )
51- options .add_argument ("--disable-infobars" )
52- options .add_argument ("--headless" )
53- options .add_argument ('--proxy-server=%s' % proxy .proxy )
54- options .add_argument ('--no-sandbox' )
55- options .add_argument ('--disable-dev-shm-usage' )
56- capabilities = DesiredCapabilities .CHROME
57- capabilities ['goog:loggingPrefs' ] = {'browser' : 'ALL' }
41+ asyncio .get_event_loop ().run_until_complete (
42+ collect_msgs_and_screenshot (url , os .path .join (output_folder , output_filename )))
5843
59- driver = webdriver .Chrome (os .environ ['CHROMEDRIVER_PATH' ], chrome_options = options ,
60- desired_capabilities = capabilities , service_args = ["--verbose" ])
61- driver .maximize_window ()
62- driver .fullscreen_window ()
63- proxy .new_har ("Logs" )
64- driver .get (url )
65-
66- logs = driver .get_log ('browser' )
6744 with open (os .path .join (output_folder , output_filename .split ('.' )[0 ] + '_js_log.json' ), 'w' ) as f :
68- json .dump (logs , f , indent = 2 )
69- with open (os .path .join (output_folder , output_filename .split ('.' )[0 ] + '_network_log.json' ), 'w' ) as f :
70- json .dump (proxy .har , f , indent = 2 )
45+ json .dump (javascript , f , indent = 2 )
7146
72- height = driver .execute_script (SCRIPT )
73- for i in range (4 ):
74- driver .execute_script ("window.scrollBy(0, " + str (height / 5 ) + ")" )
75- time .sleep (3 )
76- png = _chrome_full_screenshot (driver )
77- with open (os .path .join (output_folder , output_filename ), 'wb' ) as f :
78- f .write (png )
79- driver .execute_script ("window.scrollTo(0, 0)" )
80- driver .close ()
47+ with open (os .path .join (output_folder , output_filename .split ('.' )[0 ] + '_network_log.json' ), 'w' ) as f :
48+ json .dump (network , f , indent = 2 )
0 commit comments