Skip to content

Commit 306c1e8

Browse files
committed
🔨 code refactoring
1 parent b5e3021 commit 306c1e8

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

snapshot_selenium/snapshot.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
from selenium.common import exceptions
88
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
99

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+
1024

1125
def make_snapshot(
1226
html_path: str,
@@ -25,39 +39,27 @@ def make_snapshot(
2539
raise Exception('Unknown browser!')
2640
driver.set_script_timeout(delay + 1)
2741

42+
if file_type == 'svg':
43+
snapshot_js = SNAPSHOT_SVG_JS
44+
else:
45+
snapshot_js = SNAPSHOT_JS % (file_type, pixel_ratio)
46+
2847
if not html_path.startswith("http"):
2948
html_path = 'file://' + os.path.abspath(html_path)
3049
driver.get(html_path)
3150
time.sleep(delay)
3251

3352
try:
34-
output = driver.execute_script(__gen_js_code(file_type, pixel_ratio, delay))
53+
output = driver.execute_script(snapshot_js)
3554
driver.close()
3655
return output
3756
except exceptions.TimeoutException:
3857
raise Exception("Failed to get snapshot content")
3958

4059

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-
5660
def get_chrome():
5761
option = webdriver.ChromeOptions()
5862
option.add_argument("headless")
59-
if sys.platform == 'darwin':
60-
option.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
6163
capabilities = DesiredCapabilities.CHROME
6264
capabilities["loggingPrefs"] = {"browser": "ALL"}
6365
return webdriver.Chrome(

0 commit comments

Comments
 (0)