From 0d59e3494e5c7d3284d0e7bd8c68833cb022d4d0 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Sat, 18 Dec 2021 13:22:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=A1=B9=E7=9B=AE=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E5=91=8A):=20=E9=A1=B9=E7=9B=AE=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E5=91=8A=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基于docker-selenium的项目定时报告开发 --- .../commons/constants/ParamConstants.java | 5 +- .../metersphere/dto/BaseSystemConfigDTO.java | 1 + .../reportstatistics/dto/HeadlessRequest.java | 9 +++- .../service/ReportStatisticsService.java | 7 +-- .../reportstatistics/utils/ChromeUtils.java | 52 +++++++++++-------- .../service/SystemParameterService.java | 3 ++ backend/src/main/java/io/metersphere/xpack | 2 +- .../resources/i18n/messages_en_US.properties | 8 +-- .../settings/system/BaseSetting.vue | 5 ++ frontend/src/business/components/xpack | 2 +- frontend/src/i18n/en-US.js | 4 ++ frontend/src/i18n/zh-CN.js | 4 ++ frontend/src/i18n/zh-TW.js | 4 ++ 13 files changed, 72 insertions(+), 34 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java b/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java index 3abdeee089..552527c378 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java +++ b/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java @@ -88,7 +88,7 @@ public interface ParamConstants { } } - enum MAIL implements ParamConstants{ + enum MAIL implements ParamConstants { SERVER("smtp.host"), PORT("smtp.port"), ACCOUNT("smtp.account"), @@ -112,7 +112,8 @@ public interface ParamConstants { enum BASE implements ParamConstants { URL("base.url"), CONCURRENCY("base.concurrency"), - PROMETHEUS_HOST("base.prometheus.host"); + PROMETHEUS_HOST("base.prometheus.host"), + SELENIUM_DOCKER_URL("base.selenium.docker.url"); private String value; diff --git a/backend/src/main/java/io/metersphere/dto/BaseSystemConfigDTO.java b/backend/src/main/java/io/metersphere/dto/BaseSystemConfigDTO.java index 7d0cf7bd7a..804ba93ecd 100644 --- a/backend/src/main/java/io/metersphere/dto/BaseSystemConfigDTO.java +++ b/backend/src/main/java/io/metersphere/dto/BaseSystemConfigDTO.java @@ -9,4 +9,5 @@ public class BaseSystemConfigDTO { private String url; private String concurrency; private String prometheusHost; + private String seleniumDockerUrl; } diff --git a/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java b/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java index 90faa26654..171a007233 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java @@ -2,10 +2,15 @@ package io.metersphere.reportstatistics.dto; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; @Getter @Setter public class HeadlessRequest { - public String url; - public String driverPath; + private String url; + private String remoteDriverUrl; + + public boolean isEmpty() { + return StringUtils.isEmpty(this.url) || StringUtils.isEmpty(this.remoteDriverUrl); + } } diff --git a/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java b/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java index f879d36bc4..b20462e173 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java @@ -251,18 +251,19 @@ public class ReportStatisticsService { } public String getImageContentById(ReportStatisticsWithBLOBs reportRecordId) { - ChromeUtils chromeUtils = new ChromeUtils(); + ChromeUtils chromeUtils = ChromeUtils.getInstance(); HeadlessRequest headlessRequest = new HeadlessRequest(); - BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); // 占位符 String platformUrl = "http://localhost:8081"; + String remoteDriverUrl = "http://localhost:4444"; if (baseInfo != null) { platformUrl = baseInfo.getUrl(); + remoteDriverUrl = baseInfo.getSeleniumDockerUrl(); } platformUrl += "/echartPic?shareId=" + reportRecordId.getId(); - headlessRequest.setUrl(platformUrl); + headlessRequest.setRemoteDriverUrl(remoteDriverUrl); String imageData = chromeUtils.getImageInfo(headlessRequest); return imageData; } diff --git a/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java b/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java index 5f73764e36..75bb773055 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java @@ -1,21 +1,30 @@ package io.metersphere.reportstatistics.utils; +import com.alibaba.fastjson.JSON; import io.metersphere.commons.utils.LogUtil; import io.metersphere.reportstatistics.dto.HeadlessRequest; import org.apache.commons.lang3.StringUtils; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.net.URL; public class ChromeUtils { - private static final String DEFAULT_DRIVERPATH = "/Users/handsomesong/chromeDriver/chromedriver_mac64_m1/chromedriver"; + private static ChromeUtils chromeUtils = new ChromeUtils(); - private WebDriver genWebDriver(HeadlessRequest headlessRequest) { - String driverPath = headlessRequest.driverPath; - if (StringUtils.isEmpty(driverPath)) { - driverPath = DEFAULT_DRIVERPATH; + private ChromeUtils() { + } + + public static ChromeUtils getInstance() { + return chromeUtils; + } + + private synchronized WebDriver genWebDriver(HeadlessRequest headlessRequest) { + if (headlessRequest.isEmpty()) { + LogUtil.error("Headless request is null! " + JSON.toJSONString(headlessRequest)); + return null; } //初始化一个chrome浏览器实例driver ChromeOptions options = new ChromeOptions(); @@ -30,45 +39,46 @@ public class ChromeUtils { options.addArguments("no-proxy-server"); options.addArguments("disable-dev-shm-usage"); options.addArguments("lang=zh_CN.UTF-8"); + WebDriver driver = null; try { - System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverPath); - driver = new ChromeDriver(options); - driver.get(headlessRequest.url); + driver = new RemoteWebDriver(new URL(headlessRequest.getRemoteDriverUrl()), options); + driver.get(headlessRequest.getUrl()); driver.manage().window().fullscreen(); - }catch (Exception e){ - if(driver != null){ + } catch (Exception e) { + if (driver != null) { driver.quit(); driver = null; } LogUtil.error(e); } - return driver; + return driver; } - public String getImageInfo(HeadlessRequest request){ + public synchronized String getImageInfo(HeadlessRequest request) { WebDriver driver = this.genWebDriver(request); String files = null; - if(driver != null){ + if (driver != null) { try { //预留echart动画的加载时间 Thread.sleep(3 * 1000); String js = "var chartsCanvas = document.getElementById('picChart').getElementsByTagName('canvas')[0];" + "var imageUrl = null;" + "if (chartsCanvas!= null) {" + - " imageUrl = chartsCanvas && chartsCanvas.toDataURL('image/png');"+ + " imageUrl = chartsCanvas && chartsCanvas.toDataURL('image/png');" + "return imageUrl;" + "}"; - files = ((JavascriptExecutor)driver).executeScript(js).toString(); - }catch (Exception e){ + files = ((JavascriptExecutor) driver).executeScript(js).toString(); + } catch (Exception e) { LogUtil.error(e); - }finally { + } finally { driver.quit(); } } - if(StringUtils.isNotEmpty(files)){ + if (StringUtils.isNotEmpty(files)) { return files; - }else { + } else { + LogUtil.error("获取报表图片失败!参数:" + JSON.toJSONString(request)); return null; } } diff --git a/backend/src/main/java/io/metersphere/service/SystemParameterService.java b/backend/src/main/java/io/metersphere/service/SystemParameterService.java index dd87660a8d..8e90ef8947 100644 --- a/backend/src/main/java/io/metersphere/service/SystemParameterService.java +++ b/backend/src/main/java/io/metersphere/service/SystemParameterService.java @@ -253,6 +253,9 @@ public class SystemParameterService { if (StringUtils.equals(param.getParamKey(), ParamConstants.BASE.PROMETHEUS_HOST.getValue())) { baseSystemConfigDTO.setPrometheusHost(param.getParamValue()); } + if (StringUtils.equals(param.getParamKey(), ParamConstants.BASE.SELENIUM_DOCKER_URL.getValue())) { + baseSystemConfigDTO.setSeleniumDockerUrl(param.getParamValue()); + } } } return baseSystemConfigDTO; diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index 2165b7cc0b..b0c1379883 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit 2165b7cc0b0358b0b792bc06ff25570dbb230a5c +Subproject commit b0c13798836a8bfbc1d415bc5a9610c56df61833 diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index 5a41eeb6fc..acea013e9e 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -113,7 +113,7 @@ test_case_remark=Remark test_case_step_desc=Step description test_case_step_result=Step result test_case_module=Module -test_case=Test case +test_case=Case user=User user_import_template_name=User import templates user_import_template_sheet=templates @@ -276,9 +276,9 @@ test_case_status_success=Success test_case_status_trash=Trash test_case_status_saved=Saved # track home -api_case=API -performance_case=PERFORMANCE -scenario_case=SCENARIO +api_case=Api test +performance_case=Performance test +scenario_case=Scenario test create_user=Create user test_case_status=Case status id_not_rightful=ID is not rightful diff --git a/frontend/src/business/components/settings/system/BaseSetting.vue b/frontend/src/business/components/settings/system/BaseSetting.vue index 92514a6c0a..143e9984ac 100644 --- a/frontend/src/business/components/settings/system/BaseSetting.vue +++ b/frontend/src/business/components/settings/system/BaseSetting.vue @@ -15,6 +15,10 @@ ({{ $t('commons.examples') }}:http://ms-prometheus:9090) + + + ({{ $t('commons.examples') }}:http://localhost:4444) + @@ -91,6 +95,7 @@ export default { {paramKey: "base.url", paramValue: this.formInline.url, type: "text", sort: 1}, {paramKey: "base.concurrency", paramValue: this.formInline.concurrency, type: "text", sort: 2}, {paramKey: "base.prometheus.host", paramValue: this.formInline.prometheusHost, type: "text", sort: 1}, + {paramKey: "base.selenium.docker.url", paramValue: this.formInline.seleniumDockerUrl, type: "text", sort: 1}, ]; this.$refs[formInline].validate(valid => { diff --git a/frontend/src/business/components/xpack b/frontend/src/business/components/xpack index 77076b0e65..11750dbae3 160000 --- a/frontend/src/business/components/xpack +++ b/frontend/src/business/components/xpack @@ -1 +1 @@ -Subproject commit 77076b0e650fe18119b434c86c724ad61d43ce21 +Subproject commit 11750dbae3a23590a8e58c7b42f5838be51677af diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index e247bb6a1b..1317615983 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -449,6 +449,10 @@ export default { host: 'Prometheus Host', host_tip: 'example: http://ms-prometheus:9090', host_is_null: 'Prometheus Host cannot be empty' + }, + selenium_docker_url: 'selenium-docker URL', + selenium_docker: { + url_tip: 'example: http://localhost:4444', } }, custom_field: { diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index 00afb74feb..6e5bb111c7 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -451,6 +451,10 @@ export default { host: 'Prometheus地址', host_tip: '例如:http://ms-prometheus:9090', host_is_null: 'Prometheus地址不能为空' + }, + selenium_docker_url: 'selenium-docker 地址', + selenium_docker: { + url_tip: '例如:http://localhost:4444', } }, custom_field: { diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index 738db798da..d4e00bc13a 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -451,6 +451,10 @@ export default { host: 'Prometheus地址', host_tip: '例如:http://ms-prometheus:9090', host_is_null: 'Prometheus地址不能為空' + }, + selenium_docker_url: 'selenium-docker 地址', + selenium_docker: { + url_tip: '例如:http://localhost:4444', } }, custom_field: {