github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_class.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_CLASS waits for a class to appear on a given element.
    13  // Stops the execution until the navigation ends or operation times out.
    14  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    15  // @param {String} selectorOrClass - If document is passed, this param must represent an element selector. Otherwise target class.
    16  // @param {String | Int} [classOrTimeout] - If document is passed, this param must represent target class name. Otherwise timeout.
    17  // @param {Int} [timeout] - If document is passed, this param must represent timeout. Otherwise not passed.
    18  func WaitClass(ctx context.Context, args ...core.Value) (core.Value, error) {
    19  	return waitClassWhen(ctx, args, drivers.WaitEventPresence)
    20  }
    21  
    22  // WAIT_NO_CLASS waits for a class to disappear on a given element.
    23  // Stops the execution until the navigation ends or operation times out.
    24  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    25  // @param {String} selectorOrClass - If document is passed, this param must represent an element selector. Otherwise target class.
    26  // @param {String | Int} [classOrTimeout] - If document is passed, this param must represent target class name. Otherwise timeout.
    27  // @param {Int} [timeout] - If document is passed, this param must represent timeout. Otherwise not passed.
    28  func WaitNoClass(ctx context.Context, args ...core.Value) (core.Value, error) {
    29  	return waitClassWhen(ctx, args, drivers.WaitEventAbsence)
    30  }
    31  
    32  func waitClassWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) {
    33  	err := core.ValidateArgs(args, 2, 4)
    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  	if arg1.Type() == drivers.HTMLPageType || arg1.Type() == drivers.HTMLDocumentType {
    51  		// revalidate args with more accurate amount
    52  		err := core.ValidateArgs(args, 3, 4)
    53  
    54  		if err != nil {
    55  			return values.None, err
    56  		}
    57  
    58  		selector, err := drivers.ToQuerySelector(args[1])
    59  
    60  		if err != nil {
    61  			return values.None, err
    62  		}
    63  
    64  		// class
    65  		err = core.ValidateType(args[2], types.String)
    66  
    67  		if err != nil {
    68  			return values.None, err
    69  		}
    70  
    71  		el, err := drivers.ToElement(arg1)
    72  
    73  		if err != nil {
    74  			return values.None, err
    75  		}
    76  
    77  		class := args[2].(values.String)
    78  
    79  		if len(args) == 4 {
    80  			err = core.ValidateType(args[3], types.Int)
    81  
    82  			if err != nil {
    83  				return values.None, err
    84  			}
    85  
    86  			timeout = args[3].(values.Int)
    87  		}
    88  
    89  		ctx, fn := waitTimeout(ctx, timeout)
    90  		defer fn()
    91  
    92  		return values.True, el.WaitForClassBySelector(ctx, selector, class, when)
    93  	}
    94  
    95  	el := arg1.(drivers.HTMLElement)
    96  	class := args[1].(values.String)
    97  
    98  	if len(args) == 3 {
    99  		err = core.ValidateType(args[2], types.Int)
   100  
   101  		if err != nil {
   102  			return values.None, err
   103  		}
   104  
   105  		timeout = args[2].(values.Int)
   106  	}
   107  
   108  	ctx, fn := waitTimeout(ctx, timeout)
   109  	defer fn()
   110  
   111  	return values.True, el.WaitForClass(ctx, class, when)
   112  }