尝试在 Hound 中阅读网页时,出现 Hound.start_session 的编译错误

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

我开始了一个新项目并像这样配置它:

mix new example
cd example

我清空了“lib/example.ex”并在其中放置了以下代码:

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

这是 https://github.com/HashNuke/hound/blob/master/notes/simple-browser-automation.md

中提供的示例代码

然后我通过 brew install selenium-server-standalone 安装了 Selenium 服务器(我在 MacOS 上),通过 brew services start selenium-server-standalone 启动它并添加 config :hound, driver: "selenium"config/config.exs

我在test/test_helper.exs的第一行添加了Application.ensure_all_started(:hound)

最后,我在 mix.exs 和 运行 mix test 中添加了 {:hound, "~> 1.0"}。那是当我收到以下编译错误时:

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

我是不是忘记了某个步骤或配置不正确?非常感谢任何帮助,谢谢!

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

 defmodule Example do
   ...
 end

 Example.run

.ex 个文件和 .exs 个文件之间存在差异。您决定将该代码放入应用程序的主 .ex 文件中。去掉这一行:

Example.run

然后,要执行 Example.run() 你这样做:

.../example$ iex -S mix

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

或者,您可以将扩展名更改为 .exs,然后 运行 代码为:

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

另一方面,如果你想mix test到运行一个测试,那么你必须把测试放在测试目录下。例如:

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

在 hound exunit 示例 here 中,hound_session() 调用对我造成错误:

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