Skip to content

Commit

Permalink
Merge branch 'feature/384-setInputFiles' into feature/384-setInputFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Mar 6, 2024
2 parents 2cca8f9 + 57f169e commit bf4a59d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
11 changes: 9 additions & 2 deletions common/execution_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (

const evaluationScriptURL = "__xk6_browser_evaluation_script__"

// This error code originates from chromium.
const devToolsServerErrorCode = -32000

var sourceURLRegex = regexp.MustCompile(`^(?s)[\040\t]*//[@#] sourceURL=\s*(\S*?)\s*$`)

type executionWorld string
Expand Down Expand Up @@ -206,9 +209,13 @@ func (e *ExecutionContext) eval(
)
if remoteObject, exceptionDetails, err = action.Do(cdp.WithExecutor(apiCtx, e.session)); err != nil {
var cdpe *cdproto.Error
if errors.As(err, &cdpe) && cdpe.Code == -32000 {
err = errors.New("execution context changed; most likely because of a navigation")
if errors.As(err, &cdpe) && cdpe.Code == devToolsServerErrorCode {
// By creating a new error instead of reusing it, we're removing the
// chromium specific error code.
return nil, errors.New(cdpe.Message)
}

e.logger.Warn("ExecutionContext:eval", "Unexpected DevTools server error: %v", err)
return nil, err
}
if exceptionDetails != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/mccutchen/go-httpbin v1.1.2-0.20190116014521-c5cb2f4802fa
github.com/mstoykov/k6-taskqueue-lib v0.1.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
go.k6.io/k6 v0.49.1-0.20240213133040-b5a6febd5638
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand Down
105 changes: 105 additions & 0 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,111 @@ func TestPageEvaluate(t *testing.T) {
})
}

func TestPageEvaluateMapping(t *testing.T) {
t.Parallel()

tests := []struct {
name string
script string
want any
}{
{
name: "arrow",
script: "() => 0",
want: 0,
},
{
name: "full_func",
script: "function() {return 1}",
want: 1,
},
{
name: "arrow_func_no_return",
script: "() => {2}",
want: goja.Null(),
},
{
name: "full_func_no_return",
script: "function() {3}",
want: goja.Null(),
},
{
name: "async_func",
script: "async function() {return 4}",
want: 4,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_, rt, _, cleanUp := startIteration(t)
defer cleanUp()

// Test script as non string input
got, err := rt.RunString(fmt.Sprintf(`
const p = browser.newPage()
p.evaluate(%s)
`, tt.script))
assert.NoError(t, err)
assert.Equal(t, rt.ToValue(tt.want), got)

// Test script as string input
got, err = rt.RunString(fmt.Sprintf(`
p.evaluate("%s")
`, tt.script))
assert.NoError(t, err)
assert.Equal(t, rt.ToValue(tt.want), got)
})
}
}

func TestPageEvaluateMappingError(t *testing.T) {
t.Parallel()

tests := []struct {
name string
script string
wantErr string
}{
{
name: "invalid",
script: "5",
wantErr: "Given expression does not evaluate to a function",
},
{
name: "invalid_with_brackets",
script: "(6)",
wantErr: "Given expression does not evaluate to a function",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_, rt, _, cleanUp := startIteration(t)
defer cleanUp()

// Test script as non string input
_, err := rt.RunString(fmt.Sprintf(`
const p = browser.newPage()
p.evaluate(%s)
`, tt.script))
assert.ErrorContains(t, err, tt.wantErr)

// Test script as string input
_, err = rt.RunString(fmt.Sprintf(`
p.evaluate("%s")
`, tt.script))
assert.ErrorContains(t, err, tt.wantErr)
})
}
}

func TestPageGoto(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit bf4a59d

Please sign in to comment.