diff --git a/remote.go b/remote.go index 653b5f4..a60d5e7 100644 --- a/remote.go +++ b/remote.go @@ -4,15 +4,18 @@ package selenium import ( + "archive/zip" "bytes" "encoding/base64" "encoding/json" "errors" "fmt" + "io" "io/ioutil" "mime" "net/http" "net/url" + "os" "path" "strings" "time" @@ -249,6 +252,64 @@ func DeleteSession(urlPrefix, id string) error { return voidCommand("DELETE", u.String(), nil) } +// Upload a files to remote WebDriver +// Returns directory which uploaded files +func (wd *remoteWD) UploadFiles(filenames ...string) (string, error) { + var buf bytes.Buffer + zipped := zip.NewWriter(&buf) + + for _, filename := range filenames { + w, err := zipped.Create(filename) + if err != nil { + return "", err + } + + r, err := os.Open(filename) + if err != nil { + return "", err + } + + fBuf, err := io.ReadAll(r) + if err != nil { + return "", err + } + + if _, err := w.Write(fBuf); err != nil { + return "", err + } + } + + zipped.Close() + + payload, err := json.Marshal(map[string]interface{}{ + "file": buf.Bytes(), + }) + + if err != nil { + return "", err + } + + resp, err := wd.execute("POST", + wd.requestURL("/session/%s/file", wd.id), + payload, + ) + if err != nil { + return "", err + } + + responseData := map[string]interface{}{} + if err := json.Unmarshal(resp, &responseData); err != nil { + return "", err + } + + directory, ok := responseData["value"].(string) + if !ok { + return "", fmt.Errorf("nil return value") + } + + return directory, nil +} + func (wd *remoteWD) stringCommand(urlTemplate string) (string, error) { url := wd.requestURL(urlTemplate, wd.id) response, err := wd.execute("GET", url, nil) diff --git a/selenium.go b/selenium.go index ecd8c91..dc7a6ad 100644 --- a/selenium.go +++ b/selenium.go @@ -292,6 +292,9 @@ type WebDriver interface { // Quit ends the current session. The browser instance will be closed. Quit() error + // Uploads files to remote WebDriver + UploadFiles(filenames ...string) (string, error) + // CurrentWindowHandle returns the ID of current window handle. CurrentWindowHandle() (string, error) // WindowHandles returns the IDs of current open windows.