github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/input.go (about) 1 package html 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/drivers" 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 "github.com/MontFerret/ferret/pkg/runtime/values/types" 10 ) 11 12 // INPUT types a value to an underlying input element. 13 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 14 // @param {String} valueOrSelector - CSS selector or a value. 15 // @param {String} value - Target value. 16 // @param {Int} [delay] - Target value. 17 // @return {Boolean} - Returns true if an element was found. 18 func Input(ctx context.Context, args ...core.Value) (core.Value, error) { 19 err := core.ValidateArgs(args, 2, 4) 20 21 if err != nil { 22 return values.False, err 23 } 24 25 el, err := drivers.ToElement(args[0]) 26 27 if err != nil { 28 return values.False, err 29 } 30 31 delay := values.NewInt(drivers.DefaultKeyboardDelay) 32 33 // INPUT(el, value) 34 if len(args) == 2 { 35 return values.True, el.Input(ctx, args[1], delay) 36 } 37 38 var selector drivers.QuerySelector 39 var value core.Value 40 41 // INPUT(el, valueOrSelector, valueOrOpts) 42 if len(args) == 3 { 43 switch v := args[2].(type) { 44 // INPUT(el, value, delay) 45 case values.Int, values.Float: 46 value = args[1] 47 delay = values.ToInt(v) 48 49 return values.True, el.Input(ctx, value, delay) 50 default: 51 // INPUT(el, selector, value) 52 selector, err = drivers.ToQuerySelector(args[1]) 53 54 if err != nil { 55 return values.None, err 56 } 57 58 value = args[2] 59 } 60 } else { 61 // INPUT(el, selector, value, delay) 62 if err := core.ValidateType(args[3], types.Int); err != nil { 63 return values.False, err 64 } 65 66 selector, err = drivers.ToQuerySelector(args[1]) 67 68 if err != nil { 69 return values.None, err 70 } 71 72 value = args[2] 73 delay = values.ToInt(args[3]) 74 } 75 76 exists, err := el.ExistsBySelector(ctx, selector) 77 78 if err != nil { 79 return values.False, err 80 } 81 82 if !exists { 83 return values.False, nil 84 } 85 86 return values.True, el.InputBySelector(ctx, selector, value, delay) 87 }