1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-21 21:47:43 +02:00
ferret/pkg/stdlib/html/find_frames.go
Tim Voronov e6dd5689b4
Bugfix/e2e tests (#648)
* Fixed logger level

* Fixed WAITFOR EVENT parser

* Added tracing to Network Manager

* Updated logging

* Swtitched to value type of logger

* Added tracing

* Increased websocket maximum buffer size

* Ignore unimportant error message

* Added support of new CDP API for layouts

* Switched to value type of logger

* Added log level

* Fixed early context cancellation

* Updated example of 'click' action

* Switched to val for elements lookup

* Fixed unit tests

* Refactored 'eval' module

* Fixed SetStyle eval expression

* Fixed style deletion

* Updated logic of setting multiple styles
2021-09-02 11:09:48 -04:00

64 lines
1.3 KiB
Go

package html
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"regexp"
)
// FRAMES finds HTML frames by a given property selector.
// Returns an empty array if frames not found.
// @param {HTMLPage} page - HTML page.
// @param {String} property - Property selector.
// @param {String} exp - Regular expression to match property value.
// @return {HTMLDocument[]} - Returns an array of found HTML frames.
func Frames(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 3, 3)
if err != nil {
return values.None, err
}
page, err := drivers.ToPage(args[0])
if err != nil {
return values.None, err
}
frames, err := page.GetFrames(ctx)
if err != nil {
return values.None, err
}
propName := values.ToString(args[1])
matcher, err := regexp.Compile(values.ToString(args[2]).String())
if err != nil {
return values.None, err
}
result, _ := frames.Find(func(value core.Value, idx int) bool {
doc, e := drivers.ToDocument(value)
if e != nil {
err = e
return false
}
currentPropValue, e := doc.GetIn(ctx, []core.Value{propName})
if e != nil {
err = e
return false
}
return matcher.MatchString(currentPropValue.String())
})
return result, err
}