簡體   English   中英

如何使用獵犬和硒在e劑/鳳凰中運行測試

[英]How to run tests in elixir/phoenix using hound and selenium

這是我的mix.exs

defmodule HelloPhoenix.Mixfile do
use Mix.Project

def project do
[app: :hello_phoenix,
 version: "0.0.1",
 elixir: "~> 1.0",
 elixirc_paths: elixirc_paths(Mix.env),
 compilers: [:phoenix, :gettext] ++ Mix.compilers,
 build_embedded: Mix.env == :prod,
 start_permanent: Mix.env == :prod,
 aliases: aliases,
 deps: deps]
end

# Configuration for the OTP application.
#
# Type `mix help compile.app` for more information.
def application do
     [mod: {HelloPhoenix, []},
     applications: app_list(Mix.env) ]
 end

    def app_list do
    [:phoenix, :phoenix_html, :cowboy, :logger, :gettext, :phoenix_ecto, :postgrex]
end

    def app_list(:test), do: [:hound | app_list]
def app_list(_),     do: app_list


  # Specifies which paths to compile per environment.
      defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
  defp elixirc_paths(_),     do: ["lib", "web"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
[{:phoenix, "~> 1.1.4"},
 {:postgrex, ">= 0.0.0"},
 {:phoenix_ecto, "~> 2.0"},
 {:phoenix_html, "~> 2.4"},
 {:phoenix_live_reload, "~> 1.0", only: :dev},
 {:gettext, "~> 0.9"},
 {:cowboy, "~> 1.0"},
{:hound, "~> 0.8"}]
  end

  # Aliases are shortcut or tasks specific to the current project.
   # For example, to create, migrate and run the seeds file at once:
   #
   #     $ mix ecto.setup
   #
   # See the documentation for `Mix` for more info on aliases.
   defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
 "ecto.reset": ["ecto.drop", "ecto.setup"]]
  end
 end

這是我的config / config.exs

# Define your application's host and port (defaults to "http://localhost:4001")
#config :hound, app_host: "http://localhost", app_port: 4000

# Start with selenium driver (default)
config :hound, driver: "selenium"

# Use Chrome with the default driver (selenium)
#config :hound, browser: "chrome"

# Start with default driver at port 1234 and use firefox
#config :hound, port: 1234, browser: "firefox"

# Start Hound for PhantomJs
#config :hound, driver: "phantomjs"

# Start Hound for ChromeDriver (default port 9515 assumed)
#config :hound, driver: "chrome_driver"

測試/ sample_test.exs

    defmodule HelloPhoenix.SampleTest do
  use HelloPhoenix.ConnCase

  # Import Hound helpers
  use Hound.Helpers

  # Start a Hound session
  hound_session
    IO.puts "hai"
  test "GET /" do
    navigate_to("http://localhost:4000")
    :timer.sleep(5000) # puts server to sleep
    assert page_source =~ "Welcome to Phoenix"
  end
end

配置/ test.exs

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :hello_phoenix, HelloPhoenix.Endpoint,
  http: [port: 4000],
  server: true

啟動Webdriver服務器 (一次嘗試所有這些)

  1. java -jar selenium-server-standalone-2.48.2.jar

  2. java -jar selenium-server-standalone-2.49.0.jar

  3. java -jar selenium-server-standalone-2.52.0.jar

  4. docker run -it -p 4444:4444 danielfrg / selenium

  5. docker run --privileged -p 4444:4444 -p 5999:5999 -d vvoyer / docker-selenium-firefox-chrome

每次我運行該應用程序時,都會出現此錯誤:

karthik@dkarnik2-Vostro-3558:~/5/hello_phoenix/hello_phoenix$ mix test
hai
....10:46:52.543 [error] GenServer Hound.SessionServer terminating
** (MatchError) no match of right hand side value: {:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}
    (hound) lib/hound/request_utils.ex:43:Hound.RequestUtils.send_req/4
    (hound) lib/hound/session_server.ex:67: Hound.SessionServer.handle_call/3
    (stdlib) gen_server.erl:629: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:661: :gen_server.handle_msg/5
    (stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3


  1) test GET / (HelloPhoenix.SampleTest)
     test/sample_test.exs:10
     ** (exit) exited in: GenServer.call(Hound.SessionServer, {:change_session, #PID<0.394.0>, :default, %{}}, 60000)
         ** (EXIT) an exception was raised:
             ** (MatchError) no match of right hand side value: {:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}
                 (hound) lib/hound/request_utils.ex:43: Hound.RequestUtils.send_req/4
                 (hound) lib/hound/session_server.ex:67: Hound.SessionServer.handle_call/3
                 (stdlib) gen_server.erl:629: :gen_server.try_handle_call/4
                 (stdlib) gen_server.erl:661: :gen_server.handle_msg/5
                 (stdlib) proc_lib.erl:240:     :proc_lib.init_p_do_apply/3
     stacktrace:
       (elixir) lib/gen_server.ex:544: GenServer.call/3
       test/sample_test.exs:8: HelloPhoenix.SampleTest.__ex_unit_setup_1/1
       test/sample_test.exs:1: HelloPhoenix.SampleTest.__ex_unit__/2



Finished in 0.7 seconds (0.5s on load, 0.1s on tests)
5 tests, 1 failure

Randomized with seed 332515
karthik@dkarnik2-Vostro-3558:~/5/hello_phoenix/hello_phoenix$

為了完整起見,以防萬一有人遇到類似問題,我將做一個總結:

當Erlang返回:econnrefused錯誤時,表示連接存在問題。 以下內容適用於任何編程語言,因為它是網絡問題:

  • 您的網絡接口已關閉。 解決方案:連接到接口。
  • 您試圖連接到錯誤的地址和/或端口。 解決方案:在您的代碼或配置中查看以檢查IP地址和端口是否正確。 還要檢查遠程主機是否實際上在該IP地址中,並在該端口中偵聽連接。
  • 您被防火牆阻止了。 解決方案:這是一個更復雜的問題,因為它取決於您如何部署和測試應用程序。
  • 從運行您的應用程序的主機無法訪問遠程主機。 解決方案:這也是一個更復雜的問題,但是對於初學者來說,您可以檢查遠程主機是否在專用網絡中
    • 在接入點中配置適當的端口轉發。
    • 如果是VM,則將來賓OS的虛擬網絡接口連接到橋接適配器而不是NAT適配器,並使用與主機OS相同的網絡接口。 如果未在路由器/訪問點中啟用DHCP,則應在與主機相同的網絡中為訪客計算機分配新的IP地址。 在Virtualbox中,您可以在Settings > Network配置適配器。*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM