如何使用测试容器将硒容器连接到本地主机网络?

How to connect selenium container to localhost network with test containers?

我正在尝试将 selenium 集成到我的 spring 启动应用程序的测试管道中。

在测试 运行 应用程序期间(使用 spring 启动测试),之后测试容器 运行s selenium。之后硒容器应该 运行 测试。

问题:selenium 无法连接到主机网络。

  @SpringBootTest(
    classes = [StatisticServer::class],
    webEnvironment = RANDOM_PORTq)
  @ContextConfiguration(initializers =     [DbTextContextInitializer::class])
  @ActiveProfiles("local", "no-security")
  @Sql(executionPhase = AFTER_TEST_METHOD, scripts = ["classpath:/sql/clear_db.sql"])
class SeleniumParentTest {

@LocalServerPort
protected var port=0

class KWebDriverContainer(dockerImage: String) : BrowserWebDriverContainer<KWebDriverContainer>(dockerImage)

var chrome: BrowserWebDriverContainer<*> = KWebDriverContainer("selenium/standalone-chrome-debug:latest")
        .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, File("build"))
        .withCapabilities(ChromeOptions().setAcceptInsecureCerts(true))
        .withEnv("http_host", "http://192.168.5.46:8080")
        .withNetworkMode("host")


@BeforeEach
fun setUp() {
    chrome.start()
    val driver = chrome.getWebDriver()
    WebDriverRunner.setWebDriver(driver)
}

@AfterEach
fun tearDown() {
    WebDriverRunner.closeWebDriver();
}

@Test
fun shouldSuccessfullyPassThisTestUsingTheRemoteDriver() {
    open("https://127.0.0.1:9200/swagger-ui.html");
    // open("https://host.docker.internal:$port/swagger-ui.html");
    `$`(By.className("info")).value
    val link = `$`("#res .g", 0).find("a")

我尝试了多种方法来解决此类问题,例如添加网络模式、使用内部 docker 主机名。

在测试执行期间,我得到了一张显示“无法访问此站点”的屏幕截图

我不能使用 getTestHostIpAdress(),因为我使用了 cents。

所以,我的问题是:如何正确设置测试容器才能在本地测试 Web 应用 运行?

Exposing host ports to the container

它允许您公开您的 local 端口,以便容器可以在 host.testcontainers.internal.

上访问它