1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-03 15:02:32 +02:00

Added delay randomization for inputs (#283)

* Added delay randomization for inputs

* Fixed formatting
This commit is contained in:
Tim Voronov 2019-04-12 21:05:11 -04:00 committed by GitHub
parent d8ef12c3f7
commit e23e73c1c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

View File

@ -1,6 +1,7 @@
.PHONY: build compile test e2e doc fmt lint vet release
.PHONY: build install compile test e2e doc fmt lint vet release
export GOPATH
export GO111MODULE=on
VERSION ?= $(shell git describe --tags --always --dirty)
RELEASE_VERSION ?= $(version)
@ -13,6 +14,9 @@ default: build
build: vet generate test compile
install:
go get
compile:
go build -v -o ${DIR_BIN}/ferret \
-ldflags "-X main.version=${VERSION}" \

View File

@ -529,9 +529,8 @@ func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.St
return values.False, nil
}
delayMs := time.Duration(delay)
time.Sleep(delayMs * time.Millisecond)
// Initial delay after focusing but before typing
time.Sleep(time.Duration(delay) * time.Millisecond)
for _, ch := range valStr {
for _, ev := range []string{"keyDown", "keyUp"} {
@ -540,9 +539,9 @@ func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.St
if err := doc.client.Input.DispatchKeyEvent(ctx, ke); err != nil {
return values.False, err
}
time.Sleep(delayMs * time.Millisecond)
}
time.Sleep(randomDuration(delay) * time.Millisecond)
}
return values.True, nil

View File

@ -12,6 +12,7 @@ import (
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
"github.com/mafredri/cdp"
@ -483,3 +484,10 @@ func normalizeCookieURL(url string) string {
return httpPrefix + url
}
func randomDuration(delay values.Int) time.Duration {
max, min := core.NumberBoundaries(float64(int64(delay)))
value := core.Random(max, min)
return time.Duration(int64(value))
}