github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/navigate.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  // NAVIGATE navigates a given page to a new resource.
    13  // The operation blocks the execution until the page gets loaded.
    14  // Which means there is no need in WAIT_NAVIGATION function.
    15  // @param {HTMLPage} page - Target page.
    16  // @param {String} url - Target url to navigate.
    17  // @param {Int} [timeout=5000] - Navigation timeout.
    18  func Navigate(ctx context.Context, args ...core.Value) (core.Value, error) {
    19  	err := core.ValidateArgs(args, 2, 3)
    20  
    21  	if err != nil {
    22  		return values.None, err
    23  	}
    24  
    25  	page, err := drivers.ToPage(args[0])
    26  
    27  	if err != nil {
    28  		return values.None, err
    29  	}
    30  
    31  	err = core.ValidateType(args[1], types.String)
    32  
    33  	if err != nil {
    34  		return values.None, err
    35  	}
    36  
    37  	timeout := values.NewInt(drivers.DefaultWaitTimeout)
    38  
    39  	if len(args) > 2 {
    40  		err = core.ValidateType(args[2], types.Int)
    41  
    42  		if err != nil {
    43  			return values.None, err
    44  		}
    45  
    46  		timeout = args[2].(values.Int)
    47  	}
    48  
    49  	ctx, fn := waitTimeout(ctx, timeout)
    50  	defer fn()
    51  
    52  	return values.True, page.Navigate(ctx, args[1].(values.String))
    53  }