I am new to selenium, I want to add screen recording in my test cases, and I am using ffmpeg. The issue i am facing is the recording successfully starts, but it does not stop after the test case is successfully executed.
Below is how my code is strcutured:
package utilities;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ScreenRecording {
private Process ffmpeg;
private String outputFilePath;
private boolean isRecording;
public ScreenRecording(String outputFilePath) {
this.isRecording = false;
this.outputFilePath = outputFilePath;
}
public void startRecording() throws IOException {
String command = String.format(
"ffmpeg -y -f gdigrab -framerate 30 -probesize 100M -analyzeduration 10M -i desktop -vf "crop=1920:1080:0:0" -c:v libx264 -preset ultrafast -qp 0 -pix_fmt yuv420p %s",
outputFilePath
);
System.out.println("Executing command: " + command); // Print command for debugging
try {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
builder.redirectErrorStream(true); // Combine error and output streams
ffmpeg = builder.start();
isRecording = true;
// Log output and errors
new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ffmpeg.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
} catch (IOException e) {
e.printStackTrace(); // Print exception for debugging
throw e; // Rethrow exception to propagate it to the caller
}
}
public void stopRecording() {
if (ffmpeg != null) {
ffmpeg.destroy();
System.out.println("Recording stopped.");
isRecording = false;
}
}
public boolean isRecording() {
return isRecording;
}
}
public class AccountFeatures extends BaseTest {
@Test(testName = "Register User")
public void testRegisterUserScenario() throws InterruptedException {
HomePage homePage = new HomePage(driver);
LoginPage loginPage = new LoginPage(driver);
SignUpPage signUpPage = new SignUpPage(driver);
homePage.clickOnLogin();
Assert.assertEquals(loginPage.getSingupText(), "New User Signup!");
loginPage.enterUserName(randomName);
loginPage.enterUserEmail(randomEmail);
loginPage.clickOnSignUpButton();
signUpPage.clickOnTitle();
signUpPage.enterPassword(randomPassword);
signUpPage.selectDate(birthDay);
signUpPage.selectMonth("December");
signUpPage.selectYear();
signUpPage.selectFromCountryDropDown("Canada");
}
}
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import utilities.ScreenRecording;
import webdrivers.DriverManager;
import java.io.IOException;
public class BaseTest {
protected WebDriver driver;
private ScreenRecording recorder;
@BeforeMethod
public void setUp() throws IOException {
// Initialize WebDriver (you can choose Chrome, Firefox, etc.)
String outputFilePath = "D:\Projects\screen_recordings\screen_recording.mp4";
recorder = new ScreenRecording(outputFilePath);
recorder.startRecording();
driver = DriverManager.getDriver();
driver.get(DataConfig.baseUrl);
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
if (recorder != null && recorder.isRecording()) {
recorder.stopRecording();
}
}
}
I tried different solutions but it does not work at all. Before that i was using @AfterTest annotations but it was giving stack overflow exception.