testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.04k stars 1.65k forks source link

Length of the recording is too long #4380

Open haevnEU opened 3 years ago

haevnEU commented 3 years ago

We're using testcontainers for a couple month now for our test environment, but since the beginning we have a problem that the recordings take roughly double the time as the actual test. At the first moment I thought my implementation was not correct, and I tried the examples with the exact same result, therefore it seems there's a problem somewhere else. We're using the BrowserWebDriverContainer and the VncRecordingContainer with chrome, selenium and junit5. Connecting via a VNC viewer I have noticed that the browser quits correctly, but the container stays alive for a long period. My guess is that the recording container works fine and the browser container has some process which blocks a shutdown The following example should execute three tests inside the container, each of the test visits google with a specific search request. The actual test footage ends around 1min and 43sec, but the entire recording is 3min and 13sec long.

https://user-images.githubusercontent.com/28145109/129879769-8f76bef7-69b3-4a58-a7ee-7d1618cbdd95.mp4

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;

@Testcontainers
public class MainTest {

    private static final int WAIT_TIME = 2000;
    @Container
    private final static BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>().withCapabilities(new ChromeOptions())
           .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, new File("./target/"))
           .withNetwork(Network.SHARED).withNetworkAliases("vnchost");
    public static RemoteWebDriver driver;

    @BeforeAll
    static void setup(){
        driver = chrome.getWebDriver();
        driver.manage().window().maximize();
    }

    @AfterAll
    static void tearDown(){
        driver.quit();
    }

    @Test
    @DisplayName("Hunde Test")
    public void run3() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Hunde Test</h1></html>", 5);
        String[] trees = {"Labrador", "Labrador Retriever", "Golden Retriever", "Yourkshire", "West Highland White Terrier", "Pudel", "Rotweiler", "Dogge", "Windhund", "Husky"};
        for (String tree : trees) {
            driver.get("https://www.google.de/search?q=" + tree);
            Thread.sleep(WAIT_TIME);
         }
    }

    @Test
    @DisplayName("Baum Test")
    public void run2() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Baum Test</h1></html>", 5);
        String[] trees = {"Eiche", "Ahorn", "Kastanie", "Apfelbaum", "Birke", "Tanne", "Buche", "Fichte", "Erle"};
        for (String tree : trees) {
            driver.get("https://www.google.de/search?q=" + tree);
            Thread.sleep(WAIT_TIME);
        }
    }

    @Test
    @DisplayName("Zahlen Test")
    public void run() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Zahlen Test</h1></html>", 5);

        for (int i = 0; i < 10; i++) {
            driver.get("https://www.google.de/search?q=" + i);
            Thread.sleep(WAIT_TIME);
        }
    }

    private void showLocalHtml(String html, long seconds){
        try {
            driver.get("data:text/html;charset=utf-8," + html);
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
chrisgleissner commented 3 years ago

We have a similar issue. Latest versions of Testcontainers, Docker, Chrome, Ubuntu 20.04 and Junit 5.

In our case, the Chrome interaction of each test takes about 20s. However, writing the MP4 video of the test takes another 10s. (It typically takes 30 percent of the test's runtime.) Forcefully closing the browser in the Selenium container after test completion (but before the Vnc container's recording write starts - basically just as you are doing in your code sample above) makes no difference in this 10s post test delay.

I had attributed this to being caused by the slowness of writing the recording, possibly its MP4 encoding, but your bug report indicates something else may be going on.

Update: In our latest test, the test-runtime is 45s and the post-test recording write-out takes 40s. The resulting MP4 recording weighs 2MB. I doubt that this is just caused by encoding CPU overhead and it most definitely isn't IO.

trudoliubov commented 2 years ago

Hello! Why the name video file is .5Bengine.3Ajunit-jupiter.5D.2F.5B ? I've a similiar problem.

We're using testcontainers for a couple month now for our test environment, but since the beginning we have a problem that the recordings take roughly double the time as the actual test. At the first moment I thought my implementation was not correct, and I tried the examples with the exact same result, therefore it seems there's a problem somewhere else. We're using the BrowserWebDriverContainer and the VncRecordingContainer with chrome, selenium and junit5. Connecting via a VNC viewer I have noticed that the browser quits correctly, but the container stays alive for a long period. My guess is that the recording container works fine and the browser container has some process which blocks a shutdown The following example should execute three tests inside the container, each of the test visits google with a specific search request. The actual test footage ends around 1min and 43sec, but the entire recording is 3min and 13sec long.

PASSED-.5Bengine.3Ajunit-jupiter.5D.2F.5Bclass.3AMainTest.5D-20210818-120140.mp4

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;

@Testcontainers
public class MainTest {

    private static final int WAIT_TIME = 2000;
    @Container
    private final static BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>().withCapabilities(new ChromeOptions())
           .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, new File("./target/"))
           .withNetwork(Network.SHARED).withNetworkAliases("vnchost");
    public static RemoteWebDriver driver;

    @BeforeAll
    static void setup(){
        driver = chrome.getWebDriver();
        driver.manage().window().maximize();
    }

    @AfterAll
    static void tearDown(){
        driver.quit();
    }

    @Test
    @DisplayName("Hunde Test")
    public void run3() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Hunde Test</h1></html>", 5);
        String[] trees = {"Labrador", "Labrador Retriever", "Golden Retriever", "Yourkshire", "West Highland White Terrier", "Pudel", "Rotweiler", "Dogge", "Windhund", "Husky"};
        for (String tree : trees) {
            driver.get("https://www.google.de/search?q=" + tree);
            Thread.sleep(WAIT_TIME);
         }
    }

    @Test
    @DisplayName("Baum Test")
    public void run2() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Baum Test</h1></html>", 5);
        String[] trees = {"Eiche", "Ahorn", "Kastanie", "Apfelbaum", "Birke", "Tanne", "Buche", "Fichte", "Erle"};
        for (String tree : trees) {
            driver.get("https://www.google.de/search?q=" + tree);
            Thread.sleep(WAIT_TIME);
        }
    }

    @Test
    @DisplayName("Zahlen Test")
    public void run() throws InterruptedException {
        showLocalHtml("<!DOCTYPE html><html><h1>Zahlen Test</h1></html>", 5);

        for (int i = 0; i < 10; i++) {
            driver.get("https://www.google.de/search?q=" + i);
            Thread.sleep(WAIT_TIME);
        }
    }

    private void showLocalHtml(String html, long seconds){
        try {
            driver.get("data:text/html;charset=utf-8," + html);
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}