Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Renamed DOCUMENT to PAGE

* Added PageLoadParams

* Added PageLoadParams

* Renamed LoadPageParams -> PageLoadParams

* Added support for context.Done() (MontFerret#201)

* Bug/MontFerret#189 operators precedence (MontFerret#202)

* Fixed math operators precedence

* Fixed logical operators precedence

* Fixed array operator

* Added support for parentheses to enforce a different operator evaluation order

* Feature/MontFerret#200 drivers (MontFerret#209)

* Added new interfaces

* Renamed dynamic to cdp driver

* Renamed drivers

* Added ELEMENT_EXISTS function (MontFerret#210)

* Renamed back PAGE to DOCUMENT (MontFerret#211)

* Added Getter and Setter interfaces
  • Loading branch information
ziflex authored and Владимир Фетисов committed Apr 10, 2019
1 parent 0b00a1a commit 207d9d0
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 584 deletions.
64 changes: 18 additions & 46 deletions pkg/drivers/cdp/events/wait.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package events

import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"time"

"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/mafredri/cdp"
"time"
)

type (
Function func(ctx context.Context) (core.Value, error)

Function func() (core.Value, error)
WaitTask struct {
fun Function
timeout time.Duration
polling time.Duration
}
)
Expand All @@ -23,31 +22,39 @@ const DefaultPolling = time.Millisecond * time.Duration(200)

func NewWaitTask(
fun Function,
timeout time.Duration,
polling time.Duration,
) *WaitTask {
return &WaitTask{
fun,
timeout,
polling,
}
}

func (task *WaitTask) Run(ctx context.Context) (core.Value, error) {
func (task *WaitTask) Run() (core.Value, error) {
timer := time.NewTimer(task.timeout)

for {
select {
case <-ctx.Done():
case <-timer.C:
return values.None, core.ErrTimeout
default:
out, err := task.fun(ctx)
out, err := task.fun()

// expression failed
// terminating
if err != nil {
timer.Stop()

return values.None, err
}

// output is not empty
// terminating
if out != values.None {
timer.Stop()

return out, nil
}

Expand All @@ -60,54 +67,19 @@ func (task *WaitTask) Run(ctx context.Context) (core.Value, error) {
func NewEvalWaitTask(
client *cdp.Client,
predicate string,
timeout time.Duration,
polling time.Duration,
) *WaitTask {
return NewWaitTask(
func(ctx context.Context) (core.Value, error) {
func() (core.Value, error) {
return eval.Eval(
ctx,
client,
predicate,
true,
false,
)
},
timeout,
polling,
)
}

func NewValueWaitTask(
when drivers.WaitEvent,
value core.Value,
getter Function,
polling time.Duration,
) *WaitTask {
return &WaitTask{
func(ctx context.Context) (core.Value, error) {
current, err := getter(ctx)

if err != nil {
return values.None, err
}

if when == drivers.WaitEventPresence {
// Values appeared, exit
if current.Compare(value) == 0 {
// The value does not really matter if it's not None
// None indicates that operation needs to be repeated
return values.True, nil
}
} else {
// Value disappeared, exit
if current.Compare(value) != 0 {
// The value does not really matter if it's not None
// None indicates that operation needs to be repeated
return values.True, nil
}
}

return values.None, nil
},
polling,
}
}
31 changes: 7 additions & 24 deletions pkg/drivers/cdp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cdp

type (
Options struct {
Name string
Proxy string
UserAgent string
Address string
KeepCookies bool
proxy string
userAgent string
address string
}

Option func(opts *Options)
Expand All @@ -16,8 +14,7 @@ const DefaultAddress = "http://127.0.0.1:9222"

func newOptions(setters []Option) *Options {
opts := new(Options)
opts.Name = DriverName
opts.Address = DefaultAddress
opts.address = DefaultAddress

for _, setter := range setters {
setter(opts)
Expand All @@ -28,32 +25,18 @@ func newOptions(setters []Option) *Options {

func WithAddress(address string) Option {
return func(opts *Options) {
if address != "" {
opts.Address = address
}
opts.address = address
}
}

func WithProxy(address string) Option {
return func(opts *Options) {
opts.Proxy = address
opts.proxy = address
}
}

func WithUserAgent(value string) Option {
return func(opts *Options) {
opts.UserAgent = value
}
}

func WithKeepCookies() Option {
return func(opts *Options) {
opts.KeepCookies = true
}
}

func WithCustomName(name string) Option {
return func(opts *Options) {
opts.Name = name
opts.userAgent = value
}
}
Loading

0 comments on commit 207d9d0

Please sign in to comment.