spring-boot - 如何使用测试容器将 selenium 容器连接到 localhost 网络?

标签 spring-boot selenium docker kotlin testcontainers

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

在测试期间,我运行应用程序(使用 spring boot 测试),之后 testcontainers 运行 selenium。之后 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() 因为我使用美分。

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

最佳答案

Exposing host ports to the container .

它允许您公开本地端口,以便容器可以在 host.testcontainers.internal 上访问它.

关于spring-boot - 如何使用测试容器将 selenium 容器连接到 localhost 网络?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59904413/

相关文章:

java - 通过 POST 从 React App 发送数组到 Spring Boot

spring-boot - Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup

java - spring boot多个模块如何继承application.properties

testing - 我可以使用 MyEclipse IDE 而不是 Eclipse IDE(Eclipse for Testers)来使用 Selenium 进行测试吗

javascript - 在 Selenium IDE 中执行 HTTP POST

Spring Boot Taglibs

java - Selenium WebDriver FluentWait

docker - https://localhost:8080/console:Hasura控制台不适用于Chrome浏览器

docker - docker 容器中的 microk8s

python - 无法命中本地运行的 docker 容器