fix 修改截图路径

This commit is contained in:
2025-06-19 17:13:06 +08:00
parent 49d63847ee
commit cfdc292c1d

View File

@@ -1,12 +1,15 @@
package com.test.common.utils; package com.test.common.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*; import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select; 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.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -368,9 +371,31 @@ public class SeleniumUtils {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
} }
// 截屏并保存为文件
public String takeScreenshotAsFile(String filePath) { public String takeScreenshotAsFile(String filePath) {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE).getAbsolutePath(); // 获取截图文件
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// 创建目标文件
File destFile = new File(filePath);
// 确保父目录存在
File parentDir = destFile.getParentFile();
if (parentDir != null) {
parentDir.mkdirs();
}
// 将截图文件复制到指定路径
FileUtils.copyFile(srcFile, destFile);
// 返回保存的文件路径
return destFile.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Failed to save screenshot to " + filePath, e);
} finally {
// 删除临时文件
srcFile.delete();
}
} }
// 关闭浏览器 // 关闭浏览器