fix 修改截图路径
This commit is contained in:
@@ -9,8 +9,9 @@ import org.openqa.selenium.support.ui.Select;
|
|||||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -371,33 +372,53 @@ public class SeleniumUtils {
|
|||||||
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
|
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 截屏并保存为文件
|
||||||
|
* @param filePath 保存路径的基础目录
|
||||||
|
* @return 保存后的文件完整路径
|
||||||
|
*/
|
||||||
public String takeScreenshotAsFile(String filePath) {
|
public String takeScreenshotAsFile(String filePath) {
|
||||||
// 获取截图文件
|
|
||||||
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 创建目标文件
|
// 生成文件名: screenshot_年月日_时分秒.png
|
||||||
File destFile = new File(filePath);
|
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||||||
|
String fileName = "screenshot_" + timestamp + ".png";
|
||||||
|
|
||||||
// 确保父目录存在
|
// 确保目录存在
|
||||||
File parentDir = destFile.getParentFile();
|
File directory = new File(filePath);
|
||||||
if (parentDir != null) {
|
if (!directory.exists()) {
|
||||||
parentDir.mkdirs();
|
boolean created = directory.mkdirs();
|
||||||
|
if (!created) {
|
||||||
|
log.error("无法创建目录: {}", filePath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将截图文件复制到指定路径
|
// 检查目录是否可写
|
||||||
FileUtils.copyFile(srcFile, destFile);
|
if (!directory.canWrite()) {
|
||||||
|
log.error("目录不可写: {}", filePath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// 返回保存的文件路径
|
// 获取截图临时文件
|
||||||
return destFile.getAbsolutePath();
|
File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Failed to save screenshot to " + filePath, e);
|
// 构建目标文件路径
|
||||||
} finally {
|
String fullPath = filePath + File.separator + fileName;
|
||||||
// 删除临时文件
|
File targetFile = new File(fullPath);
|
||||||
srcFile.delete();
|
|
||||||
|
// 复制文件到目标路径
|
||||||
|
FileUtils.copyFile(tempFile, targetFile);
|
||||||
|
|
||||||
|
log.info("截图已保存到: {}", targetFile.getAbsolutePath());
|
||||||
|
return targetFile.getAbsolutePath();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("截图保存失败: {}", e.getMessage());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 关闭浏览器
|
// 关闭浏览器
|
||||||
public void quit() {
|
public void quit() {
|
||||||
if (driver != null) {
|
if (driver != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user