Best Testcontainers-java code snippet using org.testcontainers.containers.BrowserWebDriverContainer.withRecordingFileFactory
Source:ChromeRecordingWebDriverContainerTest.java
...38 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()39 .withCapabilities(new ChromeOptions())40 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, target)41 // }42 .withRecordingFileFactory(new DefaultRecordingFileFactory())43 .withNetwork(NETWORK)44 ) {45 File[] files = runSimpleExploreInContainer(chrome, "PASSED-.*\\.flv");46 assertThat(files).as("Recorded file found").hasSize(1);47 }48 }49 private File[] runSimpleExploreInContainer(BrowserWebDriverContainer<?> container, String fileNamePattern)50 throws InterruptedException {51 container.start();52 TimeUnit.MILLISECONDS.sleep(MINIMUM_VIDEO_DURATION_MILLISECONDS);53 doSimpleExplore(container);54 container.afterTest(55 new TestDescription() {56 @Override57 public String getTestId() {58 return getFilesystemFriendlyName();59 }60 @Override61 public String getFilesystemFriendlyName() {62 return "ChromeThatRecordsAllTests-recordingTestThatShouldBeRecordedAndRetained";63 }64 },65 Optional.empty()66 );67 return vncRecordingDirectory.toFile().listFiles(new PatternFilenameFilter(fileNamePattern));68 }69 @Test70 public void recordingTestShouldHaveFlvExtension() throws InterruptedException {71 File target = vncRecordingDirectory.toFile();72 System.out.println("Record folder: " + target.getAbsolutePath());73 try (74 // recordFlv {75 // Set (explicitly) FLV format for recorded video:76 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()77 .withCapabilities(new ChromeOptions())78 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, target, VncRecordingFormat.FLV)79 // }80 .withRecordingFileFactory(new DefaultRecordingFileFactory())81 .withNetwork(NETWORK)82 ) {83 File[] files = runSimpleExploreInContainer(chrome, "PASSED-.*\\.flv");84 assertThat(files).as("Recorded file found").hasSize(1);85 }86 }87 @Test88 public void recordingTestShouldHaveMp4Extension() throws InterruptedException {89 File target = vncRecordingDirectory.toFile();90 System.out.println("Record folder: " + target.getAbsolutePath());91 try (92 // recordMp4 {93 // Set MP4 format for recorded video:94 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()95 .withCapabilities(new ChromeOptions())96 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, target, VncRecordingFormat.MP4)97 // }98 .withRecordingFileFactory(new DefaultRecordingFileFactory())99 .withNetwork(NETWORK)100 ) {101 File[] files = runSimpleExploreInContainer(chrome, "PASSED-.*\\.mp4");102 assertThat(files).as("Recorded file found").hasSize(1);103 }104 }105 @Test106 public void recordingTestThatShouldHaveCorrectDuration() throws IOException, InterruptedException {107 MountableFile mountableFile;108 try (109 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()110 .withCapabilities(new ChromeOptions())111 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, vncRecordingDirectory.toFile())112 .withRecordingFileFactory(new DefaultRecordingFileFactory())113 .withNetwork(NETWORK)114 ) {115 File[] recordedFiles = runSimpleExploreInContainer(chrome, "PASSED-.*\\.flv");116 mountableFile = MountableFile.forHostPath(recordedFiles[0].getCanonicalPath());117 }118 try (119 GenericContainer<?> container = new GenericContainer<>(120 DockerImageName.parse("testcontainers/vnc-recorder:1.2.0")121 )122 ) {123 String recordFileContainerPath = "/tmp/chromeTestRecord.flv";124 container125 .withCopyFileToContainer(mountableFile, recordFileContainerPath)126 .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withEntrypoint("ffmpeg"))127 .withCommand("-i", recordFileContainerPath, "-f", "null", "-")128 .waitingFor(129 new LogMessageWaitStrategy()130 .withRegEx(".*Duration.*")131 .withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS))132 )133 .start();134 String ffmpegOutput = container.getLogs();135 assertThat(ffmpegOutput)136 .as("Duration starts with 00:")137 .contains("Duration: 00:")138 .doesNotContain("Duration: 00:00:00.00");139 }140 }141 }142 @Nested143 class ChromeThatRecordsFailingTests {144 @TempDir145 public Path vncRecordingDirectory;146 @Test147 public void recordingTestThatShouldBeRecordedButNotPersisted() {148 try (149 // withRecordingFileFactory {150 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()151 // }152 .withCapabilities(new ChromeOptions())153 // withRecordingFileFactory {154 .withRecordingFileFactory(new CustomRecordingFileFactory())155 // }156 .withNetwork(NETWORK)157 ) {158 chrome.start();159 doSimpleExplore(chrome);160 }161 }162 @Test163 public void recordingTestThatShouldBeRecordedAndRetained() throws InterruptedException {164 File target = vncRecordingDirectory.toFile();165 System.out.println("Record folder: " + target.getAbsolutePath());166 try (167 // recordFailing {168 // or if you only want videos for test failures:169 BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()170 .withCapabilities(new ChromeOptions())171 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING, target)172 // }173 .withRecordingFileFactory(new DefaultRecordingFileFactory())174 .withNetwork(NETWORK)175 ) {176 chrome.start();177 TimeUnit.MILLISECONDS.sleep(MINIMUM_VIDEO_DURATION_MILLISECONDS);178 doSimpleExplore(chrome);179 chrome.afterTest(180 new TestDescription() {181 @Override182 public String getTestId() {183 return getFilesystemFriendlyName();184 }185 @Override186 public String getFilesystemFriendlyName() {187 return "ChromeThatRecordsFailingTests-recordingTestThatShouldBeRecordedAndRetained";...
Source:CardAMTest.java
...44 new BrowserWebDriverContainer<>()45 .withNetwork(network)46 .withCapabilities(new ChromeOptions())47 .withRecordingMode(RECORD_ALL, new File("./build/"))48 .withRecordingFileFactory(new DefaultRecordingFileFactory());49/* @Rule50 public BrowserWebDriverContainer firefox =51 new BrowserWebDriverContainer()52 .withCapabilities(new FirefoxOptions())53 .withRecordingMode(RECORD_ALL, new File("./build/"))54 .withRecordingFileFactory(new DefaultRecordingFileFactory())*/;55 @Test56 public void customImageTest() throws InterruptedException, IOException {57 RemoteWebDriver driver = chrome.getWebDriver();58 driver.get("http://my-server/");59 File screenshot = driver.getScreenshotAs(OutputType.FILE);60 FileUtils.copyFile(screenshot, new File("./build/screenshots/" + screenshot.getName()));61/* String title = driver.findElement(By.id("title")).getText();62 assertEquals("Agnieszka Miller, tester automatyzujacy, tel. 501 000 000", title);*/63 }64}...
withRecordingFileFactory
Using AI Code Generation
1package com.testcontainers.demo;2import org.junit.After;3import org.junit.Before;4import org.junit.Test;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeOptions;9import org.openqa.selenium.remote.RemoteWebDriver;10import org.openqa.selenium.support.ui.ExpectedConditions;11import org.openqa.selenium.support.ui.WebDriverWait;12import org.testcontainers.containers.BrowserWebDriverContainer;13import org.testcontainers.containers.Network;14import org.testcontainers.containers.VncRecordingContainer;15import org.testcontainers.containers.wait.strategy.Wait;16import java.io.File;17import java.util.concurrent.TimeUnit;18public class DemoTest {
withRecordingFileFactory
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeOptions;5import org.openqa.selenium.remote.RemoteWebDriver;6import org.testcontainers.containers.BrowserWebDriverContainer;7import org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingFormat;8import org.testcontainers.containers.output.OutputFrame;9import org.testcontainers.containers.output.ToStringConsumer;10import org.testcontainers.containers.output.WaitingConsumer;11import org.testcontainers.utility.MountableFile;12import java.io.File;13import java.nio.file.Path;14import java.nio.file.Paths;15import java.util.function.Consumer;16import static org.junit.jupiter.api.Assertions.assertEquals;17import static org.junit.jupiter.api.Assertions.assertTrue;18{19 public void testRecordingFileFactory() throws Exception20 {21 BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()22 .withCapabilities(new ChromeOptions());23 chrome.start();24 RemoteWebDriver driver = chrome.getWebDriver();25 WebElement searchBox = driver.findElement(By.name("q"));26 searchBox.sendKeys("Testcontainers");27 searchBox.submit();28 assertTrue(driver.getTitle().startsWith("Testcontainers"));29 chrome.stop();30 }31}32import org.junit.jupiter.api.Test;33import org.openqa.selenium.By;34import org.openqa.selenium.WebElement;35import org.openqa.selenium.chrome.ChromeOptions;36import org.openqa.selenium.remote.RemoteWebDriver;37import org.testcontainers.containers.BrowserWebDriverContainer;38import org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingFormat;39import org.testcontainers.containers.output.OutputFrame;40import org.testcontainers.containers.output.ToStringConsumer;41import org.testcontainers.containers.output.WaitingConsumer;42import org.testcontainers.utility.MountableFile;43import java.io.File;44import java
withRecordingFileFactory
Using AI Code Generation
1public class NewTest {2 public void test() {3 File recordingFile = new File("target/test.mp4");4 recordingFile.getParentFile().mkdirs();5 recordingFile.delete();6 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()7 .withRecordingFileFactory(new SingleFileRecordingFileFactory(recordingFile))) {8 chrome.start();9 WebDriver driver = chrome.getWebDriver();10 }11 }12}13public class NewTest {14 public void test() {15 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()16 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, new File("target"))) {17 chrome.start();18 WebDriver driver = chrome.getWebDriver();19 }20 }21}22public class NewTest {23 public void test() {24 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()25 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.SKIP, new File("target"))) {26 chrome.start();27 WebDriver driver = chrome.getWebDriver();28 }29 }30}31public class NewTest {32 public void test() {33 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()34 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.SKIP, null)) {35 chrome.start();36 WebDriver driver = chrome.getWebDriver();37 }38 }39}40public class NewTest {41 public void test() {42 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()43 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING, new File("target"))) {44 chrome.start();
withRecordingFileFactory
Using AI Code Generation
1import org.junit.Test;2import org.openqa.selenium.By;3import org.openqa.selenium.Dimension;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeOptions;6import org.testcontainers.containers.BrowserWebDriverContainer;7import org.testcontainers.containers.output.OutputFrame;8import org.testcontainers.containers.output.ToStringConsumer;9import java.io.File;10import java.io.IOException;11import java.nio.file.Path;12import java.util.concurrent.TimeUnit;13public class TestVideoRecording {14 public void testVideoRecording() throws IOException {15 Path recordingPath = new File("target/recordings").toPath();16 try (BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()17 .withCapabilities(new ChromeOptions())18 .withRecordingFileFactory(recordingPath)19 ) {20 chrome.start();21 WebElement searchInput = chrome.getWebDriver().findElement(By.name("q"));22 searchInput.sendKeys("testcontainers.org");23 searchInput.submit();24 chrome.getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);25 chrome.stop();26 }27 }28}29import org.junit.Test;30import org.openqa.selenium.By;31import org.openqa.selenium.Dimension;32import org.openqa.selenium.WebElement;33import org.openqa.selenium.chrome.ChromeOptions;34import org.testcontainers.containers.BrowserWebDriverContainer;35import org.testcontainers.containers.output.OutputFrame;36import org.testcontainers.containers.output.ToStringConsumer;37import java.io.File;38import java.io.IOException;39import java.nio.file.Path;40import java.util.concurrent.TimeUnit;
withRecordingFileFactory
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeOptions;5import org.testcontainers.containers.BrowserWebDriverContainer;6import org.testcontainers.containers.output.OutputFrame;7import org.testcontainers.containers.output.ToStringConsumer;8import org.testcontainers.containers.output.WaitingConsumer;9import org.testcontainers.utility.MountableFile;10import java.io.IOException;11import java.nio.file.Files;12import java.nio.file.Path;13import java.nio.file.Paths;14import java.time.Duration;15import java.util.concurrent.TimeUnit;16public class RecordVideoFile {17 public void testRecordVideoFile() throws IOException {18 String videoFileName = "test.mp4";19 Path videoFilePath = Paths.get(videoFileName);20 String dockerImageName = "selenoid/video-recorder:latest-release";21 Path dockerImagePath = Paths.get(dockerImageName);22 MountableFile dockerImageFile = MountableFile.forHostPath(dockerImagePath);23 String dockerImageName2 = "selenoid/vnc:chrome_88.0";24 Path dockerImagePath2 = Paths.get(dockerImageName2);25 MountableFile dockerImageFile2 = MountableFile.forHostPath(dockerImagePath2);26 ChromeOptions chromeOptions = new ChromeOptions();27 chromeOptions.setExperimentalOption("w3c", false);28 WaitingConsumer waitingConsumer = new WaitingConsumer();29 ToStringConsumer toStringConsumer = new ToStringConsumer();30 BrowserWebDriverContainer browserWebDriverContainer = new BrowserWebDriverContainer(dockerImageFile2)31 .withRecordingFileFactory(containerPath -> {32 ToStringConsumer consumer = new ToStringConsumer();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!