Skip to content

Commit

Permalink
Uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
RODJER200586 committed Sep 8, 2017
1 parent 2723428 commit 1ecd697
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ erl_crash.dump
.DS_Store
doc/
screenshot-*.png
/.idea
1 change: 1 addition & 0 deletions lib/hound/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lib/hound/helpers/file.ex
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/helpers/file_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/sample_files/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test

0 comments on commit 1ecd697

Please sign in to comment.