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..74880e65830404b36186cbade9a9a175b3e25430 |
| --- /dev/null |
| +++ b/telemetry/bin/snap_page |
| @@ -0,0 +1,66 @@ |
| +#!/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, snapshot_path): |
| + """ Save the HTML snapshot of the page whose address |url| to |snapshot_path|. |
|
wkorman
2017/09/02 13:50:06
"is" after "address"
nednguyen
2017/09/05 17:49:08
Done.
|
| + """ |
| + 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() |
| + 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')) |
| + with codecs.open(snapshot_path, 'w', 'utf-8') as f: |
| + f.write(serialized_dom) |
| + print 'Successfuly saved snapshot to file://%s' % snapshot_path |
|
wkorman
2017/09/02 13:50:06
'lly' vs 'ly'
nednguyen
2017/09/05 17:49:08
Done.
|
| + 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('--snapshot-path', help='Where to save the snapshot', |
| + default='snapshot.html') |
| + parser.parse_args(args) |
| + SnapPage(options, options.url, options.snapshot_path) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv[1:])) |