java 每次Selenium WebDriver获取屏幕截图时创建一个新文件夹

4xrmg8kj  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(107)
// Take screenshot method
static void captureScreenshot(String fileName) throws IOException {

// Take the screenshot and store as file format
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// Open the current date and time
    String timestamp = new SimpleDateFormat("dd_MM_yyyy__hh_mm_ss").format(new Date());

//Copy the screenshot on the desire location with different name using current date and time
    Cache.copyFile(scrFile, new File("C:\\Users\\Kiko Kikostov\\IdeaProjects\\AboutPagesBanerScreenSizes\\15inchScreenSize\\Asia\\" + fileName + " " + timestamp + ".png"));
    String st = scrFile.getAbsolutePath();
    String str = scrFile.getParent();
    scrFile = new File(str+"/"+ "/" + fileName);
}

这对我来说很好用,但是我想实现的是,每次运行测试时,都会在现有的文件夹中创建一个新的文件夹或子文件夹。

falq053o

falq053o1#

可以使用os创建目录

import os
  
directory = "MyFolder"
parent_dir = "D:/Pycharm projects/"
path = os.path.join(parent_dir, directory)      
os.mkdir(path)
print("Directory '% s' created" % directory)

因此,对于您的使用情况,应该是这样的:

import os
parent_dir="C:\\Users\\Kiko Kikostov\\IdeaProjects\\AboutPagesBanerScreenSizes\\15inchScreenSize\\Asia\\";

// Take screenshot method
static void captureScreenshot(String fileName) throws IOException {

// Take the screenshot and store as file format
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// Open the current date and time
String timestamp = new SimpleDateFormat("dd_MM_yyyy__hh_mm_ss").format(new Date());

// Assuming you want a folder with the timestamp
directory = timestamp;
path = os.path.join(parent_dir, directory)      
os.mkdir(path)

//Copy the screenshot on the desire location with different name using current date and time
Cache.copyFile(scrFile, new File(directory + fileName + " " + timestamp + ".png"));
String st = scrFile.getAbsolutePath();
String str = scrFile.getParent();
scrFile = new File(str+"/"+ "/" + fileName);

相关问题