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

Commit a5d43f1

Browse files
committed
Refactor ElementHandleScreenshotOptions.Parse
1 parent 80ae11c commit a5d43f1

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

common/element_handle_options.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -435,36 +435,39 @@ func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHan
435435
}
436436

437437
// Parse parses the ElementHandleScreenshotOptions from the given opts.
438-
func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error {
438+
func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error { //nolint:cyclop
439+
if !sobekValueExists(opts) {
440+
return nil
441+
}
442+
439443
rt := k6ext.Runtime(ctx)
440-
if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) {
441-
formatSpecified := false
442-
opts := opts.ToObject(rt)
443-
for _, k := range opts.Keys() {
444-
switch k {
445-
case "omitBackground":
446-
o.OmitBackground = opts.Get(k).ToBoolean()
447-
case "path":
448-
o.Path = opts.Get(k).String()
449-
case "quality":
450-
o.Quality = opts.Get(k).ToInteger()
451-
case "type":
452-
if f, ok := imageFormatToID[opts.Get(k).String()]; ok {
453-
o.Format = f
454-
formatSpecified = true
455-
}
456-
case "timeout":
457-
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
444+
formatSpecified := false
445+
obj := opts.ToObject(rt)
446+
for _, k := range obj.Keys() {
447+
switch k {
448+
case "omitBackground":
449+
o.OmitBackground = obj.Get(k).ToBoolean()
450+
case "path":
451+
o.Path = obj.Get(k).String()
452+
case "quality":
453+
o.Quality = obj.Get(k).ToInteger()
454+
case "type":
455+
if f, ok := imageFormatToID[obj.Get(k).String()]; ok {
456+
o.Format = f
457+
formatSpecified = true
458458
}
459+
case "timeout":
460+
o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond
459461
}
462+
}
460463

461-
// Infer file format by path if format not explicitly specified (default is PNG)
462-
if o.Path != "" && !formatSpecified {
463-
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
464-
o.Format = ImageFormatJPEG
465-
}
464+
// Infer file format by path if format not explicitly specified (default is PNG)
465+
if o.Path != "" && !formatSpecified {
466+
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
467+
o.Format = ImageFormatJPEG
466468
}
467469
}
470+
468471
return nil
469472
}
470473

0 commit comments

Comments
 (0)