Chromium Code Reviews| Index: telemetry/bin/snap_page |
| diff --git a/telemetry/bin/snap_page b/telemetry/bin/snap_page |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..75ed8444e2cef66239b25ca1bca96acea9192b51 |
| --- /dev/null |
| +++ b/telemetry/bin/snap_page |
| @@ -0,0 +1,73 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import codecs |
| +import os |
| +import sys |
| + |
| +TELEMETRY_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') |
| +sys.path.append(TELEMETRY_DIR) |
| + |
| +from telemetry.util import wpr_modes |
| +from telemetry.core import util |
| +from telemetry.internal.browser import browser_finder |
| +from telemetry.internal.browser import browser_options |
| +from telemetry.internal.util import binary_manager |
| + |
| + |
| +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
|
| + """ Save the HTML snapshot of the page whose address is |url| to |
| + |snapshot_path|. |
| + """ |
| + snapshot_path = os.path.abspath(snapshot_path) |
| + binary_manager.InitDependencyManager([]) |
| + possible_browser = browser_finder.FindBrowser(finder_options) |
| + browser = possible_browser.Create(finder_options) |
| + try: |
| + tab = browser.tabs[0] |
| + tab.Navigate(url) |
| + tab.WaitForDocumentReadyStateToBeComplete() |
| + if interactive: |
| + raw_input( |
| + 'Activating interactive mode. Press enter after you finish ' |
| + "interacting with the page to snapshot the page's DOM content.") |
| + with open(os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', |
| + 'HTMLSerializer.js')) as f: |
| + snapit_script = f.read() |
| + tab.ExecuteJavaScript(snapit_script) |
| + tab.ExecuteJavaScript( |
| + ''' |
| + var serializedDomArray; |
| + var htmlSerializer = new HTMLSerializer(); |
| + htmlSerializer.processDocument(document); |
| + htmlSerializer.fillHolesAsync(document, function(s) { |
| + serializedDomArray = s.html; |
| + }); |
| + ''') |
| + print 'Snapshotting content of %s. This could take a while...' % url |
| + tab.WaitForJavaScriptCondition('serializedDomArray !== undefined') |
| + serialized_dom = ''.join(tab.EvaluateJavaScript('serializedDomArray')) |
|
pdr.
2017/09/05 21:30:41
What is ''.join for? Is this converting the result
|
| + with codecs.open(snapshot_path, 'w', 'utf-8') as f: |
| + f.write(serialized_dom) |
| + print 'Successfully saved snapshot to file://%s' % snapshot_path |
| + finally: |
| + browser.Close() |
| + |
| + |
| +def main(args): |
| + options = browser_options.BrowserFinderOptions() |
| + options.browser_options.wpr_mode = wpr_modes.WPR_OFF |
| + parser = options.CreateParser( |
| + usage='Create a static HTML snapshot of the page') |
| + parser.add_option('--url', help='URL of the web page to record') |
| + parser.add_option('--interactive', action="store_true", default=False, |
| + help='Activate interactive mode after loading the page') |
| + parser.add_option('--snapshot-path', help='Where to save the snapshot', |
| + default='snapshot.html') |
| + parser.parse_args(args) |
| + SnapPage(options, options.url, options.interactive, options.snapshot_path) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv[1:])) |