From 1ecd69701224128d7786ceb2c9173f4559163477 Mon Sep 17 00:00:00 2001 From: Sindyukov Alexei Date: Fri, 8 Sep 2017 15:27:40 +0400 Subject: [PATCH] Uploading files --- .gitignore | 1 + lib/hound/helpers.ex | 1 + lib/hound/helpers/file.ex | 45 ++++++++++++++++++++++++++++++++++++++ test/helpers/file_test.exs | 25 +++++++++++++++++++++ test/sample_files/test.txt | 1 + 5 files changed, 73 insertions(+) create mode 100644 lib/hound/helpers/file.ex create mode 100644 test/helpers/file_test.exs create mode 100644 test/sample_files/test.txt diff --git a/.gitignore b/.gitignore index ff6a7e5..41a1b2b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ erl_crash.dump .DS_Store doc/ screenshot-*.png +/.idea diff --git a/lib/hound/helpers.ex b/lib/hound/helpers.ex index 87a74ae..7a6d658 100644 --- a/lib/hound/helpers.ex +++ b/lib/hound/helpers.ex @@ -7,6 +7,7 @@ defmodule Hound.Helpers do import Hound.Helpers.Cookie import Hound.Helpers.Dialog import Hound.Helpers.Element + import Hound.Helpers.File import Hound.Helpers.Navigation import Hound.Helpers.Orientation import Hound.Helpers.Page diff --git a/lib/hound/helpers/file.ex b/lib/hound/helpers/file.ex new file mode 100644 index 0000000..c08e932 --- /dev/null +++ b/lib/hound/helpers/file.ex @@ -0,0 +1,45 @@ +defmodule Hound.Helpers.File do + @moduledoc "Functions to work with an files" + + import Hound.RequestUtils + + @spec upload(String.t) :: String.t + def upload(local_file_path) do + fail_if_webdriver_phantomjs("upload()") + + session_id = Hound.current_session_id + {:ok, zip_file_path} = local_file_path + |> zip() + zip_file_content = zip_file_path + |> File.read!() + |> :base64.encode() + zip_file_path + |> File.rm() + make_req(:post, "session/#{session_id}/file", %{file: zip_file_content}) + end + + @spec zip(String.t) :: String.t + def zip(local_file_path) do + + + + local_file_name = local_file_path + |> Path.basename() + local_file_name <> ".zip" + |> to_char_list() + |> :zip.create( + [ + { + local_file_name + |> to_char_list(), + local_file_path + |> File.read!() + } + ] + ) + end + + defp fail_if_webdriver_phantomjs(function) do + Hound.NotSupportedError.raise_for(%{driver: "phantomjs"}, function) + end +end diff --git a/test/helpers/file_test.exs b/test/helpers/file_test.exs new file mode 100644 index 0000000..8ddc3bf --- /dev/null +++ b/test/helpers/file_test.exs @@ -0,0 +1,25 @@ +defmodule FileTest do + use ExUnit.Case + use Hound.Helpers + + hound_session() + + test "must upload the file to a remote server" do + local_file_path = "./test/sample_files/test.txt" + if is_webdriver_phantomjs() do + assert_raise Hound.NotSupportedError, "upload() is not supported by driver phantomjs with browser phantomjs", fn -> + local_file_path + |> upload() + end + else + file_path_type = local_file_path + |> upload() + assert is_binary(file_path_type) + end + end + + defp is_webdriver_phantomjs() do + match?({:ok, %{driver: "phantomjs"}}, Hound.driver_info) + end + +end diff --git a/test/sample_files/test.txt b/test/sample_files/test.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/test/sample_files/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file