forked from nicholasvuono/xk6-playwright
/
file_chooser_wrapper.go
70 lines (56 loc) · 1.55 KB
/
file_chooser_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package playwright
import (
"encoding/base64"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/playwright-community/playwright-go"
"go.k6.io/k6/js/modules"
)
type fileChooserWrapper struct {
FileChooser playwright.FileChooser
vu modules.VU
}
type InputFilePaths struct {
Name string
MimeType string
Path string
}
func (f *fileChooserWrapper) SetFiles(files []InputFilePaths, options ...playwright.ElementHandleSetInputFilesOptions) {
var inputFiles []playwright.InputFile
log.Printf("I am in our custom setFiles go function")
cwd, err := os.Getwd()
if err != nil {
Throw(f.vu.Runtime(), "Error getting current working directory (cwd)", err)
}
for _, inputFilePath := range files {
var file []byte
var err error
if strings.HasPrefix(inputFilePath.Path, "data:") {
base64File := strings.SplitN(inputFilePath.Path, "base64,", 2)
file, err = base64.StdEncoding.DecodeString(base64File[1])
} else {
file, err = ioutil.ReadFile(filepath.Join(cwd, inputFilePath.Path))
}
if err != nil {
Throw(f.vu.Runtime(), "Error reading file", err)
}
inputFiles = append(inputFiles, playwright.InputFile{
Name: inputFilePath.Name,
MimeType: inputFilePath.MimeType,
Buffer: file,
})
}
err = f.FileChooser.SetFiles(inputFiles, options...)
if err != nil {
Throw(f.vu.Runtime(), "Error setting files", err)
}
}
func newFileChooserWrapper(fileChooser playwright.FileChooser, vu modules.VU) *fileChooserWrapper {
return &fileChooserWrapper{
FileChooser: fileChooser,
vu: vu,
}
}