2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-19 23:58:56 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-07 04:33:39 +02:00
|
|
|
)
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
// WaitNavigation waits for a given page to navigate to a new url.
|
2018-10-14 19:06:27 +02:00
|
|
|
// Stops the execution until the navigation ends or operation times out.
|
2019-06-19 23:58:56 +02:00
|
|
|
// @param page (HTMLPage) - Target page.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @param timeout (Int, optional) - Optional timeout. Default 5000 ms.
|
2019-02-21 04:24:05 +02:00
|
|
|
func WaitNavigation(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
doc, err := drivers.ToPage(args[0])
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:17:22 +02:00
|
|
|
timeout := values.NewInt(drivers.DefaultWaitTimeout)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if len(args) > 1 {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[1], types.Int)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = args[1].(values.Int)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx, fn := waitTimeout(ctx, timeout)
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
return values.None, doc.WaitForNavigation(ctx)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|