1

I am trying to integrate selenium into test pipeline of my spring boot application.

During test i run application (using spring boot test), after that testcontainers runs selenium. After that selenium container should run test.

The problem: selenium can’t connect to the host network.

  @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")

I tried several ways to solve such issue like adding network mode, use internal docker hostname.

During test execution I get a screenshot with “This site can’t be reached”

I can’t use getTestHostIpAdress() because I use cents.

So, my question is: how to set up testcontainers correctly to be able test web app running locally?

Artyom Karnov
  • 557
  • 9
  • 24

1 Answers1

4

See Exposing host ports to the container.

It allows you to expose your local port so that containers can access it on host.testcontainers.internal.

bsideup
  • 2,845
  • 17
  • 21
  • Sergei, Thank you. – Artyom Karnov Jan 27 '20 at 18:11
  • I have a similar setup and it works when Spring Boot tests are running directly on the laptop. But when running in GitLab CI/CD inside Docker-In-Docker and inside a Maven docker container, then the app in TestContainer is not able to "see" my backend through "host.testcontainers.internal". I suspect that "localhost" in this setting will be the DIND container. Have anyone been able to solve this? – Cominvent Oct 11 '21 at 14:02