Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor options to const #1044

Draft
wants to merge 41 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f62d724
Add a WaitForEventOptions type
ankur22 Sep 20, 2023
7221b56
Add the implementation to parse the options
ankur22 Sep 20, 2023
29aa69e
Refactor WaitForEvent to work with its parser
ankur22 Sep 20, 2023
8c734f4
Fix browserContext.waitForEvent's timeout
ankur22 Sep 20, 2023
1314be4
Update waitForEvent to clarify page support
ankur22 Sep 20, 2023
ff2b281
Update messaging around the other cases
ankur22 Sep 20, 2023
1020eef
Refactor waitForEvent to work with errors
ankur22 Sep 20, 2023
fb354e4
Fix parsing of waitForEvent options
ankur22 Sep 20, 2023
e8fec53
Update to allow nil predicate function
ankur22 Sep 20, 2023
3c37f9f
Refactor duplicate code into defer
ankur22 Sep 20, 2023
2e2e063
Update mapping to promisify waitForEvent
ankur22 Sep 20, 2023
aa5952b
Add happy path tests for waitForEvent
ankur22 Sep 20, 2023
c00acd5
Add failure cases to integration test
ankur22 Sep 21, 2023
0be19f3
Add an example script for waitForEvent
ankur22 Sep 20, 2023
4c096de
Add a new waitForEventType type
ankur22 Sep 21, 2023
d741d45
Refactor error message for incorrect event
ankur22 Sep 21, 2023
af81e9a
Update log message with correct method name
ankur22 Sep 21, 2023
bbfe8df
Refactor for early return
ankur22 Sep 21, 2023
232422c
fixup! Add a new waitForEventType type
ankur22 Sep 21, 2023
4f0b44a
fixup! Update log message with correct method name
ankur22 Sep 21, 2023
3d1f558
Refactor to early exit if opts doesn't exist
ankur22 Sep 21, 2023
855850a
Refactor comments to align with 80 chars line len
ankur22 Sep 21, 2023
b2d2503
Refactor test names to be less verbose
ankur22 Sep 21, 2023
8fd96c5
Add comments to test to bring clarity
ankur22 Sep 21, 2023
d8c52f9
Add a const for the timeout option
ankur22 Sep 21, 2023
4b8d60d
Add a const for the predicate option
ankur22 Sep 21, 2023
5e72dc1
Add const for noWaitAfter option
ankur22 Sep 21, 2023
5485c29
Add const for force option
ankur22 Sep 21, 2023
7fa2466
Add const for delay option
ankur22 Sep 21, 2023
6c8b776
Add const for omitBackground option
ankur22 Sep 21, 2023
0a46fac
Add const for type option
ankur22 Sep 21, 2023
9d037f3
Add const for quality option
ankur22 Sep 21, 2023
e51fbcf
Add const for path option
ankur22 Sep 21, 2023
e597d69
Add const for strict option
ankur22 Sep 21, 2023
7f8673e
Add const for referer option
ankur22 Sep 21, 2023
53d63ea
Add const for waitUntil option
ankur22 Sep 21, 2023
57467a7
Add const for polling option
ankur22 Sep 21, 2023
50791d6
Add const for url option
ankur22 Sep 21, 2023
71178ef
Add const for button option
ankur22 Sep 21, 2023
f06d53a
Add const for clickCount option
ankur22 Sep 21, 2023
90b47b3
Add const for modifiers option
ankur22 Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add the implementation to parse the options
This copies the parsing logic from the existing browserContext.waitFor
Event() into the new WaitForEventOptions type's parse method. It also
modifies it so that the data that is parsed is updated in the type and
during parsing it will return an error instead of panicking.
  • Loading branch information
ankur22 committed Sep 21, 2023
commit 7221b5635fde66a708b425f32f9d3140468ff341
28 changes: 28 additions & 0 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"reflect"
"time"

"github.com/grafana/xk6-browser/k6ext"
Expand Down Expand Up @@ -153,5 +154,32 @@ func NewWaitForEventOptions(defaultTimeout time.Duration) *WaitForEventOptions {
// It can parse only a callable predicate function or an object
// which contains a callable predicate function and a timeout.
func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate goja.Value) error {
var (
isCallable bool
)
if gojaValueExists(optsOrPredicate) {
switch optsOrPredicate.ExportType() {
case reflect.TypeOf(goja.Object{}):
rt := k6ext.Runtime(ctx)
opts := optsOrPredicate.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "predicate":
w.PredicateFn, isCallable = goja.AssertFunction(opts.Get(k))
if !isCallable {
return fmt.Errorf("predicate function is not callable")
}
case "timeout":
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
default:
w.PredicateFn, isCallable = goja.AssertFunction(optsOrPredicate)
if !isCallable {
return fmt.Errorf("predicate function is not callable")
}
}
}

return nil
}