Skip to content

Commit

Permalink
Simplify TestFileUpload with T.TempDir and os.WriteFile (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Mar 14, 2023
1 parent 6e7da0b commit 830eeb3
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -1245,25 +1246,16 @@ func TestFileUpload(t *testing.T) {
s := httptest.NewServer(mux)
defer s.Close()

// create temporary file on disk
tmpfile, err := os.CreateTemp("", "chromedp-upload-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
if _, err := tmpfile.WriteString(uploadHTML); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
uploadFile := filepath.Join(t.TempDir(), "chromedp-upload-test")
if err := os.WriteFile(uploadFile, []byte(uploadHTML), 0o666); err != nil {
t.Fatal(err)
}

tests := []struct {
a Action
}{
{SendKeys(`input[name="upload"]`, tmpfile.Name(), NodeVisible)},
{SetUploadFiles(`input[name="upload"]`, []string{tmpfile.Name()}, NodeVisible)},
{SendKeys(`input[name="upload"]`, uploadFile, NodeVisible)},
{SetUploadFiles(`input[name="upload"]`, []string{uploadFile}, NodeVisible)},
}

// Don't run these tests in parallel. The only way to do so would be to
Expand Down

0 comments on commit 830eeb3

Please sign in to comment.