diff --git a/README.md b/README.md index 1e5108c..67e1017 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ WebDriver webDriver=util.driver; The framework will also take care of the entire configuration of the Drivers, you just need to specify what supported WebExplorers do you want to run your scenarios on. -* Version: [1.0.3](https://github.com/orgs/WhiteOrganization/packages) +* Version: [1.1.0](https://github.com/orgs/WhiteOrganization/packages) This is still in development and some methods are being constantly added as they are used. Please help us by requesting those you need or need more detailed documentation. @@ -48,7 +48,7 @@ by including it in your POM](https://maven.pkg.github.com/whiteorganization/whit io.github.whiteorganization white-selenium-framework - 1.0.3 + 1.1.0 ``` If used on a long-term heavy-use project we recommend: @@ -61,7 +61,7 @@ If used on a long-term heavy-use project we recommend: io.github.whiteorganization white-selenium-framework - 1.0.3 + 1.1.0 @@ -101,7 +101,7 @@ and import the dependency like this: com.github.WhiteOrganization White_SeleniumFramework - white-selenium-framework-1.0.3 + white-selenium-framework-1.1.0 ``` diff --git a/pom.xml b/pom.xml index 943ba90..9b547f1 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.whiteorganization white-selenium-framework - 1.0.3 + 1.1.0 ${project.groupId}:${project.artifactId} A Selenium Framework that will help to execute tests and custom scenarios faster and reduce the effort to access many sections Selenium related. diff --git a/src/main/java/org/white_sdev/white_seleniumframework/framework/RecordingUtils.java b/src/main/java/org/white_sdev/white_seleniumframework/framework/RecordingUtils.java new file mode 100644 index 0000000..e4add16 --- /dev/null +++ b/src/main/java/org/white_sdev/white_seleniumframework/framework/RecordingUtils.java @@ -0,0 +1,100 @@ +package org.white_sdev.white_seleniumframework.framework; + + +import io.github.bonigarcia.seljup.SeleniumJupiter; +import lombok.SneakyThrows; +import org.apache.commons.io.FileUtils; +import org.white_sdev.white_seleniumframework.exceptions.White_SeleniumFrameworkException; + +import java.io.File; +import java.nio.file.NoSuchFileException; +import java.time.LocalDate; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@lombok.extern.slf4j.Slf4j +public class RecordingUtils { + + public static String startRecording(SeleniumJupiter seleniumJupiter, String displayName){ + String logID="::startRecording([seleniumJupiter, displayName]): "; + log.trace("{}Start ", logID); + Objects.requireNonNull(seleniumJupiter); + if(displayName == null) displayName = "recorded"; + try{ + + String recordedFileName = getFileName(displayName); + seleniumJupiter.startRecording(recordedFileName); + log.trace("{}Finish", logID); + return recordedFileName; + + } catch (Exception ex) { + throw new White_SeleniumFrameworkException("Impossible to start the recording", ex); + } + } + + public static Optional saveRecording(SeleniumJupiter seleniumJupiter, String displayName){ + return saveRecording(seleniumJupiter, displayName, null, null); + } + + @SneakyThrows + public static Optional saveRecording(SeleniumJupiter seleniumJupiter, String displayName, String filePath, String fileName){ + String logID="::saveRecording([seleniumJupiter, displayName, filePath, fileName]): "; + log.trace("{}Start ", logID); + Objects.requireNonNull(seleniumJupiter); + if(displayName == null) displayName = "recorded"; + if(filePath == null) filePath = "target/test-reports/recorded/"; + try { + + final int REC_TIMEOUT_SEC = 4; + final int POLL_TIME_MSEC = 100; + + seleniumJupiter.stopRecording(); + + long timeoutMs = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(REC_TIMEOUT_SEC); + + File recFile; + do { + recFile = getRecordedFile(displayName); + if (System.currentTimeMillis() > timeoutMs) { + log.error("Timeout of " + REC_TIMEOUT_SEC + " seconds waiting for recording " + recFile); + break; + } + Thread.sleep(POLL_TIME_MSEC); + + } while (!recFile.exists()); + + log.debug("{}Recorded File Status: {}", logID, recFile.exists()); + log.debug("{}File Name: {}", logID, recFile.getAbsolutePath()); + if (recFile.exists()) { + File movedFile =new File(filePath + (fileName!=null? fileName: recFile.getName())); + try { + log.trace("{}Removing file if a previous one was already in the directory.", logID); + FileUtils.delete(movedFile); + } catch (NoSuchFileException ex) { + log.trace("{}No previous file found, proceeding with happy path.", logID); + } + FileUtils.moveFile(recFile, movedFile); + return Optional.of(movedFile); + }else{ + log.error(logID+"Impossible to save the file. Are you '@Watch'ing your WebDriver?"); + return Optional.empty(); + } + }catch(Exception ex){ + throw new White_SeleniumFrameworkException("An error occurred when saving the video recording.", ex); + } + } + + public static File getRecordedFile(String displayName){ + final String RECORDED_EXT = ".webm"; + String recordedFileName = getFileName(displayName) + RECORDED_EXT; + final String RECORDED_FOLDER_PATH = System.getProperty("user.home") + "/Downloads"; //TODO this will probably throw an exception in other SO out of Windows. + File targetFolder = new File(RECORDED_FOLDER_PATH); + + return new File(targetFolder, recordedFileName); + } + + public static String getFileName(String displayName){ + return displayName + " " + LocalDate.now().toString().replace(":| ", "_"); + } +} \ No newline at end of file diff --git a/src/main/java/org/white_sdev/white_seleniumframework/framework/WebDriverUtils.java b/src/main/java/org/white_sdev/white_seleniumframework/framework/WebDriverUtils.java index ab2cdef..b00a53a 100644 --- a/src/main/java/org/white_sdev/white_seleniumframework/framework/WebDriverUtils.java +++ b/src/main/java/org/white_sdev/white_seleniumframework/framework/WebDriverUtils.java @@ -2775,69 +2775,17 @@ public String take(){ // //region Record - static String recordedFileName; - public static void startRecording(SeleniumJupiter seleniumJupiter, String displayName){ - String logID="::startRecording([seleniumJupiter, displayName]): "; - log.trace("{}Start ", logID); - Objects.requireNonNull(seleniumJupiter); - if(displayName == null) displayName = "recorded"; - try{ - recordedFileName = displayName + " " + LocalDate.now().toString().replace(":| ", "_"); - seleniumJupiter.startRecording(recordedFileName); - log.trace("{}Finish", logID); - - } catch (Exception ex) { - throw new RuntimeException("Impossible to start the recording", ex); - } - + public static String startRecording(SeleniumJupiter seleniumJupiter, String displayName){ + return RecordingUtils.startRecording(seleniumJupiter, displayName); } - - public static void saveRecording(SeleniumJupiter seleniumJupiter){ - saveRecording(seleniumJupiter, null); + public static Optional saveRecording(SeleniumJupiter seleniumJupiter, String displayName){ + return RecordingUtils.saveRecording(seleniumJupiter, displayName); } - - @SneakyThrows - public static void saveRecording(SeleniumJupiter seleniumJupiter, String filePath){ - String logID="::saveRecording([seleniumJupiter]): "; - log.trace("{}Start ", logID); - Objects.requireNonNull(seleniumJupiter); - if(filePath == null) filePath = "target/test-reports/recorded/"; - try { - - final int REC_TIMEOUT_SEC = 1; - final int POLL_TIME_MSEC = 100; - - final String RECORDED_EXT = ".webm"; - final String RECORDED_FOLDER_PATH = System.getProperty("user.home") + "/Downloads"; - File targetFolder = new File(RECORDED_FOLDER_PATH); - - seleniumJupiter.stopRecording(); - - long timeoutMs = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(REC_TIMEOUT_SEC); - - File recFile; - do { - recFile = new File(targetFolder, recordedFileName + RECORDED_EXT); - if (System.currentTimeMillis() > timeoutMs) { - log.error("Timeout of " + REC_TIMEOUT_SEC + " seconds waiting for recording " + recFile); - break; - } - Thread.sleep(POLL_TIME_MSEC); - - } while (!recFile.exists()); - - log.debug("{}Recorded File Status: {}", logID, recFile.exists()); - log.debug("{}File Name: {}", logID, recFile.getAbsolutePath()); - if (recFile.exists()) { - File movedFile = new File(filePath + recordedFileName + RECORDED_EXT); - try {FileUtils.delete(movedFile);} catch (NoSuchFileException ex) {} - FileUtils.moveFile(recFile, movedFile); - } - }catch(Exception ex){ - throw new White_SeleniumFrameworkException("An error occurred when saving the video recording.", ex); - } + public static Optional saveRecording(SeleniumJupiter seleniumJupiter, String displayName, String filePath, String fileName){ + return RecordingUtils.saveRecording(seleniumJupiter, displayName, filePath, fileName); } + //endregion Record //