mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-29 22:17:29 +02:00
Bugfix/#399 navigation (#432)
* Refactored networking * Some work * Added event loop * Renamed EventHandler to Handler * wip * Removed console logs * Added DOMManager * Refactored frame managment * Fixes * Fixed concurrency issues * Fixed unit tests * Improved EventLoop api * Some fixes * Refactored event loop. * Improved logic of initial page load * Cleaned up * Fixed linting issues * Fixed dom.Manager.Close * SOme works * Fixes * Removed fmt.Println statements * Refactored WaitForNavigation * Removed filter for e2e tests * Made Cookies Measurable * Made Cookies KeyedCollection * Fixes after code review * Updated e2e tests for iframes * Fixed iframe lookup in e2e tests * Added comments
This commit is contained in:
@@ -26,8 +26,8 @@ func CookieDel(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
}
|
||||
|
||||
inputs := args[1:]
|
||||
var currentCookies *values.Array
|
||||
cookies := make([]drivers.HTTPCookie, 0, len(inputs))
|
||||
var currentCookies drivers.HTTPCookies
|
||||
cookies := make(drivers.HTTPCookies)
|
||||
|
||||
for _, c := range inputs {
|
||||
switch cookie := c.(type) {
|
||||
@@ -42,23 +42,18 @@ func CookieDel(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
currentCookies = current
|
||||
}
|
||||
|
||||
found, isFound := currentCookies.Find(func(value core.Value, _ int) bool {
|
||||
cv := value.(drivers.HTTPCookie)
|
||||
|
||||
return cv.Name == cookie.String()
|
||||
})
|
||||
found, isFound := currentCookies[cookie.String()]
|
||||
|
||||
if isFound {
|
||||
cookies = append(cookies, found.(drivers.HTTPCookie))
|
||||
cookies[cookie.String()] = found
|
||||
}
|
||||
|
||||
case drivers.HTTPCookie:
|
||||
cookies = append(cookies, cookie)
|
||||
|
||||
cookies[cookie.Name] = cookie
|
||||
default:
|
||||
return values.None, core.TypeError(c.Type(), types.String, drivers.HTTPCookieType)
|
||||
}
|
||||
}
|
||||
|
||||
return values.None, page.DeleteCookies(ctx, cookies...)
|
||||
return values.None, page.DeleteCookies(ctx, cookies)
|
||||
}
|
||||
|
||||
@@ -39,15 +39,11 @@ func CookieGet(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
found, _ := cookies.Find(func(value core.Value, _ int) bool {
|
||||
cookie, ok := value.(drivers.HTTPCookie)
|
||||
cookie, found := cookies[name.String()]
|
||||
|
||||
if !ok {
|
||||
return ok
|
||||
}
|
||||
if found {
|
||||
return cookie, nil
|
||||
}
|
||||
|
||||
return cookie.Name == name.String()
|
||||
})
|
||||
|
||||
return found, nil
|
||||
return values.None, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func CookieSet(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
cookies := make([]drivers.HTTPCookie, 0, len(args)-1)
|
||||
cookies := make(drivers.HTTPCookies)
|
||||
|
||||
for _, c := range args[1:] {
|
||||
cookie, err := parseCookie(c)
|
||||
@@ -33,8 +33,8 @@ func CookieSet(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
cookies = append(cookies, cookie)
|
||||
cookies[cookie.Name] = cookie
|
||||
}
|
||||
|
||||
return values.None, page.SetCookies(ctx, cookies...)
|
||||
return values.None, page.SetCookies(ctx, cookies)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package html
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
@@ -9,6 +10,11 @@ import (
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
type WaitNavigationParams struct {
|
||||
TargetURL values.String
|
||||
Timeout values.Int
|
||||
}
|
||||
|
||||
// WAIT_NAVIGATION waits for a given page to navigate to a new url.
|
||||
// Stops the execution until the navigation ends or operation times out.
|
||||
// @param page (HTMLPage) - Target page.
|
||||
@@ -26,20 +32,67 @@ func WaitNavigation(ctx context.Context, args ...core.Value) (core.Value, error)
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
timeout := values.NewInt(drivers.DefaultWaitTimeout)
|
||||
var params WaitNavigationParams
|
||||
|
||||
if len(args) > 1 {
|
||||
err = core.ValidateType(args[1], types.Int)
|
||||
p, err := parseWaitNavigationParams(args[1])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
timeout = args[1].(values.Int)
|
||||
params = p
|
||||
} else {
|
||||
params = defaultWaitNavigationParams()
|
||||
}
|
||||
|
||||
ctx, fn := waitTimeout(ctx, timeout)
|
||||
ctx, fn := waitTimeout(ctx, params.Timeout)
|
||||
defer fn()
|
||||
|
||||
return values.None, doc.WaitForNavigation(ctx)
|
||||
return values.None, doc.WaitForNavigation(ctx, params.TargetURL)
|
||||
}
|
||||
|
||||
func parseWaitNavigationParams(arg core.Value) (WaitNavigationParams, error) {
|
||||
params := defaultWaitNavigationParams()
|
||||
err := core.ValidateType(arg, types.Int, types.Object)
|
||||
|
||||
if err != nil {
|
||||
return params, err
|
||||
}
|
||||
|
||||
if arg.Type() == types.Int {
|
||||
params.Timeout = arg.(values.Int)
|
||||
|
||||
} else {
|
||||
obj := arg.(*values.Object)
|
||||
|
||||
if v, exists := obj.Get("timeout"); exists {
|
||||
err := core.ValidateType(v, types.Int)
|
||||
|
||||
if err != nil {
|
||||
return params, errors.Wrap(err, "navigation parameters: timeout")
|
||||
}
|
||||
|
||||
params.Timeout = v.(values.Int)
|
||||
}
|
||||
|
||||
if v, exists := obj.Get("target"); exists {
|
||||
err := core.ValidateType(v, types.String)
|
||||
|
||||
if err != nil {
|
||||
return params, errors.Wrap(err, "navigation parameters: url")
|
||||
}
|
||||
|
||||
params.TargetURL = v.(values.String)
|
||||
}
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func defaultWaitNavigationParams() WaitNavigationParams {
|
||||
return WaitNavigationParams{
|
||||
TargetURL: "",
|
||||
Timeout: values.NewInt(drivers.DefaultWaitTimeout),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user