Docker with phantomjs container and Hound testing

Hi everyone!

I’m trying to run my integration test suite with Docker. I’m using Hound. And I have configured it this way:

docker-compose.yml

version: '2'
services:
  db:
    image: postgres:9.5
  phantomjs:
    image: wernight/phantomjs:latest
    command: phantomjs --wd
  web:
    build: .
    command: mix phx.server
    volumes:
      - .:/app
    ports:
      - "4000:4000"
      - "4001:4001"
    depends_on:
      - db
    links:
      - phantomjs

test.exs:

config :app, App.Endpoint,
  http: [port: 4001],
  server: true

[...]

config :hound,
  driver: "phantomjs",
  host: "http://phantomjs",
  app_host: "http://web",
  app_port: 4001

Then I’ve this simple test, in order to check that it works:

test_hound.exs

defmodule App.TestHound do
  use App.IntegrationCase

  defp login_index do
    session_url(App.Endpoint, :new)
  end

  test "dummy test" do
    login_index() |> navigate_to()

    assert element?(:class, "site") == true
  end
end

Then I run the test:

$ docker-compose run web env MIX_ENV=test mix test test/integration/test_hound.exs

but it didn’t pass:

Assertion with == failed
 code:  element?(:class, "site") == true
 left:  false
 right: true

I inspected with IO.inspect current_url() after execute the login_index() |> navigate_to() and it returns about:blank.
So the problem, I think, it’s that the test can’t connect properly with the phantomjs container in order to visit the web. It’s like it doesn’t launch the server. I inspected the login_index() too, and it returns the login URL.

My integration case, look like this:

integration_case.ex

defmodule App.IntegrationCase do
  use Hound.Helpers
  use ExUnit.CaseTemplate

  using do
    quote do
      use Hound.Helpers

      import Ecto
      import Ecto.Query
      import App.Router.Helpers
      import App.Factory
      import App.IntegrationCase

      alias App.Repo

      # The default endpoint for testing
      @endpoint App.Endpoint

      hound_session()
    end
  end

  setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(App.Repo)

    unless tags[:async] do
    Ecto.Adapters.SQL.Sandbox.mode(App.Repo, {:shared, self()})
    end

    :ok
  end
end

I’ve tried with different ports in the hound config, just in case, but I can’t make it work.

Any help would be appreciated. Thanks in advance! :blush:

3 Likes

Rubber ducking debugging here! :smile:

It’s a little bit embarrassing to me. But the error was in the test.exs file. In the Endpoint configuration, I needed to add url: [host: "web"]. In order to change the default localhost endpoint to point to the web container:

config :hcmex, Hcmex.Endpoint,
  http: [port: 4001],
  url: [host: "web"],
  server: true 

Now if I run the tests (there are some IO.inspects):

"http://web:4001/sign_in"
{:ok,
%{browser: "phantomjs", driver: "phantomjs", host: "http://phantomjs",
  path_prefix: nil, port: 8910}}
{:ok, %{host: "http://web", port: 4001}}
.

Finished in 2.4 seconds
1 test, 0 failures

Randomized with seed 427281

And It works! I feel a little bit dumb right now. But I hope this post can help other people.

Cheers!

1 Like

This helped a lot – thank you!

Though I’m finding I get CSRF errors when I post a form with a similar configuration – I can verify that the csrf token is included in the form, and if I run the test server manually I can post said form successfully, but hound/phantomjs does not like it. Any ideas?