Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate getTestHostIpAddress and getContainerIpAddress #5149

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) feature of Testcontainers, e.g.:
bsideup marked this conversation as resolved.
Show resolved Hide resolved
<!--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