2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-20 01:10:18 +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
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// WaitClass waits for a class to appear on a given element.
|
|
|
|
// Stops the execution until the navigation ends or operation times out.
|
|
|
|
// @param docOrEl (HTMLDocument|HTMLElement) - Target document or element.
|
|
|
|
// @param selectorOrClass (String) - If document is passed, this param must represent an element selector.
|
|
|
|
// Otherwise target class.
|
|
|
|
// @param classOrTimeout (String|Int, optional) - If document is passed, this param must represent target class name.
|
|
|
|
// Otherwise timeout.
|
|
|
|
// @param timeout (Int, optional) - If document is passed, this param must represent timeout.
|
|
|
|
// Otherwise not passed.
|
2019-02-21 04:24:05 +02:00
|
|
|
func WaitClass(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
err := core.ValidateArgs(args, 2, 4)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// document or element
|
2019-02-20 01:10:18 +02:00
|
|
|
arg1 := args[0]
|
|
|
|
err = core.ValidateType(arg1, drivers.HTMLDocumentType, drivers.HTMLElementType)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// selector or class
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[1], types.String)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := values.NewInt(defaultTimeout)
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
// if a document is passed
|
|
|
|
if arg1.Type() == drivers.HTMLDocumentType {
|
2018-11-22 17:39:15 +02:00
|
|
|
// revalidate args with more accurate amount
|
|
|
|
err := core.ValidateArgs(args, 3, 4)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
// class
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[2], types.String)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
doc := arg1.(drivers.HTMLDocument)
|
2018-10-07 04:33:39 +02:00
|
|
|
selector := args[1].(values.String)
|
|
|
|
class := args[2].(values.String)
|
|
|
|
|
|
|
|
if len(args) == 4 {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[3], types.Int)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = args[3].(values.Int)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx, fn := waitTimeout(ctx, timeout)
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
return values.None, doc.WaitForClassBySelector(ctx, selector, class)
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
el := arg1.(drivers.HTMLElement)
|
|
|
|
class := args[1].(values.String)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
if len(args) == 3 {
|
|
|
|
err = core.ValidateType(args[2], types.Int)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
timeout = args[2].(values.Int)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
2019-02-20 01:10:18 +02:00
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx, fn := waitTimeout(ctx, timeout)
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
return values.None, el.WaitForClass(ctx, class)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|