Skip to content

Commit 5f7ded3

Browse files
bsideuprnorth
andauthored
Deprecate getTestHostIpAddress and getContainerIpAddress() (#5149)
* Deprecate `getTestHostIpAddress` * Update docs/modules/webdriver_containers.md Co-authored-by: Richard North <[email protected]> Co-authored-by: Richard North <[email protected]>
1 parent 6fada56 commit 5f7ded3

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

core/src/main/java/org/testcontainers/containers/Container.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ default SELF withClasspathResourceMapping(final String resourcePath, final Strin
365365
* from inside the container is not going to work, since the container has its own IP address.
366366
*
367367
* @return the IP address of the host machine
368+
* @deprecated use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)}
368369
*/
370+
@Deprecated
369371
String getTestHostIpAddress();
370372

371373
/**

core/src/main/java/org/testcontainers/containers/ContainerState.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public interface ContainerState {
4444
* Get the IP address that this container may be reached on (may not be the local machine).
4545
*
4646
* @return an IP address
47+
* @deprecated use {@link #getHost()}
4748
* @see #getHost()
4849
*/
4950
default String getContainerIpAddress() {

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ public String getDockerImageName() {
13321332
* {@inheritDoc}
13331333
*/
13341334
@Override
1335+
@Deprecated
13351336
public String getTestHostIpAddress() {
13361337
if (DockerMachineClient.instance().isInstalled()) {
13371338
try {

docs/modules/webdriver_containers.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ test methods:
3636
You can then use this driver instance like a regular WebDriver.
3737

3838
Note that, if you want to test a **web application running on the host machine** (the machine the JUnit tests are
39-
running on - which is quite likely), you'll need to replace any references to `localhost` with an IP address that the
40-
Docker container can reach. Use the `getTestHostIpAddress()` method, e.g.:
39+
running on - which is quite likely), you'll need to use [the host exposing](../features/networking.md#exposing-host-ports-to-the-container) feature of Testcontainers, e.g.:
4140
<!--codeinclude-->
4241
[Open Web Page](../../modules/selenium/src/test/java/org/testcontainers/junit/LocalServerWebDriverContainerTest.java) inside_block:getPage
4342
<!--/codeinclude-->

modules/selenium/src/test/java/org/testcontainers/junit/LocalServerWebDriverContainerTest.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import org.openqa.selenium.By;
88
import org.openqa.selenium.chrome.ChromeOptions;
99
import org.openqa.selenium.remote.RemoteWebDriver;
10+
import org.testcontainers.Testcontainers;
1011
import org.testcontainers.containers.BrowserWebDriverContainer;
11-
import org.testcontainers.utility.TestEnvironment;
1212

13-
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC_OSX;
1413
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
1514

1615
/**
@@ -20,18 +19,11 @@
2019
public class LocalServerWebDriverContainerTest {
2120

2221
@Rule
23-
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer().withCapabilities(new ChromeOptions());
22+
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
23+
.withAccessToHost(true)
24+
.withCapabilities(new ChromeOptions());
2425
private int localPort;
2526

26-
/**
27-
* The getTestHostIpAddress() method is only implemented for OS X running docker-machine. Skip JUnit execution elsewhere.
28-
*/
29-
@BeforeClass
30-
public static void checkOS() {
31-
Assume.assumeTrue("These tests are currently only applicable to OS X", IS_OS_MAC_OSX);
32-
Assume.assumeTrue("These tests are only applicable to docker machine", TestEnvironment.dockerIsDockerMachine());
33-
}
34-
3527
@Before
3628
public void setupLocalServer() throws Exception {
3729

@@ -55,8 +47,8 @@ public void testConnection() {
5547

5648
// Construct a URL that the browser container can access
5749
// getPage {
58-
String hostIpAddress = chrome.getTestHostIpAddress();
59-
driver.get("http://" + hostIpAddress + ":" + localPort);
50+
Testcontainers.exposeHostPorts(localPort);
51+
driver.get("http://host.testcontainers.internal:" + localPort);
6052
// }
6153

6254
String headingText = driver.findElement(By.cssSelector("h1")).getText().trim();

0 commit comments

Comments
 (0)