Skip to content

Uploading files #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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