OLD | NEW |
1 # Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
| 6 import json |
6 | 7 |
7 from telemetry.core import util | 8 from telemetry.core import util |
8 from telemetry.internal.browser import browser_finder | 9 from telemetry.internal.browser import browser_finder |
9 | 10 |
10 | 11 |
11 def SnapPage(finder_options, url, interactive, snapshot_file): | 12 def SnapPage(finder_options, url, interactive, snapshot_file): |
12 """ Save the HTML snapshot of the page whose address is |url| to | 13 """ Save the HTML snapshot of the page whose address is |url| to |
13 |snapshot_file|. | 14 |snapshot_file|. |
14 """ | 15 """ |
15 possible_browser = browser_finder.FindBrowser(finder_options) | 16 possible_browser = browser_finder.FindBrowser(finder_options) |
16 browser = possible_browser.Create(finder_options) | 17 browser = possible_browser.Create(finder_options) |
17 try: | 18 try: |
18 tab = browser.tabs[0] | 19 tab = browser.tabs[0] |
19 tab.Navigate(url) | 20 tab.Navigate(url) |
20 tab.WaitForDocumentReadyStateToBeComplete() | |
21 if interactive: | 21 if interactive: |
22 raw_input( | 22 raw_input( |
23 'Activating interactive mode. Press enter after you finish ' | 23 'Activating interactive mode. Press enter after you finish ' |
24 "interacting with the page to snapshot the page's DOM content.") | 24 "interacting with the page to snapshot the page's DOM content.") |
25 with open( | 25 |
26 os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', | 26 print 'Snapshotting content of %s. This could take a while...' % url |
27 'HTMLSerializer.js')) as f: | 27 tab.WaitForDocumentReadyStateToBeComplete() |
| 28 tab.action_runner.WaitForNetworkQuiescence() |
| 29 |
| 30 with open(os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', |
| 31 'HTMLSerializer.js')) as f: |
28 snapit_script = f.read() | 32 snapit_script = f.read() |
29 tab.ExecuteJavaScript(snapit_script) | 33 |
30 tab.ExecuteJavaScript( | 34 with open(os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', |
31 ''' | 35 'popup.js')) as f: |
32 var serializedDomArray; | 36 dom_combining_script = f.read() |
33 var htmlSerializer = new HTMLSerializer(); | 37 |
34 htmlSerializer.processDocument(document); | 38 serialized_doms = [] |
35 htmlSerializer.fillHolesAsync(document, function(s) { | 39 |
36 serializedDomArray = s.html; | 40 # Serialize the dom in each frame. |
37 }); | 41 for context_id in tab.EnableAllContexts(): |
38 ''') | 42 tab.ExecuteJavaScript(snapit_script, context_id=context_id) |
39 print 'Snapshotting content of %s. This could take a while...' % url | 43 tab.ExecuteJavaScript( |
40 tab.WaitForJavaScriptCondition('serializedDomArray !== undefined') | 44 ''' |
41 serialized_dom = ''.join(tab.EvaluateJavaScript('serializedDomArray')) | 45 var serializedDom; |
42 snapshot_file.write(serialized_dom) | 46 var htmlSerializer = new HTMLSerializer(); |
| 47 htmlSerializer.processDocument(document); |
| 48 htmlSerializer.fillHolesAsync(document, function(s) { |
| 49 serializedDom = s.asDict(); |
| 50 }); |
| 51 ''', context_id=context_id) |
| 52 tab.WaitForJavaScriptCondition( |
| 53 'serializedDom !== undefined', context_id=context_id) |
| 54 serialized_doms.append(tab.EvaluateJavaScript( |
| 55 'serializedDom', context_id=context_id)) |
| 56 |
| 57 # Sending all the serialized doms back to tab execution context. |
| 58 tab.ExecuteJavaScript('var serializedDoms = [];') |
| 59 for sub_dom in serialized_doms: |
| 60 print len(json.dumps(sub_dom)) |
| 61 tab.ExecuteJavaScript('serializedDoms.push({{sub_dom}})', sub_dom=sub_dom) |
| 62 |
| 63 # Combine all the doms to one HTML string. |
| 64 tab.EvaluateJavaScript(dom_combining_script) |
| 65 page_snapshot = tab.ExecuteJavaScript( |
| 66 'var domString = outputHTMLString(serialized_doms);') |
| 67 |
| 68 snapshot_file.write(page_snapshot) |
43 finally: | 69 finally: |
44 browser.Close() | 70 browser.Close() |
OLD | NEW |