1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-18 23:47:48 +02:00
ferret/pkg/stdlib/html/wait_navigation.go
Tim Voronov fe7b45df6e
Bugfix/#399 navigation (#432)
* Refactored networking

* Some work

* Added event loop

* Renamed EventHandler to Handler

* wip

* Removed console logs

* Added DOMManager

* Refactored frame managment

* Fixes

* Fixed concurrency issues

* Fixed unit tests

* Improved EventLoop api

* Some fixes

* Refactored event loop.

* Improved logic of initial page load

* Cleaned up

* Fixed linting issues

* Fixed dom.Manager.Close

* SOme works

* Fixes

* Removed fmt.Println statements

* Refactored WaitForNavigation

* Removed filter for e2e tests

* Made Cookies Measurable

* Made Cookies KeyedCollection

* Fixes after code review

* Updated e2e tests for iframes

* Fixed iframe lookup in e2e tests

* Added comments
2019-12-24 18:47:21 -05:00

99 lines
2.1 KiB
Go

package html
import (
"context"
"github.com/pkg/errors"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
type WaitNavigationParams struct {
TargetURL values.String
Timeout values.Int
}
// WAIT_NAVIGATION waits for a given page to navigate to a new url.
// Stops the execution until the navigation ends or operation times out.
// @param page (HTMLPage) - Target page.
// @param timeout (Int, optional) - Optional timeout. Default 5000 ms.
func WaitNavigation(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
}
doc, err := drivers.ToPage(args[0])
if err != nil {
return values.None, err
}
var params WaitNavigationParams
if len(args) > 1 {
p, err := parseWaitNavigationParams(args[1])
if err != nil {
return values.None, err
}
params = p
} else {
params = defaultWaitNavigationParams()
}
ctx, fn := waitTimeout(ctx, params.Timeout)
defer fn()
return values.None, doc.WaitForNavigation(ctx, params.TargetURL)
}
func parseWaitNavigationParams(arg core.Value) (WaitNavigationParams, error) {
params := defaultWaitNavigationParams()
err := core.ValidateType(arg, types.Int, types.Object)
if err != nil {
return params, err
}
if arg.Type() == types.Int {
params.Timeout = arg.(values.Int)
} else {
obj := arg.(*values.Object)
if v, exists := obj.Get("timeout"); exists {
err := core.ValidateType(v, types.Int)
if err != nil {
return params, errors.Wrap(err, "navigation parameters: timeout")
}
params.Timeout = v.(values.Int)
}
if v, exists := obj.Get("target"); exists {
err := core.ValidateType(v, types.String)
if err != nil {
return params, errors.Wrap(err, "navigation parameters: url")
}
params.TargetURL = v.(values.String)
}
}
return params, nil
}
func defaultWaitNavigationParams() WaitNavigationParams {
return WaitNavigationParams{
TargetURL: "",
Timeout: values.NewInt(drivers.DefaultWaitTimeout),
}
}