github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/click.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 // CLICK dispatches click event on a given element 13 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 14 // @param {String | Int} [cssSelectorOrClicks] - CSS selector or count of clicks. 15 // @param {Int} [clicks=1] - Count of clicks. 16 func Click(ctx context.Context, args ...core.Value) (core.Value, error) { 17 err := core.ValidateArgs(args, 1, 3) 18 19 if err != nil { 20 return values.False, err 21 } 22 23 el, err := drivers.ToElement(args[0]) 24 25 if err != nil { 26 return values.False, err 27 } 28 29 // CLICK(elOrDoc) 30 if len(args) == 1 { 31 return values.True, el.Click(ctx, 1) 32 } 33 34 if len(args) == 2 { 35 err := core.ValidateType(args[1], types.String, types.Int, drivers.QuerySelectorType) 36 37 if err != nil { 38 return values.False, err 39 } 40 41 if args[1].Type() == types.String || args[1].Type() == drivers.QuerySelectorType { 42 selector, err := drivers.ToQuerySelector(args[1]) 43 44 if err != nil { 45 return values.None, err 46 } 47 48 exists, err := el.ExistsBySelector(ctx, selector) 49 50 if err != nil { 51 return values.False, err 52 } 53 54 if !exists { 55 return exists, nil 56 } 57 58 return exists, el.ClickBySelector(ctx, selector, 1) 59 } 60 61 return values.True, el.Click(ctx, values.ToInt(args[1])) 62 } 63 64 err = core.ValidateType(args[2], types.Int) 65 66 if err != nil { 67 return values.False, err 68 } 69 70 // CLICK(doc, selector) 71 selector, err := drivers.ToQuerySelector(args[1]) 72 73 if err != nil { 74 return values.None, err 75 } 76 77 exists, err := el.ExistsBySelector(ctx, selector) 78 79 if err != nil { 80 return values.False, err 81 } 82 83 if !exists { 84 return exists, nil 85 } 86 87 count := values.ToInt(args[2]) 88 89 return exists, el.ClickBySelector(ctx, selector, count) 90 }