Skip to content

Commit

Permalink
Deprecate getTestHostIpAddress and getContainerIpAddress() (#5149)
Browse files Browse the repository at this point in the history
* Deprecate `getTestHostIpAddress`

* Update docs/modules/webdriver_containers.md

Co-authored-by: Richard North <rich.north@gmail.com>

Co-authored-by: Richard North <rich.north@gmail.com>
  • Loading branch information
bsideup and rnorth committed Mar 8, 2022
1 parent 6fada56 commit 5f7ded3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ default SELF withClasspathResourceMapping(final String resourcePath, final Strin
* from inside the container is not going to work, since the container has its own IP address.
*
* @return the IP address of the host machine
* @deprecated use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)}
*/
@Deprecated
String getTestHostIpAddress();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public interface ContainerState {
* Get the IP address that this container may be reached on (may not be the local machine).
*
* @return an IP address
* @deprecated use {@link #getHost()}
* @see #getHost()
*/
default String getContainerIpAddress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ public String getDockerImageName() {
* {@inheritDoc}
*/
@Override
@Deprecated
public String getTestHostIpAddress() {
if (DockerMachineClient.instance().isInstalled()) {
try {
Expand Down
3 changes: 1 addition & 2 deletions docs/modules/webdriver_containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ test methods:
You can then use this driver instance like a regular WebDriver.

Note that, if you want to test a **web application running on the host machine** (the machine the JUnit tests are
running on - which is quite likely), you'll need to replace any references to `localhost` with an IP address that the
Docker container can reach. Use the `getTestHostIpAddress()` method, e.g.:
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.:
<!--codeinclude-->
[Open Web Page](../../modules/selenium/src/test/java/org/testcontainers/junit/LocalServerWebDriverContainerTest.java) inside_block:getPage
<!--/codeinclude-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.utility.TestEnvironment;

import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC_OSX;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
Expand All @@ -20,18 +19,11 @@
public class LocalServerWebDriverContainerTest {

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer().withCapabilities(new ChromeOptions());
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withAccessToHost(true)
.withCapabilities(new ChromeOptions());
private int localPort;

/**
* The getTestHostIpAddress() method is only implemented for OS X running docker-machine. Skip JUnit execution elsewhere.
*/
@BeforeClass
public static void checkOS() {
Assume.assumeTrue("These tests are currently only applicable to OS X", IS_OS_MAC_OSX);
Assume.assumeTrue("These tests are only applicable to docker machine", TestEnvironment.dockerIsDockerMachine());
}

@Before
public void setupLocalServer() throws Exception {

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

// Construct a URL that the browser container can access
// getPage {
String hostIpAddress = chrome.getTestHostIpAddress();
driver.get("http://" + hostIpAddress + ":" + localPort);
Testcontainers.exposeHostPorts(localPort);
driver.get("http://host.testcontainers.internal:" + localPort);
// }

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

0 comments on commit 5f7ded3

Please sign in to comment.