基于Docker的WebdriverIO容器化应用测试配置求助
Let's fix your setup step by step so you can run docker-compose up --build and have your tests execute successfully against the containerized app.
Key Issue to Fix
In your current WebdriverIO test, you're using http://0.0.0.0:3000 to access the app—but within the Docker Compose network, containers can't reach each other via localhost or 0.0.0.0. Instead, you need to use the service name defined in docker-compose.yml (which is app) as the hostname.
Step 1: Update WebdriverIO Test URL
Change the URL in your test script to point to the app service's hostname and port:
it("check if submit button works", function(done) { // Use the Docker service name "app" instead of 0.0.0.0/localhost browser.url('http://app:3000'); var title = browser.getTitle(); browser.click('#submitButton'); console.log('App Title is: ' + title); browser.pause(3000); done(); // Don't forget to call done() to signal test completion! });
Note: I added done() at the end—this is critical for async test functions to let the runner know the test is finished.
Step 2: Adjust Docker Compose Configuration
Your docker-compose.yml needs small tweaks to ensure all containers communicate properly:
- The
testservice needs access to bothappandselenium(tests talk to Selenium, which in turn talks to the app) - We'll replace legacy
linkswithdepends_onfor clearer startup ordering - Optional: Add healthchecks to ensure tests don't run before the app is fully ready
Here's the updated docker-compose.yml:
version: '3' # Specify a modern compose version for better compatibility services: app: build: . command: "yarn start" ports: - "3000:3000" # Optional healthcheck to confirm app is ready (add if your app takes time to boot) healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000"] interval: 5s timeout: 5s retries: 5 selenium: image: 'selenium/standalone-chrome:3.11.0-californium' depends_on: - app log_driver: "none" test: build: . command: "yarn test --host selenium" depends_on: app: condition: service_healthy # Wait for app to be fully ready selenium: condition: service_started # Just need selenium to be running
Changes explained:
depends_onensures services start in the right order (app → selenium → tests)- The healthcheck for
appprevents tests from running before the app is responsive - Removed redundant
exposedirectives (ports already handle external/internal access)
Step 3: Verify Dockerfile (No Changes Needed)
Your existing Dockerfile is solid—it correctly builds the Node app and starts it:
FROM node:8.10.0 ADD . /app WORKDIR /app RUN yarn CMD ["yarn", "start"]
How to Run
Execute this command from your project root:
docker-compose up --build
This will:
- Build the app and test images from your Dockerfile
- Start the app container and wait for it to be healthy
- Spin up the Selenium Chrome container
- Run your WebdriverIO tests in the test container—tests will connect to Selenium, which accesses the app via
http://app:3000
内容的提问来源于stack exchange,提问作者Mad-D

