Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Move startIteration to helpers.go
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 9, 2024
1 parent b478107 commit 7ea8e2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
39 changes: 39 additions & 0 deletions tests/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tests

import (
"testing"

"github.com/dop251/goja"

Check failure on line 6 in tests/helpers.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s prefix(github.com/grafana/xk6-browser) -s prefix(go.k6.io) -s default (gci)
"github.com/grafana/xk6-browser/browser"
"github.com/grafana/xk6-browser/k6ext/k6test"
"github.com/stretchr/testify/require"

Check failure on line 9 in tests/helpers.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s prefix(github.com/grafana/xk6-browser) -s prefix(go.k6.io) -s default (gci)
)

// startIteration will work with the event system to start chrome and
// (more importantly) set browser as the mapped browser instance which will
// force all tests that work with this to go through the mapping layer.
// This returns a cleanup function which should be deferred.
// The opts are passed to k6test.NewVU as is without any modification.
func startIteration(t *testing.T, opts ...any) (*k6test.VU, *goja.Runtime, *[]string, func()) {
t.Helper()

vu := k6test.NewVU(t, opts...)
rt := vu.Runtime()

mod := browser.New().NewModuleInstance(vu)
jsMod, ok := mod.Exports().Default.(*browser.JSModule)
require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default)

// Setting the mapped browser into the vu's goja runtime.
require.NoError(t, rt.Set("browser", jsMod.Browser))

// Setting log, which is used by the callers to assert that certain actions
// have been made.
var log []string
require.NoError(t, rt.Set("log", func(s string) { log = append(log, s) }))

vu.ActivateVU()
vu.StartIteration(t)

return vu, rt, &log, func() { t.Helper(); vu.EndIteration(t) }
}
31 changes: 0 additions & 31 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/xk6-browser/browser"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext/k6test"
)

type emulateMediaOpts struct {
Expand Down Expand Up @@ -811,35 +809,6 @@ func TestPageWaitForFunction(t *testing.T) {
})
}

// startIteration will work with the event system to start chrome and
// (more importantly) set browser as the mapped browser instance which will
// force all tests that work with this to go through the mapping layer.
// This returns a cleanup function which should be deferred.
// The opts are passed to k6test.NewVU as is without any modification.
func startIteration(t *testing.T, opts ...any) (*k6test.VU, *goja.Runtime, *[]string, func()) {
t.Helper()

vu := k6test.NewVU(t, opts...)
rt := vu.Runtime()

mod := browser.New().NewModuleInstance(vu)
jsMod, ok := mod.Exports().Default.(*browser.JSModule)
require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default)

// Setting the mapped browser into the vu's goja runtime.
require.NoError(t, rt.Set("browser", jsMod.Browser))

// Setting log, which is used by the callers to assert that certain actions
// have been made.
var log []string
require.NoError(t, rt.Set("log", func(s string) { log = append(log, s) }))

vu.ActivateVU()
vu.StartIteration(t)

return vu, rt, &log, func() { t.Helper(); vu.EndIteration(t) }
}

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

Expand Down

0 comments on commit 7ea8e2a

Please sign in to comment.