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"
|
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Click dispatches click event on a given element
|
2019-06-19 23:58:56 +02:00
|
|
|
// @param source (Open | GetElement) - Event source.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @param selector (String, optional) - Optional selector. Only used when a document instance is passed.
|
2019-02-21 04:24:05 +02:00
|
|
|
func Click(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.False, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CLICK(el)
|
|
|
|
if len(args) == 1 {
|
2019-06-19 23:58:56 +02:00
|
|
|
el, err := drivers.ToElement(args[0])
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.Click(ctx)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CLICK(doc, selector)
|
2019-06-19 23:58:56 +02:00
|
|
|
doc, err := drivers.ToDocument(args[0])
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
return values.False, err
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
selector := args[1].String()
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
return doc.ClickBySelector(ctx, values.NewString(selector))
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|