【问题标题】:Trying to read a webpage in Hound, I get a compilation error for Hound.start_session尝试在 Hound 中阅读网页时,出现 Hound.start_session 的编译错误
【发布时间】:2019-07-03 17:08:43
【问题描述】:

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

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(我在MacOS上)安装了Selenium服务器,通过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 并运行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

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

【问题讨论】:

  • 我想我会向您报告,我无法再与 Hound 合作。现在,我总是收到错误消息:[error] GenServer Hound.SessionServer terminating ** (RuntimeError) could not create a new session: timeout, check webdriver is running。我尝试重新安装selenium-server-standalone,删除我的 elixir 项目并重新创建它,但没有运气。现在,我在回答中使用的示例都产生了相同的超时错误。
  • 啊哈!我退出 Safari,然后重新启动 Safari,在 Safari 菜单栏中重新检查了Develop/Allow Remote Automation,然后我的 Hound 测试再次运行。

标签: selenium elixir elixir-mix hound


【解决方案1】:

我清空了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 exunit 示例 here 中,hound_session() 调用对我造成了错误:

15:06:33.736 [错误] GenServer Hound.SessionServer 终止 ** (RuntimeError) 无法创建新会话:超时,检查 webdriver 是否正在运行 (猎犬) lib/hound/session_server.ex:101: Hound.SessionServer.create_session/2

【讨论】:

  • @seisvelas,该超时错误是 Safari 出于某种原因不再允许自动化的结果。当我退出 Safari 并重新启动 Safari 时,hound_session() 工作正常。
  • 感谢您回复我!幸运的是,我正在为 FireFox 使用 GeckoDriver,一旦我在您的回答中应用了这些建议,它就对我有用了 :)
  • @seisvelas,是的,事情也对我有用——直到他们没有。
  • 啊,我没有注意到你说事情正在工作并停止(我以为你的意思是在 Safari 上它无法开始)。好吧,这很奇怪。知道为什么它停止工作并且需要重新启动 Safari 吗?
  • @seisvelas,不。我发布了该信息,以便如果其他人收到超时错误,他们可以尝试重新启动浏览器。
猜你喜欢
  • 2022-07-16
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多