0

I started a new project and configured it like so:

mix new example
cd example

I emptied ´lib/example.ex´ and placed the following code there:

Application.start :hound

defmodule Example do
  use Hound.Helpers

  def run do
    Hound.start_session

    navigate_to "http://akash.im"
    IO.inspect page_title()

    # Automatically invoked if the session owner process crashes
    Hound.end_session
  end
end

Example.run

This is the sample code provided at https://github.com/HashNuke/hound/blob/master/notes/simple-browser-automation.md

Then I installed Selenium server via brew install selenium-server-standalone (I'm on MacOS), started it via brew services start selenium-server-standalone and added config :hound, driver: "selenium" to config/config.exs

I added Application.ensure_all_started(:hound) as the first line of test/test_helper.exs.

Finally, I added {:hound, "~> 1.0"} to mix.exs and ran mix test. That is when I get the following compilation error:

localhost:example alex$ mix test
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> jason
Compiling 8 files (.ex)
Generated jason app
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> hound
Compiling 37 files (.ex)
Generated hound app
==> example
Compiling 1 file (.ex)

== Compilation error in file lib/example.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup(Hound.SessionServer, #PID<0.592.0>)
    (hound) lib/hound/session_server.ex:19: Hound.SessionServer.current_session_id/1
    (hound) lib/hound/session_server.ex:13: Hound.SessionServer.session_for_pid/2
    lib/example.ex:7: Example.run/0
localhost:example alex$ mix test
Compiling 1 file (.ex)

== Compilation error in file lib/example.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup(Hound.SessionServer, #PID<0.160.0>)
    (hound) lib/hound/session_server.ex:19: Hound.SessionServer.current_session_id/1
    (hound) lib/hound/session_server.ex:13: Hound.SessionServer.session_for_pid/2
    lib/example.ex:7: Example.run/0

Am I forgetting a step somewhere or configuring things incorrectly? Any help immensely appreciated, thanks!

Alex V
  • 3,416
  • 2
  • 33
  • 52
  • I thought I'd report back to you that I can no longer get anything to work with Hound. Now, I always get the error: `[error] GenServer Hound.SessionServer terminating ** (RuntimeError) could not create a new session: timeout, check webdriver is running`. I've tried reinstalling `selenium-server-standalone`, deleting my elixir project and recreating it, but no luck. Now, the examples that I got to work in my answer all produce the same timeout error. – 7stud Jul 04 '19 at 03:12
  • Ah hah! I quit Safari, then I relaunched Safari, rechecked `Develop/Allow Remote Automation` in the Safari menu bar, and then my Hound test worked again. – 7stud Jul 04 '19 at 03:19

1 Answers1

1

I emptied lib/example.ex and placed the following code there:

 defmodule Example do
   ...
 end

 Example.run

There is a difference between .ex files and .exs files. You decided to put that code in the application's main .ex file. Get rid of this line:

Example.run

Then, to execute Example.run() you do this:

.../example$ iex -S mix

iex(1)> Example.run
"Akash Manohar // @HashNuke"
:ok

Or, you can change the extension to .exs, then run the code with this:

.../example$ mix run lib/example.exs

On the other hand, if you want mix test to run a test, then you have to put the test in the test directory. For example:

defmodule ExampleTest do
  use ExUnit.Case

  use Hound.Helpers

  test "page title is correct" do
    Hound.start_session

    navigate_to "http://akash.im"
    #IO.inspect page_title()
    assert page_title() == "Akash Manohar // @HashNuke"  

    Hound.end_session 
  end


end

In the hound exunit example here, the hound_session() call caused an error for me:

15:06:33.736 [error] GenServer Hound.SessionServer terminating ** (RuntimeError) could not create a new session: timeout, check webdriver is running (hound) lib/hound/session_server.ex:101: Hound.SessionServer.create_session/2

7stud
  • 46,922
  • 14
  • 101
  • 127
  • @seisvelas, That timeout error was a result of Safari no longer allowing automation for some reason. When I quit Safari and restarted Safari, `hound_session()` worked fine. – 7stud Jul 04 '19 at 03:36
  • Thanks for getting back to me on it! Luckily I'm using GeckoDriver for FireFox and it worked for me as soon as I applied the suggestions in your answer :) – Alex V Jul 04 '19 at 14:40
  • @seisvelas, Yeah, things worked for me too--until they didn't. – 7stud Jul 04 '19 at 17:25
  • Ah I didn't notice that you said things were working and stopped (I thought you meant that on Safari it failed to begin with). Okay, well that's weird. Any idea why it stopped working and Safari needed to be restarted? – Alex V Jul 04 '19 at 17:40
  • 1
    @seisvelas, Nope. I posted that info so that if anyone else gets a timeout error, they can try restarting their browser. – 7stud Jul 04 '19 at 17:44