Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import codecs | |
| 7 import os | |
| 8 import sys | |
| 9 | |
| 10 TELEMETRY_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') | |
| 11 sys.path.append(TELEMETRY_DIR) | |
| 12 | |
| 13 from telemetry.util import wpr_modes | |
| 14 from telemetry.core import util | |
| 15 from telemetry.internal.browser import browser_finder | |
| 16 from telemetry.internal.browser import browser_options | |
| 17 from telemetry.internal.util import binary_manager | |
| 18 | |
| 19 | |
| 20 def SnapPage(finder_options, url, interactive, snapshot_path): | |
|
pdr.
2017/09/05 21:30:41
WDYT of adding a simple end-to-end integration tes
| |
| 21 """ Save the HTML snapshot of the page whose address is |url| to | |
| 22 |snapshot_path|. | |
| 23 """ | |
| 24 snapshot_path = os.path.abspath(snapshot_path) | |
| 25 binary_manager.InitDependencyManager([]) | |
| 26 possible_browser = browser_finder.FindBrowser(finder_options) | |
| 27 browser = possible_browser.Create(finder_options) | |
| 28 try: | |
| 29 tab = browser.tabs[0] | |
| 30 tab.Navigate(url) | |
| 31 tab.WaitForDocumentReadyStateToBeComplete() | |
| 32 if interactive: | |
| 33 raw_input( | |
| 34 'Activating interactive mode. Press enter after you finish ' | |
| 35 "interacting with the page to snapshot the page's DOM content.") | |
| 36 with open(os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', | |
| 37 'HTMLSerializer.js')) as f: | |
| 38 snapit_script = f.read() | |
| 39 tab.ExecuteJavaScript(snapit_script) | |
| 40 tab.ExecuteJavaScript( | |
| 41 ''' | |
| 42 var serializedDomArray; | |
| 43 var htmlSerializer = new HTMLSerializer(); | |
| 44 htmlSerializer.processDocument(document); | |
| 45 htmlSerializer.fillHolesAsync(document, function(s) { | |
| 46 serializedDomArray = s.html; | |
| 47 }); | |
| 48 ''') | |
| 49 print 'Snapshotting content of %s. This could take a while...' % url | |
| 50 tab.WaitForJavaScriptCondition('serializedDomArray !== undefined') | |
| 51 serialized_dom = ''.join(tab.EvaluateJavaScript('serializedDomArray')) | |
|
pdr.
2017/09/05 21:30:41
What is ''.join for? Is this converting the result
| |
| 52 with codecs.open(snapshot_path, 'w', 'utf-8') as f: | |
| 53 f.write(serialized_dom) | |
| 54 print 'Successfully saved snapshot to file://%s' % snapshot_path | |
| 55 finally: | |
| 56 browser.Close() | |
| 57 | |
| 58 | |
| 59 def main(args): | |
| 60 options = browser_options.BrowserFinderOptions() | |
| 61 options.browser_options.wpr_mode = wpr_modes.WPR_OFF | |
| 62 parser = options.CreateParser( | |
| 63 usage='Create a static HTML snapshot of the page') | |
| 64 parser.add_option('--url', help='URL of the web page to record') | |
| 65 parser.add_option('--interactive', action="store_true", default=False, | |
| 66 help='Activate interactive mode after loading the page') | |
| 67 parser.add_option('--snapshot-path', help='Where to save the snapshot', | |
| 68 default='snapshot.html') | |
| 69 parser.parse_args(args) | |
| 70 SnapPage(options, options.url, options.interactive, options.snapshot_path) | |
| 71 | |
| 72 if __name__ == '__main__': | |
| 73 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |