我启动了一个新项目,然后像这样配置了:
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 Server(我在MacOS上(,通过brew services start selenium-server-standalone
启动它,并将config :hound, driver: "selenium"
添加到config/config.exs
我添加了Application.ensure_all_started(:hound)
作为test/test_helper.exs
的第一行。
最后,我将{:hound, "~> 1.0"}
添加到Mix.exs和RAN mix test
中。那是我收到以下汇编错误的时候:
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
我是否忘记了某个地方的一步或错误地配置事物?任何帮助非常感谢!
我清空
lib/example.ex
并在此处放置以下代码:
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_session()
调用给我带来了错误:
15:06:33.736 [错误] Genserver Hound.Sessionserver终止 **(RuntimeError(无法创建一个新的会话:超时,检查WebDriver正在运行 (猎犬(lib/hound/session_server.ex:101:hound.sessionserver.create_session/2