2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 19:06:27 +02:00
|
|
|
|
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-09-07 07:59:32 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-07 04:33:39 +02:00
|
|
|
)
|
|
|
|
|
2019-09-07 20:03:17 +02:00
|
|
|
// CLICK dispatches click event on a given element
|
2019-06-19 23:58:56 +02:00
|
|
|
// @param source (Open | GetElement) - Event source.
|
2019-09-07 07:59:32 +02:00
|
|
|
// @param selectorOrCount (String | Int, optional) - Optional selector or count of clicks.
|
|
|
|
// @param count (Int, optional) - Optional count of clicks.
|
2019-02-21 04:24:05 +02:00
|
|
|
func Click(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2019-09-07 07:59:32 +02:00
|
|
|
err := core.ValidateArgs(args, 1, 3)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
2019-08-25 02:26:27 +02:00
|
|
|
el, err := drivers.ToElement(args[0])
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-08-25 02:26:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-08-25 02:26:27 +02:00
|
|
|
// CLICK(elOrDoc)
|
|
|
|
if len(args) == 1 {
|
2019-09-07 07:59:32 +02:00
|
|
|
return values.True, el.Click(ctx, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 2 {
|
|
|
|
err := core.ValidateType(args[1], types.String, types.Int)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[1].Type() == types.String {
|
|
|
|
selector := values.ToString(args[1])
|
|
|
|
exists, err := el.ExistsBySelector(ctx, selector)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists, el.ClickBySelector(ctx, selector, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.True, el.Click(ctx, values.ToInt(args[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = core.ValidateType(args[1], types.String)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = core.ValidateType(args[2], types.Int)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CLICK(doc, selector)
|
2019-07-26 19:22:06 +02:00
|
|
|
selector := values.ToString(args[1])
|
2019-08-25 02:26:27 +02:00
|
|
|
exists, err := el.ExistsBySelector(ctx, selector)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:59:32 +02:00
|
|
|
count := values.ToInt(args[2])
|
|
|
|
|
|
|
|
return exists, el.ClickBySelector(ctx, selector, count)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|