7
7
from selenium .common import exceptions
8
8
from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
9
9
10
+ SNAPSHOT_JS = """
11
+ var ele = document.querySelector('div[_echarts_instance_]');
12
+ var mychart = echarts.getInstanceByDom(ele);
13
+ return mychart.getDataURL({
14
+ type: '%s',
15
+ pixelRatio: %s,
16
+ excludeComponents: ['toolbox']
17
+ });
18
+ """
19
+ SNAPSHOT_SVG_JS = """
20
+ var element = document.querySelector('div[_echarts_instance_] div');
21
+ return element.innerHTML;
22
+ """
23
+
10
24
11
25
def make_snapshot (
12
26
html_path : str ,
@@ -25,39 +39,27 @@ def make_snapshot(
25
39
raise Exception ('Unknown browser!' )
26
40
driver .set_script_timeout (delay + 1 )
27
41
42
+ if file_type == 'svg' :
43
+ snapshot_js = SNAPSHOT_SVG_JS
44
+ else :
45
+ snapshot_js = SNAPSHOT_JS % (file_type , pixel_ratio )
46
+
28
47
if not html_path .startswith ("http" ):
29
48
html_path = 'file://' + os .path .abspath (html_path )
30
49
driver .get (html_path )
31
50
time .sleep (delay )
32
51
33
52
try :
34
- output = driver .execute_script (__gen_js_code ( file_type , pixel_ratio , delay ) )
53
+ output = driver .execute_script (snapshot_js )
35
54
driver .close ()
36
55
return output
37
56
except exceptions .TimeoutException :
38
57
raise Exception ("Failed to get snapshot content" )
39
58
40
59
41
- def __gen_js_code (file_type : str , pixel_ratio : int , delay : int ) -> str :
42
- script = (
43
- """
44
- var ele = document.querySelector('div[_echarts_instance_]');
45
- var mychart = echarts.getInstanceByDom(ele);
46
- return mychart.getDataURL(
47
- {type:'--file-type--', pixelRatio: --pixel-ratio--, excludeComponents: ['toolbox']});
48
- """ .replace (
49
- "--file-type--" , file_type
50
- )
51
- .replace ("--pixel-ratio--" , str (pixel_ratio ))
52
- )
53
- return script
54
-
55
-
56
60
def get_chrome ():
57
61
option = webdriver .ChromeOptions ()
58
62
option .add_argument ("headless" )
59
- if sys .platform == 'darwin' :
60
- option .binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
61
63
capabilities = DesiredCapabilities .CHROME
62
64
capabilities ["loggingPrefs" ] = {"browser" : "ALL" }
63
65
return webdriver .Chrome (
0 commit comments