2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// WaitElement waits for element to appear in the DOM.
|
|
|
|
// Stops the execution until it finds an element or operation times out.
|
2019-02-20 01:10:18 +02:00
|
|
|
// @param doc (HTMLDocument) - Driver HTMLDocument.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @param selector (String) - Target element's selector.
|
|
|
|
// @param timeout (Int, optional) - Optional timeout. Default 5000 ms.
|
2019-02-21 04:24:05 +02:00
|
|
|
func WaitElement(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
err := core.ValidateArgs(args, 2, 3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
doc, err := toDocument(args[0])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
selector := args[1].String()
|
|
|
|
timeout := values.NewInt(defaultTimeout)
|
|
|
|
|
|
|
|
if len(args) > 2 {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[2], types.Int)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = args[2].(values.Int)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx, fn := waitTimeout(ctx, timeout)
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
return values.None, doc.WaitForSelector(ctx, values.NewString(selector))
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|