Skip to content

Commit

Permalink
Merge pull request #39 from WhiteOrganization/recording-upgrade
Browse files Browse the repository at this point in the history
Recoding functionality Improvements
  • Loading branch information
obed-vazquez authored Jan 24, 2023
2 parents a9d2e0e + 5894d04 commit c81a951
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 64 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -48,7 +48,7 @@ by including it in your POM](https://maven.pkg.github.com/whiteorganization/whit
<dependency>
<groupId>io.github.whiteorganization</groupId>
<artifactId>white-selenium-framework</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>
</dependency>
```
If used on a long-term heavy-use project we recommend:
Expand All @@ -61,7 +61,7 @@ If used on a long-term heavy-use project we recommend:
<dependency>
<groupId>io.github.whiteorganization</groupId>
<artifactId>white-selenium-framework</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>
<!-- You need to manually import this library due to lack of maintenance on White_SeleniumFramework -->
<exclusions>
<exclusion>
Expand Down Expand Up @@ -101,7 +101,7 @@ and import the dependency like this:
<dependency>
<groupId>com.github.WhiteOrganization</groupId>
<artifactId>White_SeleniumFramework</artifactId>
<version>white-selenium-framework-1.0.3</version>
<version>white-selenium-framework-1.1.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.whiteorganization</groupId>
<artifactId>white-selenium-framework</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>A Selenium Framework that will help to execute tests and custom scenarios faster and reduce the effort
to access many sections Selenium related.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<File> saveRecording(SeleniumJupiter seleniumJupiter, String displayName){
return saveRecording(seleniumJupiter, displayName, null, null);
}

@SneakyThrows
public static Optional<File> 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(":| ", "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2775,69 +2775,17 @@ public String take(){
//</editor-fold>

//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<File> 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<File> saveRecording(SeleniumJupiter seleniumJupiter, String displayName, String filePath, String fileName){
return RecordingUtils.saveRecording(seleniumJupiter, displayName, filePath, fileName);
}

//endregion Record

//<editor-fold defaultstate="collapsed" desc="WebExplorer in use">
Expand Down

0 comments on commit c81a951

Please sign in to comment.