简体   繁体   中英

Trying to read a webpage in Hound, I get a compilation error for Hound.start_session

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM