Search code examples
cucumbercucumber-jvmcucumber-java

cucumber hook scenario.embed always create screenshot at project root


In cucumber hook scenario.embed always create screenshot at my project root directory. I need it to create it different location

scenario.embed(screenshot, "image/png");

I create below code still no wayout:

File screenShot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// extracting date for folder name.
SimpleDateFormat dateFormatForFoldername = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
Date currentDate = new Date();
String folderDateFormat = dateFormatForFoldername.format(currentDate);
// extracting date and time for snapshot file
SimpleDateFormat dateFormatForFileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy
String fileDateFormet = dateFormatForFileName.format(currentDate);
String filefolder="./ScreenShots"+"/FailCase/"+folderDateFormat+"/";
// Creating folders and files
File screenshot = new File(filefolder+fileDateFormet+".jpeg");
FileUtils.copyFile(screenShot, new File(screenshot.getPath()));
byte[] fileContent = Files.readAllBytes(screenshot.toPath());
scenario.embed(fileContent, "image/png");

How to pass a directory path to embed funtion or override it?


Solution

  • @mpkorstanje - he is rightly pointed about it

    As per his comment:

    Use OutputType.BYTES and send the bytes directly to scenario.embed and write them directly to the screenshot file

    But my issue was I am using mkolisnyk package and when I am using @AfterSuite annotation of it, it creating fail file images over root folder. seems bug in mkolisnyk package.

    Mixing AfterSuite of testng and @ExtendedCucumberOptions of mkolisnyk works for me