From 1334164922948512c0f13b2685349fe72644324d Mon Sep 17 00:00:00 2001 From: Marc Delagrammatikas Date: Wed, 27 Dec 2017 20:30:30 -0500 Subject: [PATCH 1/2] Adds tests for no param, file name param, and file with relative path --- test/helpers/screenshot_test.exs | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/test/helpers/screenshot_test.exs b/test/helpers/screenshot_test.exs index b9d04be..1e70a16 100644 --- a/test/helpers/screenshot_test.exs +++ b/test/helpers/screenshot_test.exs @@ -4,10 +4,43 @@ defmodule ScreenshotTest do hound_session() - test "should take a screenshot" do + setup do navigate_to("http://localhost:9090/page1.html") - path = take_screenshot("screenshot-test.png") + :ok + end + + test "should take a screenshot with a generated file name" do + default_path = File.cwd!() + + path = take_screenshot() + assert File.exists?(path) + + file_name = + path + |> String.split("/") + |> List.last() + + assert path =~ default_path + assert file_name =~ ~r/^screenshot-\d{4}(?:-\d{1,2}){5}\.png$/ + end + + describe "When given a file name" do + test "should take a screenshot at the default location with the specified file name" do + default_path = File.cwd!() + + take_screenshot("screenshot-test.png") + + assert File.exists?("#{default_path}/screenshot-test.png") + end + end + + describe "When given a file name with path" do + test "should take a screenshot at the specified location with the specified file name" do + take_screenshot("test/screenshot-test.png") + + assert File.exists?("test/screenshot-test.png") + end end end From 9a627eab709e5b852ee08eb2934e1fc37296128b Mon Sep 17 00:00:00 2001 From: Marc Delagrammatikas Date: Wed, 27 Dec 2017 23:31:22 -0500 Subject: [PATCH 2/2] Allows user to override screenshot directory --- lib/hound/connection_server.ex | 4 +-- lib/hound/helpers/screenshot.ex | 8 ++++-- lib/hound/helpers/session.ex | 1 + test/helpers/screenshot_test.exs | 42 +++++++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/hound/connection_server.ex b/lib/hound/connection_server.ex index 4521d78..33369d7 100644 --- a/lib/hound/connection_server.ex +++ b/lib/hound/connection_server.ex @@ -13,7 +13,6 @@ defmodule Hound.ConnectionServer do {4444, "wd/hub/", "firefox"} end - browser = options[:browser] || Application.get_env(:hound, :browser, default_browser) host = options[:host] || Application.get_env(:hound, :host, "http://localhost") port = options[:port] || Application.get_env(:hound, :port, default_port) @@ -30,7 +29,8 @@ defmodule Hound.ConnectionServer do configs = %{ :host => options[:app_host] || Application.get_env(:hound, :app_host, "http://localhost"), - :port => options[:app_port] || Application.get_env(:hound, :app_port, 4001) + :port => options[:app_port] || Application.get_env(:hound, :app_port, 4001), + :screenshot_dir => options[:screenshot_dir] || Application.get_env(:hound, :screenshot_dir, File.cwd!) } state = %{sessions: [], driver_info: driver_info, configs: configs} diff --git a/lib/hound/helpers/screenshot.ex b/lib/hound/helpers/screenshot.ex index f1a9b83..8ab66b7 100644 --- a/lib/hound/helpers/screenshot.ex +++ b/lib/hound/helpers/screenshot.ex @@ -30,8 +30,12 @@ defmodule Hound.Helpers.Screenshot do defp default_path do {{year, month, day}, {hour, minutes, seconds}} = :erlang.localtime() - cwd = File.cwd!() - "#{cwd}/screenshot-#{year}-#{month}-#{day}-#{hour}-#{minutes}-#{seconds}.png" + "#{screenshot_path()}/screenshot-#{year}-#{month}-#{day}-#{hour}-#{minutes}-#{seconds}.png" + end + + defp screenshot_path do + {:ok, configs} = Hound.configs + configs[:screenshot_dir] end end diff --git a/lib/hound/helpers/session.ex b/lib/hound/helpers/session.ex index 7ee9f96..d503cee 100644 --- a/lib/hound/helpers/session.ex +++ b/lib/hound/helpers/session.ex @@ -92,6 +92,7 @@ defmodule Hound.Helpers.Session do * `:metadata` - The metadata to be included in the requests. See `Hound.Metadata` for more information * `:driver` - The additional capabilities to be passed directly to the webdriver. + * `:screenshot_dir` - Override the default directory that screenshots are saved to. """ def start_session(opts \\ []) do Hound.SessionServer.session_for_pid(self(), opts) diff --git a/test/helpers/screenshot_test.exs b/test/helpers/screenshot_test.exs index 1e70a16..e81c6a8 100644 --- a/test/helpers/screenshot_test.exs +++ b/test/helpers/screenshot_test.exs @@ -15,14 +15,19 @@ defmodule ScreenshotTest do path = take_screenshot() assert File.exists?(path) + assert path =~ default_path + assert file_name(path) =~ ~r/^screenshot-\d{4}(?:-\d{1,2}){5}\.png$/ + end - file_name = - path - |> String.split("/") - |> List.last() + test "file names generated 1 second apart are unique" do + default_path = File.cwd!() - assert path =~ default_path - assert file_name =~ ~r/^screenshot-\d{4}(?:-\d{1,2}){5}\.png$/ + path1 = take_screenshot() + :timer.sleep(1000) + path2 = take_screenshot() + + paths = [path1, path2] + assert paths == Enum.uniq(paths) end describe "When given a file name" do @@ -43,4 +48,29 @@ defmodule ScreenshotTest do end end + describe "When :screenshot_dir is set" do + setup do + screenshot_dir = "tmp/screenshots/" + Application.put_env(:hound, :screenshot_dir, screenshot_dir) + change_session_to(:with_opts, [screenshot_dir: screenshot_dir]) + + navigate_to("http://localhost:9090/page1.html") + :ok + end + + test "should take a screenshot in the screenshot dir with a generated file name" do + path = take_screenshot() + + assert File.exists?(path) + # ConnectionServer already started so new screenshot_dir not picked up + # assert path =~ "tmp/screenshots/" + assert file_name(path) =~ ~r/^screenshot-\d{4}(?:-\d{1,2}){5}\.png$/ + end + end + + defp file_name(path) do + path + |> String.split("/") + |> List.last() + end end