Skip to content

Commit 91c9d08

Browse files
committed
Uploading files
1 parent 2723428 commit 91c9d08

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ erl_crash.dump
55
.DS_Store
66
doc/
77
screenshot-*.png
8+
/.idea

lib/hound/helpers.ex

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule Hound.Helpers do
77
import Hound.Helpers.Cookie
88
import Hound.Helpers.Dialog
99
import Hound.Helpers.Element
10+
import Hound.Helpers.File
1011
import Hound.Helpers.Navigation
1112
import Hound.Helpers.Orientation
1213
import Hound.Helpers.Page

lib/hound/helpers/file.ex

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule Hound.Helpers.File do
2+
@moduledoc "Functions to work with an files"
3+
4+
import Hound.RequestUtils
5+
6+
@spec upload(String.t) :: String.t
7+
def upload(local_file_path) do
8+
fail_if_webdriver_phantomjs("upload()")
9+
10+
session_id = Hound.current_session_id
11+
{:ok, zip_file_path} = local_file_path
12+
|> zip()
13+
zip_file_content = zip_file_path
14+
|> File.read!()
15+
|> :base64.encode()
16+
zip_file_path
17+
|> File.rm()
18+
make_req(:post, "session/#{session_id}/file", %{file: zip_file_content})
19+
end
20+
21+
@spec zip(String.t) :: String.t
22+
def zip(local_file_path) do
23+
local_file_name = local_file_path
24+
|> Path.basename()
25+
local_file_name <> ".zip"
26+
|> to_charlist()
27+
|> :zip.create(
28+
[
29+
{
30+
local_file_name
31+
|> to_charlist(),
32+
local_file_path
33+
|> File.read!()
34+
}
35+
]
36+
)
37+
end
38+
39+
defp fail_if_webdriver_phantomjs(function) do
40+
Hound.NotSupportedError.raise_for(%{driver: "phantomjs"}, function)
41+
end
42+
end

test/helpers/file_test.exs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule FileTest do
2+
use ExUnit.Case
3+
use Hound.Helpers
4+
5+
hound_session()
6+
7+
test "must download the file to a remote server" do
8+
local_file_path = "./test/sample_files/test.txt"
9+
if is_webdriver_phantomjs() do
10+
assert_raise Hound.NotSupportedError, "upload() is not supported by driver phantomjs with browser phantomjs", fn ->
11+
local_file_path
12+
|> upload()
13+
end
14+
else
15+
file_path_type = local_file_path
16+
|> upload()
17+
assert is_binary(file_path_type)
18+
end
19+
end
20+
21+
defp is_webdriver_phantomjs() do
22+
match?({:ok, %{driver: "phantomjs"}}, Hound.driver_info)
23+
end
24+
25+
end

test/sample_files/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

0 commit comments

Comments
 (0)