1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-06 08:39:09 +02:00

#26 Updated INPUT

This commit is contained in:
Tim Voronov
2018-09-27 21:41:41 -04:00
parent 7597497acb
commit a8813f8b03
7 changed files with 113 additions and 38 deletions

View File

@@ -17,13 +17,9 @@ The final for loop filters out empty elements that might be because of inaccurat
```aql ```aql
LET g = DOCUMENT("https://www.google.com/", true) LET g = DOCUMENT("https://www.google.com/", true)
LET inputBox = ELEMENT(g, 'input[name="q"]')
INPUT(inputBox, "ferret") INPUT(g, 'input[name="q"]', "ferret")
CLICK(g, 'input[name="btnK"]')
LET searchBtn = ELEMENT(g, 'input[name="btnK"]')
CLICK(searchBtn)
WAIT_NAVIGATION(g) WAIT_NAVIGATION(g)

View File

@@ -1,11 +1,7 @@
LET g = DOCUMENT("https://www.google.com/", true) LET g = DOCUMENT("https://www.google.com/", true)
LET inputBox = ELEMENT(g, 'input[name="q"]')
INPUT(inputBox, "ferret") INPUT(g, 'input[name="q"]', "ferret")
CLICK(g, 'input[name="btnK"]')
LET searchBtn = ELEMENT(g, 'input[name="btnK"]')
CLICK(searchBtn)
WAIT_NAVIGATION(g) WAIT_NAVIGATION(g)

9
docs/examples/test.fql Normal file
View File

@@ -0,0 +1,9 @@
LET g = DOCUMENT("https://www.google.com/", true)
INPUT(ELEMENT(g, 'input[name="q"]'), "ferret")
CLICK(g, 'input[name="btnK"]')
WAIT_NAVIGATION(g)
RETURN 1

View File

@@ -36,25 +36,25 @@ func Click(_ context.Context, args ...core.Value) (core.Value, error) {
} }
return el.Click() return el.Click()
} else {
// CLICK(doc, selector)
arg1 := args[0]
selector := args[1].String()
err = core.ValidateType(arg1, core.HtmlDocumentType)
if err != nil {
return values.None, err
}
doc, ok := arg1.(*dynamic.HtmlDocument)
if !ok {
return values.False, core.Error(core.ErrInvalidType, "expected dynamic document")
}
return doc.ClickBySelector(values.NewString(selector))
} }
// CLICK(doc, selector)
arg1 := args[0]
selector := args[1].String()
err = core.ValidateType(arg1, core.HtmlDocumentType)
if err != nil {
return values.None, err
}
doc, ok := arg1.(*dynamic.HtmlDocument)
if !ok {
return values.False, core.Error(core.ErrInvalidType, "expected dynamic document")
}
return doc.ClickBySelector(values.NewString(selector))
} }
/* /*
@@ -92,6 +92,13 @@ func Navigate(_ context.Context, args ...core.Value) (core.Value, error) {
return values.None, doc.Navigate(args[1].(values.String)) return values.None, doc.Navigate(args[1].(values.String))
} }
/*
* Sends a value to an underlying input element.
* @param source (Document | Element) - Event target.
* @param valueOrSelector (String) - Selector or a value.
* @param value (String) - Target value.
* @returns (Boolean) - Returns true if an element was found.
*/
func Input(_ context.Context, args ...core.Value) (core.Value, error) { func Input(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3) err := core.ValidateArgs(args, 2, 3)
@@ -115,8 +122,36 @@ func Input(_ context.Context, args ...core.Value) (core.Value, error) {
return values.False, core.Error(core.ErrInvalidType, "expected dynamic element") return values.False, core.Error(core.ErrInvalidType, "expected dynamic element")
} }
return values.None, el.Input(args[1], values.NewInt(100)) err = el.Input(args[1])
if err != nil {
return values.False, err
}
return values.True, nil
} }
return values.None, nil arg1 := args[0]
err = core.ValidateType(arg1, core.HtmlDocumentType)
if err != nil {
return values.False, err
}
arg2 := args[1]
err = core.ValidateType(arg2, core.StringType)
if err != nil {
return values.False, err
}
doc, ok := arg1.(*dynamic.HtmlDocument)
if !ok {
return values.False, core.Error(core.ErrInvalidType, "expected dynamic document")
}
return doc.InputBySelector(arg2.(values.String), args[2])
} }

View File

@@ -327,7 +327,7 @@ func (doc *HtmlDocument) ClickBySelector(selector values.String) (values.Boolean
res, err := eval.Eval( res, err := eval.Eval(
doc.client, doc.client,
fmt.Sprintf(` fmt.Sprintf(`
var el = document.querySelector("%s"); var el = document.querySelector(%s);
if (el == null) { if (el == null) {
return false; return false;
@@ -337,7 +337,43 @@ func (doc *HtmlDocument) ClickBySelector(selector values.String) (values.Boolean
el.dispatchEvent(evt); el.dispatchEvent(evt);
return true; return true;
`, selector), `, eval.ParamString(selector.String())),
true,
false,
)
if err != nil {
return values.False, err
}
if res.Type() == core.BooleanType {
return res.(values.Boolean), nil
}
return values.False, nil
}
func (doc *HtmlDocument) InputBySelector(selector values.String, value core.Value) (values.Boolean, error) {
res, err := eval.Eval(
doc.client,
fmt.Sprintf(
`
var el = document.querySelector(%s);
if (el == null) {
return false;
}
var evt = new window.Event('input', { bubbles: true });
el.value = %s
el.dispatchEvent(evt);
return true;
`,
eval.ParamString(selector.String()),
eval.ParamString(value.String()),
),
true, true,
false, false,
) )
@@ -357,14 +393,14 @@ func (doc *HtmlDocument) WaitForSelector(selector values.String, timeout values.
task := events.NewWaitTask( task := events.NewWaitTask(
doc.client, doc.client,
fmt.Sprintf(` fmt.Sprintf(`
el = document.querySelector("%s"); el = document.querySelector(%s);
if (el != null) { if (el != null) {
return true; return true;
} }
return null; return null;
`, selector), `, eval.ParamString(selector.String())),
time.Millisecond*time.Duration(timeout), time.Millisecond*time.Duration(timeout),
events.DefaultPolling, events.DefaultPolling,
) )

View File

@@ -5,7 +5,6 @@ import (
"context" "context"
"crypto/sha512" "crypto/sha512"
"encoding/json" "encoding/json"
"fmt"
"github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values" "github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/html/driver/common" "github.com/MontFerret/ferret/pkg/stdlib/html/driver/common"
@@ -376,7 +375,7 @@ func (el *HtmlElement) Click() (values.Boolean, error) {
return events.DispatchEvent(ctx, el.client, el.id, "click") return events.DispatchEvent(ctx, el.client, el.id, "click")
} }
func (el *HtmlElement) Input(value core.Value, timeout values.Int) error { func (el *HtmlElement) Input(value core.Value) error {
ctx, cancel := contextWithTimeout() ctx, cancel := contextWithTimeout()
defer cancel() defer cancel()

View File

@@ -14,6 +14,10 @@ func PrepareEval(exp string) string {
return fmt.Sprintf("((function () {%s})())", exp) return fmt.Sprintf("((function () {%s})())", exp)
} }
func ParamString(param string) string {
return "`" + param + "`"
}
func Eval(client *cdp.Client, exp string, ret bool, async bool) (core.Value, error) { func Eval(client *cdp.Client, exp string, ret bool, async bool) (core.Value, error) {
args := runtime. args := runtime.
NewEvaluateArgs(PrepareEval(exp)). NewEvaluateArgs(PrepareEval(exp)).