supercorn

supercorn

Problems with remote webdriver and hound testing

I’m having trouble setting up Phoenix 1.3, Hound 1.04 and docker. The short of it is that I can curl pages from my test endpoint if I use “http://localhost:4001” but not if I use a hostname like “http://web:4001”. Since my webdriver is not on localhost, I can’t use Hound’s navigate_to method to load pages from my test server.

I’ve seen this post by enoliglesias from late July of this year. He says it’s all working for him. To the best of my knowledge I’m doing the same thing, but it doesn’t work.

Here’s my docker-compose.yml:

version: '3'
services:
  web:
    image: mafs/client-compass-web
    working_dir: /app
    command: bash -c "elixir --sname web-server -S mix phx.server"
    env_file: .env
    volumes:
      - ./:/app
    networks:
      main:
        aliases:
          - web
    ports:
      - "4000:4000"
      - "4001:4001"

  webdriver:
    image: yukinying/chrome-headless-browser-selenium
    networks:
      main:
        aliases:
          - webdriver
    cap_add:
      - SYS_ADMIN
    ports:
      - 4444:4444

networks:
  main:

I’m using v3 which allows me to specify a network and host aliases. This allows me to have a circular reference between the webdriver container and the web container. With my dev server up and running I can get a shell on my webdriver container and fetch pages from the dev server like this:

headless@43ecff08a135:/opt/google/chrome$ google-chrome --headless --disable-gpu --dump-dom http://web:4000/

And the expected page markup is shown.

Here’s my config/test.exs

config :moble_ca, MobleCaWeb.Endpoint,
  http: [port: 4001, ip: {0,0,0,0}],
  check_origin: false,
  url: [host: "web"],
  server: true

config :hound, driver: "selenium", browser: "chrome", host: "http://webdriver", port: 4444, app_host: "http://web", app_port: 4001

From what I understand, the url option for the Endpoint is so that the route helpers will generate the correct urls with an external hostname. The http option is passed to Cowboy and tells him what port and ip to listen on. I’ve tried here to specify that I want him bound to the global interface: 0.0.0.0.

I’m explicitly configuring Hound to set the app host to the remote hostname as seen from the webdriver host. I’m not exactly sure what good this does. If the Endpoint is configured to generate urls on the remote host, it seems like that would be good enough. Maybe this would allow me to do things like navigate_to("/") and still get the right hostname.

Here’s my feature case:

defmodule MobleCa.Web.FeatureCase do
  use ExUnit.CaseTemplate
  use Hound.Helpers

  using do
    quote do
      alias MobleCa.Repo
      alias MobleCa.Web.Page
      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import MobleCa.Factory
      import MobleCa.Web.FeatureCase
      import MobleCaWeb.Router.Helpers

      # The default endpoint for testing
      @endpoint MobleCaWeb.Endpoint

      hound_session()
    end
  end

  setup tags do
    use Hound.Helpers
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(MobleCa.Repo)

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

    metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(MobleCa.Repo, self())
    {:ok, metadata: metadata}
  end

end

And finally, here’s my test method:

defmodule MobleCa.Web.SigninFeature do
  use MobleCa.Web.FeatureCase
  use Hound.Helpers

  test "renders a signin form" do
    root_url = session_url(@endpoint, :login)
    System.cmd "curl", ["http://localhost:4001"], into: IO.stream(:stdio, :line)
    IO.inspect root_url
    System.cmd "curl", [root_url], into: IO.stream(:stdio, :line)
    root_url="http://web:4000/"
    navigate_to(root_url)
    IO.puts page_source()
    IO.puts current_url()
    assert page_title() == "Google"
  end
end

What I can see from the output is that the first curl against localhost is able to connect to the test server and fetch the correct page content. The second curl against “http://web:4001” gets a connection refused. The navigate_to returns a somewhat blank document:

<html><head></head><body></body></html>

Note that if I navigate_to("http://google.com") then the whole test will pass. I’m pretty sure that Hound is able to communicate properly with the webdriver. It can load an external url just fine.

If I pause the script with an IEx.pry and then try to fetch the page from the command line of my webdriver container it does not work. It returns the same empty-ish document.

I’m pretty much up against the wall here and don’t know what else to try. Here’s the summary of what I think I know:

  • I’m explicitly binding the cowboy server for the test endpoint on the global interface, 0.0.0.0
  • I’m explicitly telling the Endpoint to generate urls with the remote hostname
  • I’m telling hound the app host is remote on http://web
  • I’m able to manually fetch pages from the webdriver container to my development server.
  • I’m able to curl pages from my localhost test server during tests.
  • I’m able to use Hound to navigate to external hosts and test against them
  • I’m also able use Hound to navigate to my development server and test against it

and most frustratingly:

  • I’m unable to use Hound with the remote hostname of my test server

Where Next?

Popular in Questions Top

earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement