gpt4 book ai didi

java - 根据运行时的方法名称命名屏幕截图

转载 作者:行者123 更新时间:2023-12-02 03:58:16 26 4
gpt4 key购买 nike

在以下情况下有人可以帮助我吗?

我必须创建一个通用方法来使用 Selenium Webdriver 获取屏幕截图,其中屏幕截图的文件名应更新为我调用它的方法的名称。

这就是我现在拥有的:

创建了一个方法来获取时间戳并将其用于文件名:

public String getTimeStamp() {
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
return timestamp;
}

// Method to get the Screenshot at any given instance. The Screenshots taken are copied to Screenshots folder under `"build\jbehave\view"`.
public void getScreenShots() throws Exception {
File srcfile = driver.getScreenshotAs(OutputType.FILE);
File folder = new File("Screenshots");

if(folder.exists()){
FileUtils.copyFile(srcfile, new File("./build/jbehave/view/Screenshots/" + "Screenshot_" + getTimeStamp() + ".png"));
}else {

File dir1 = new File("Screenshots");
dir1.mkdir();
FileUtils.copyFile(srcfile, new File("./build/jbehave/view/Screenshots/" + "Screenshot_" + getTimeStamp() + ".png"));

}
}

我正在寻找一种方法,它可以获取我调用上面的方法的名称 getScreenShots()方法并在运行时将其作为我的文件名而不是时间戳。

最佳答案

您可以从当前线程的堆栈跟踪中获取方法名称:

byte[] scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
String filename = Thread.currentThread().getStackTrace()[1].toString();
FileUtils.writeByteArrayToFile(new File(filename), scrFile);

在此示例中,filename 是一个方法,它执行此片段(堆栈跟踪中为 1)。

关于java - 根据运行时的方法名称命名屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35211214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com