github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/press.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  // PRESS presses a keyboard key.
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    14  // @param {String | String[]} key - Target keyboard key(s).
    15  // @param {Int} [presses=1] - Count of presses.
    16  func Press(ctx context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 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  	count := values.NewInt(1)
    30  
    31  	if len(args) == 3 {
    32  		countArg := values.ToInt(args[2])
    33  
    34  		if countArg > 0 {
    35  			count = countArg
    36  		}
    37  	}
    38  
    39  	keysArg := args[1]
    40  
    41  	switch keys := keysArg.(type) {
    42  	case values.String:
    43  		return values.True, el.Press(ctx, []values.String{keys}, count)
    44  	case *values.Array:
    45  		return values.True, el.Press(ctx, values.ToStrings(keys), count)
    46  	default:
    47  		return values.None, core.TypeError(keysArg.Type(), types.String, types.Array)
    48  	}
    49  }