Skip to content

Commit

Permalink
Make recorder .flv videos scrollable (testcontainers#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
oussamabadr committed Dec 11, 2020
1 parent c3a8ca7 commit 553fc2d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.testcontainers.utility.DockerImageName;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -52,7 +53,7 @@ public VncRecordingContainer(@NonNull GenericContainer<?> targetContainer) {
* Create a sidekick container and attach it to another container. The VNC output of that container will be recorded.
*/
public VncRecordingContainer(@NonNull Network network, @NonNull String targetNetworkAlias) throws IllegalStateException {
super(DockerImageName.parse("testcontainers/vnc-recorder:1.1.0"));
super(DockerImageName.parse("testcontainers/vnc-recorder:1.2.0"));

this.targetNetworkAlias = targetNetworkAlias;
withNetwork(network);
Expand Down Expand Up @@ -87,19 +88,27 @@ protected void configure() {
);
}

@SneakyThrows
public void saveRecordingToFile(File file) {
try(InputStream inputStream = streamRecording()) {
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}

@SneakyThrows
public InputStream streamRecording() {
fixRecordDuration();

TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(
dockerClient.copyArchiveFromContainerCmd(getContainerId(), RECORDING_FILE_NAME).exec()
);
archiveInputStream.getNextEntry();
return archiveInputStream;
}

@SneakyThrows
public void saveRecordingToFile(File file) {
try(InputStream inputStream = streamRecording()) {
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
private void fixRecordDuration() throws IOException, InterruptedException {
String newFlvOutput = "/newScreen.flv";
execInContainer("ffmpeg" , "-i", RECORDING_FILE_NAME, "-vcodec", "libx264", newFlvOutput);
execInContainer("mv" , "-f", newFlvOutput, RECORDING_FILE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.DefaultRecordingFileFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
import org.testcontainers.lifecycle.TestDescription;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL;
import static org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING;

Expand Down Expand Up @@ -59,6 +65,52 @@ public String getFilesystemFriendlyName() {
assertEquals("Recorded file not found", 1, files.length);
}
}

@Test
public void recordingTestThatShouldHaveCorrectDuration() throws IOException {
final String flvFileTitle = "ChromeThatRecordsAllTests-recordingTestThatShouldBeRecordedAndRetained";
final String flvFileNameRegEx = "PASSED-" + flvFileTitle + ".*\\.flv";

try (
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, vncRecordingDirectory.getRoot())
.withRecordingFileFactory(new DefaultRecordingFileFactory())
) {
chrome.start();

doSimpleExplore(chrome);
chrome.afterTest(new TestDescription() {
@Override
public String getTestId() {
return getFilesystemFriendlyName();
}

@Override
public String getFilesystemFriendlyName() {
return flvFileTitle;
}
}, Optional.empty());

String recordedFile = vncRecordingDirectory.getRoot().listFiles(new PatternFilenameFilter(flvFileNameRegEx))[0].getCanonicalPath();

ToStringConsumer dockerLogConsumer = new ToStringConsumer();

try( GenericContainer container = new GenericContainer<>("jrottenberg/ffmpeg:3.2-alpine38") ) {
container.withStartupCheckStrategy(new OneShotStartupCheckStrategy())
.withFileSystemBind(recordedFile, "/tmp/chromeTestRecord.flv", BindMode.READ_WRITE )
.withCommand("-i" ,"/tmp/chromeTestRecord.flv")
.withLogConsumer(dockerLogConsumer)
.start();
} catch (RuntimeException ex) {
// Container has started successfully but an exception is thrown as we used OneShotStartupCheckStrategy
// But (for now) OneShotStartupCheckStrategy is useful to get Duration from container output
}

String dockerOutput = dockerLogConsumer.toUtf8String();
assertTrue("Duration is incorrect: ", dockerOutput.matches("[\\S\\s]*Duration: (?!00:00:00.00)[\\S\\s]*"));
}
}
}

public static class ChromeThatRecordsFailingTests {
Expand Down

0 comments on commit 553fc2d

Please sign in to comment.