github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_attr.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 // WAIT_ATTR waits until a target attribute's value appears 13 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 14 // @param {String} attrNameOrSelector - String of an attr name or CSS selector. 15 // @param {String | Any} attrValueOrAttrName - Attr value or name. 16 // @param {Any | Int} [attrValueOrTimeout] - Attr value or a timeout. 17 // @param {Int} [timeout=5000] - Wait timeout. 18 func WaitAttribute(ctx context.Context, args ...core.Value) (core.Value, error) { 19 return waitAttributeWhen(ctx, args, drivers.WaitEventPresence) 20 } 21 22 // WAIT_NO_ATTR waits until a target attribute's value disappears 23 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 24 // @param {String} attrNameOrSelector - String of an attr name or CSS selector. 25 // @param {String | Any} attrValueOrAttrName - Attr value or name. 26 // @param {Any | Int} [attrValueOrTimeout] - Attr value or wait timeout. 27 // @param {Int} [timeout=5000] - Wait timeout. 28 func WaitNoAttribute(ctx context.Context, args ...core.Value) (core.Value, error) { 29 return waitAttributeWhen(ctx, args, drivers.WaitEventAbsence) 30 } 31 32 func waitAttributeWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) { 33 err := core.ValidateArgs(args, 3, 5) 34 35 if err != nil { 36 return values.None, err 37 } 38 39 // document or element 40 arg1 := args[0] 41 err = core.ValidateType(arg1, drivers.HTMLPageType, drivers.HTMLDocumentType, drivers.HTMLElementType) 42 43 if err != nil { 44 return values.None, err 45 } 46 47 timeout := values.NewInt(drivers.DefaultWaitTimeout) 48 49 // if a document is passed 50 // WAIT_ATTR(doc, selector, attrName, attrValue, timeout) 51 if arg1.Type() == drivers.HTMLPageType || arg1.Type() == drivers.HTMLDocumentType { 52 // revalidate args with more accurate amount 53 err := core.ValidateArgs(args, 4, 5) 54 55 if err != nil { 56 return values.None, err 57 } 58 59 // attr name 60 err = core.ValidateType(args[2], types.String) 61 62 if err != nil { 63 return values.None, err 64 } 65 66 el, err := drivers.ToElement(arg1) 67 68 if err != nil { 69 return values.None, err 70 } 71 72 selector, err := drivers.ToQuerySelector(args[1]) 73 74 if err != nil { 75 return values.None, err 76 } 77 78 name := args[2].(values.String) 79 value := values.ToString(args[3]) 80 81 if len(args) == 5 { 82 err = core.ValidateType(args[4], types.Int) 83 84 if err != nil { 85 return values.None, err 86 } 87 88 timeout = args[4].(values.Int) 89 } 90 91 ctx, fn := waitTimeout(ctx, timeout) 92 defer fn() 93 94 return values.True, el.WaitForAttributeBySelector(ctx, selector, name, value, when) 95 } 96 97 el := arg1.(drivers.HTMLElement) 98 name := args[1].(values.String) 99 value := args[2] 100 101 if len(args) == 4 { 102 err = core.ValidateType(args[3], types.Int) 103 104 if err != nil { 105 return values.None, err 106 } 107 108 timeout = args[3].(values.Int) 109 } 110 111 ctx, fn := waitTimeout(ctx, timeout) 112 defer fn() 113 114 return values.True, el.WaitForAttribute(ctx, name, value, when) 115 }