2018-10-06 22:33:39 -04:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-19 17:58:56 -04:00
|
|
|
|
2019-03-06 21:52:41 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-10-06 22:33:39 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 12:31:18 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-06 22:33:39 -04:00
|
|
|
)
|
|
|
|
|
2019-09-07 14:03:17 -04:00
|
|
|
// WAIT_ELEMENT waits for element to appear in the DOM.
|
2018-10-14 20:06:27 +03:00
|
|
|
// Stops the execution until it finds an element or operation times out.
|
2020-08-07 21:49:29 -04:00
|
|
|
// @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
|
|
|
|
// @param {String} selector - Target element's selector.
|
|
|
|
// @param {Int} [timeout=5000] - Wait timeout.
|
2019-02-20 21:24:05 -05:00
|
|
|
func WaitElement(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2019-03-06 21:52:41 -05:00
|
|
|
return waitElementWhen(ctx, args, drivers.WaitEventPresence)
|
|
|
|
}
|
|
|
|
|
2019-09-07 14:03:17 -04:00
|
|
|
// WAIT_NO_ELEMENT waits for element to disappear in the DOM.
|
2019-03-06 21:52:41 -05:00
|
|
|
// Stops the execution until it does not find an element or operation times out.
|
2020-08-07 21:49:29 -04:00
|
|
|
// @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
|
|
|
|
// @param {String} selector - Target element's selector.
|
|
|
|
// @param {Int} [timeout=5000] - Wait timeout.
|
2019-03-06 21:52:41 -05:00
|
|
|
func WaitNoElement(ctx context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
return waitElementWhen(ctx, args, drivers.WaitEventAbsence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitElementWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) {
|
2018-10-06 22:33:39 -04:00
|
|
|
err := core.ValidateArgs(args, 2, 3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 17:58:56 -04:00
|
|
|
doc, err := drivers.ToDocument(args[0])
|
2019-02-19 18:10:18 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:33:39 -04:00
|
|
|
selector := args[1].String()
|
2019-09-05 12:17:22 -04:00
|
|
|
timeout := values.NewInt(drivers.DefaultWaitTimeout)
|
2018-10-06 22:33:39 -04:00
|
|
|
|
|
|
|
if len(args) > 2 {
|
2019-02-13 12:31:18 -05:00
|
|
|
err = core.ValidateType(args[2], types.Int)
|
2018-10-06 22:33:39 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = args[2].(values.Int)
|
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
ctx, fn := waitTimeout(ctx, timeout)
|
|
|
|
defer fn()
|
|
|
|
|
2019-03-06 21:52:41 -05:00
|
|
|
return values.None, doc.WaitForElement(ctx, values.NewString(selector), when)
|
2018-10-06 22:33:39 -04:00
|
|
|
}
|