ui自动化执行接口

This commit is contained in:
2025-04-28 17:00:42 +08:00
parent 5442c6b688
commit 2d8be3482a
6 changed files with 107 additions and 74 deletions

View File

@@ -855,4 +855,16 @@ public class SeleniumUtils {
}
}
/**
* 在新标签页打开URL
* @param url 要打开的网址
*/
public void openUrlInNewTab(String url) {
// 打开新标签页
driver.switchTo().newWindow(WindowType.TAB);
// 导航到目标URL
driver.get(url);
}
}

View File

@@ -107,7 +107,7 @@ public class UiAutomationController extends BaseController
*/
// @PreAuthorize("@ss.hasPermi('system:automation:remove')")
@GetMapping("/executeStep")
public AjaxResult remove(@RequestParam Long id)
public AjaxResult executeStep(@RequestParam Long id)
{
try{
log.info("执行完成!");

View File

@@ -187,7 +187,7 @@ public class UiAutomationServiceImpl implements IUiAutomationService {
uiAutomation.setUpdateTime(DateUtils.getNowDate());
uiAutomation.setCreateBy(SecurityUtils.getUsername());
uiAutomation.setUpdateBy(SecurityUtils.getUsername());
uiAutomation.setStatus("1"); //未开始
// uiAutomation.setStatus("1"); //未开始
uiAutomation.setDutyBy(SecurityUtils.getUsername());
uiAutomation.setDelFlag(0);
uiAutomation.setCrontabStatus(0); //未开启

View File

@@ -24,6 +24,7 @@ import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -45,7 +46,8 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
@Resource
private UiAutomationMapper uiAutomationMapper;
@Value("${test.selenium.chrome-driver-path}")
private String chromeDriverPath;
/**
@@ -63,9 +65,9 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
//修改ui_automation 状态为进行中 执行结果为running
UiAutomation uiAutomation = new UiAutomation();
uiAutomation.setStatus("2");
uiAutomation.setId(automationId);
uiAutomation.setExecutionResult("1");
uiAutomation.setUpdateBy(DateUtils.getTime());
uiAutomation.setUpdateTime(DateUtils.getNowDate());
uiAutomationMapper.updateUiAutomation(uiAutomation);
@@ -138,15 +140,16 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
uiReportService.updateUiReport(uiReport);
//修改ui_automation
uiAutomation = new UiAutomation();
UiAutomation uiAutomation1 = new UiAutomation();
uiAutomation1.setId(automationId);
uiAutomation1.setExecutionResult("2");
if (!uiAutomation.getExecutionResult().equals("2")) {
uiAutomation.setExecutionResult("3");
uiAutomation.setStatus("3");
uiAutomation1.setExecutionResult("3");
}
uiAutomation.setUpdateBy(DateUtils.getTime());
uiAutomation.setPassRate(stepsSucceedNumber == 0 ? "0%" :
uiAutomation1.setUpdateTime(DateUtils.getNowDate());
uiAutomation1.setPassRate(stepsSucceedNumber == 0 ? "0%" :
String.format("%.2f%%", (double) stepsSucceedNumber / steps.size() * 100));
uiAutomationMapper.updateUiAutomation(uiAutomation);
uiAutomationMapper.updateUiAutomation(uiAutomation1);
}
return result;
}
@@ -194,7 +197,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
* @param steps
*/
private void executeAllSteps(List<UiSceneSteps> steps, Long reportId) {
System.setProperty("webdriver.chrome.driver", "D:/chromedriver-win64/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
WebDriver driver = new ChromeDriver();
SeleniumUtils seleniumUtils = new SeleniumUtils(driver);
Integer orderNumber = 0;
@@ -265,7 +268,6 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
}
/**
* 执行步骤并记录日志
*
@@ -286,6 +288,13 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
Integer screenshotConfiguration = otherSettingsQO.getScreenshotConfiguration(); //是否截图 1当前步骤截图 2异常截图 3 不截图
//等待元素超时时间
Integer waitElementTime = otherSettingsQO.getWaitElementTime() == null ? 0 : otherSettingsQO.getWaitElementTime();
if (waitElementTime > 0) {
// 转换为秒至少1秒
long timeoutInSeconds = Math.max(1, waitElementTime / 1000);
log.info("元素等待时间{}秒", timeoutInSeconds);
seleniumUtils.setWaitTimeout(timeoutInSeconds);
}
//前置操作设置
List<UiHighSettingVO> beforeSettingList = extractbeforeSetting(uiHighSettingVOList);
//后置操作设置
@@ -350,6 +359,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
/**
* 提取前置操作
*
* @param settings
* @return
*/
@@ -361,6 +371,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
/**
* 获取(前置//后置)等待时间
*
* @param settingList
* @return
*/
@@ -384,6 +395,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
/**
* 提取后置操作
*
* @param settings
* @return
*/
@@ -395,6 +407,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
/**
* 提取错误处理设置
*
* @param settings
* @return
*/
@@ -406,10 +419,9 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
}
/**
* 提取错误处理设置
*
* @param settings
* @return
*/
@@ -421,11 +433,6 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
}
//---------------------浏览器操作---------------------------
/**
@@ -435,8 +442,17 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
* @param seleniumUtils Selenium工具类
*/
private void openWebPage(UiSceneSteps step, SeleniumUtils seleniumUtils) {
log.info("打开网页:{}",step.getUrl());
seleniumUtils.openUrl(step.getUrl());
//追加页面在新的页面打开url不勾选覆盖当前url 0不追加 1追加
Integer isAppendPage = step.getIsAppendPage(); // 0=当前页1=新标签页
String url = step.getUrl();
log.info("打开网页:{}, 模式:{}", url, isAppendPage == 1 ? "新标签页" : "当前页");
if (isAppendPage != null && isAppendPage == 1) {
// 新标签页打开
seleniumUtils.openUrlInNewTab(url);
} else {
// 默认当前页打开
seleniumUtils.openUrl(url);
}
}
/**
@@ -447,7 +463,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
*/
private void closeBrowser(UiSceneSteps step, SeleniumUtils seleniumUtils) {
log.info("关闭网页");
seleniumUtils.quit();
seleniumUtils.closeCurrentWindow();
}
/**
@@ -746,6 +762,7 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
/**
* 鼠标点击操作
*
* @param step 步骤对象
* @param seleniumUtils Selenium工具类
*/
@@ -781,12 +798,13 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
throw new RuntimeException("鼠标操作类型必须是数字(1-5)", e);
} catch (Exception e) {
log.error("鼠标操作执行失败", e);
throw new RuntimeException("鼠标操作失败: " + e.getMessage(), e);
throw new RuntimeException("鼠标操作失败: " + e, e);
}
}
/**
* 鼠标移动操作
*
* @param step 步骤对象
* @param seleniumUtils Selenium工具类
*/
@@ -824,12 +842,13 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
throw new RuntimeException("鼠标移动类型必须是数字(1-2)", e);
} catch (Exception e) {
log.error("鼠标移动操作执行失败", e);
throw new RuntimeException("鼠标移动操作失败: " + e.getMessage(), e);
throw new RuntimeException("鼠标移动操作失败: " + e, e);
}
}
/**
* 输入操作
*
* @param step 步骤对象
* @param seleniumUtils Selenium工具类
*/
@@ -883,12 +902,13 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
} catch (Exception e) {
log.error("输入操作执行失败", e);
throw new RuntimeException("输入操作失败: " + e.getMessage(), e);
throw new RuntimeException("输入操作失败: " + e, e);
}
}
/**
* 文件上传
*
* @param step
* @param seleniumUtils
*/

View File

@@ -56,7 +56,7 @@
<if test="stepsErrorNumber != null "> and steps_error_number = #{stepsErrorNumber}</if>
<if test="stepsNotNumber != null "> and steps_not_number = #{stepsNotNumber}</if>
</where>
order by create_time asc
order by create_time desc
</select>
<select id="selectUiReportById" parameterType="Long" resultMap="UiReportResult">

View File

@@ -49,5 +49,6 @@ export function executeAutomationData(query) {
url: '/test/automation/executeStep',
method: 'get',
params: query,
timeout: 600000,
})
}