Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Add setInputFiles to ElementHandle #1097

Merged
merged 8 commits into from
Mar 7, 2024
Prev Previous commit
Next Next commit
Refactor: separate SetInputFiles parameter from options
  • Loading branch information
bandorko authored and inancgumus committed Mar 7, 2024
commit 42a56bdd7bc81a22225d3aa6e7fb3fa740223d52
9 changes: 7 additions & 2 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,14 +1243,19 @@ func (h *ElementHandle) SelectText(opts goja.Value) {
}

// SetInputFiles sets the Files given in the opts into the <input type="file"> element.
bandorko marked this conversation as resolved.
Show resolved Hide resolved
func (h *ElementHandle) SetInputFiles(opts goja.Value) {
func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) {
actionOpts := NewElementHandleSetInputFilesOptions(h.defaultTimeout())
if err := actionOpts.Parse(h.ctx, opts); err != nil {
k6ext.Panic(h.ctx, "parsing setInputFiles options: %w", err)
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
}

actionParam := &Files{}
if err := actionParam.Parse(h.ctx, files); err != nil {
k6ext.Panic(h.ctx, "parsing setInputFiles parameter: %w", err)
}

fn := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
return nil, handle.setInputFiles(apiCtx, actionOpts.Payload)
return nil, handle.setInputFiles(apiCtx, actionParam.Payload)
}
actFn := h.newAction([]string{}, fn, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout)
_, err := call(h.ctx, actFn, actionOpts.Timeout)
Expand Down
54 changes: 32 additions & 22 deletions common/element_handle_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ type File struct {
Buffer string `json:"buffer"`
}

// Files is the input parameter for ElementHandle.SetInputFiles
type Files struct {
Payload []*File `json:"payload"`
}

// ElementHandleSetInputFilesOption are options for ElementHandle.SetInputFiles.
type ElementHandleSetInputFilesOption struct {
ElementHandleBaseOptions
Payload []*File `json:"payload"`
}

type ElementHandlePressOptions struct {
Expand Down Expand Up @@ -200,51 +204,57 @@ func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *Element
}
}

// addFile to the option. Input value can be a path, or a file descriptor object.
func (o *ElementHandleSetInputFilesOption) addFile(ctx context.Context, opts goja.Value) error {
if !gojaValueExists(opts) {
// addFile to the struct. Input value can be a path, or a file descriptor object.
func (f *Files) addFile(ctx context.Context, file goja.Value) error {
if !gojaValueExists(file) {
return nil
}
rt := k6ext.Runtime(ctx)
optsType := opts.ExportType()
switch optsType.Kind() {
fileType := file.ExportType()
switch fileType.Kind() {
case reflect.Map: // file descriptor object
var file File
if err := rt.ExportTo(opts, &file); err != nil {
return fmt.Errorf("unable to parse SetInputFileOptions; reason: %w", err)
var parsedFile File
if err := rt.ExportTo(file, &parsedFile); err != nil {
return fmt.Errorf("Unable to parse SetInputFiles parameter; reason: %w", err)
bandorko marked this conversation as resolved.
Show resolved Hide resolved
}
o.Payload = append(o.Payload, &file)
f.Payload = append(f.Payload, &parsedFile)
case reflect.String: // file path
o.Payload = append(o.Payload, &File{Path: opts.Export().(string)})
f.Payload = append(f.Payload, &File{Path: file.Export().(string)})
default:
return fmt.Errorf("Cannot parse setInputFiles parameter")
return fmt.Errorf("Unable to parse setInputFiles parameter")
bandorko marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

// Parse parses the ElementHandleSetInputFilesOption from the given opts.
func (o *ElementHandleSetInputFilesOption) Parse(ctx context.Context, opts goja.Value) error {
// Parse parses the Files struct from the given goja.Value
func (f *Files) Parse(ctx context.Context, files goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil {
return err
}
if !gojaValueExists(opts) {
if !gojaValueExists(files) {
return nil
}

optsType := opts.ExportType()
optsType := files.ExportType()
switch optsType.Kind() {
case reflect.Slice: // array of filePaths or array of file descriptor objects
gopts := opts.ToObject(rt)
gopts := files.ToObject(rt)
for _, k := range gopts.Keys() {
err := o.addFile(ctx, gopts.Get(k))
err := f.addFile(ctx, gopts.Get(k))
if err != nil {
return err
}
}
default: // filePath or file descriptor object
return o.addFile(ctx, opts)
return f.addFile(ctx, files)
}

return nil
}

// Parse parses the ElementHandleSetInputFilesOption from the given opts.
func (o *ElementHandleSetInputFilesOption) Parse(ctx context.Context, opts goja.Value) error {
if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil {
return err
}

return nil
Expand Down