Skip to content

Commit

Permalink
Add test to check web vital scripts added
Browse files Browse the repository at this point in the history
This test ensures that the embedded web vital scripts have been added
to the browser contexts internal evaluateOnNewDocumentSources slice.
It does not check whether this has then been applied to a page.
  • Loading branch information
ankur22 committed Mar 28, 2023
1 parent 54c094d commit e4d42e3
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions common/browser_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestNewBrowserContext(t *testing.T) {
t.Run("add_web_vital_js_scripts_to_context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
logger := log.NewNullLogger()
b := newBrowser(ctx, cancel, nil, NewLaunchOptions(), logger)

vu := k6test.NewVU(t)
ctx = k6ext.WithVU(ctx, vu)

bc, err := NewBrowserContext(ctx, b, "some-id", nil, nil)
require.NoError(t, err)

webVitalIIFEScriptFound := false
webVitalInitScriptFound := false
for _, script := range bc.evaluateOnNewDocumentSources {
switch script {
case js.WebVitalIIFEScript:
webVitalIIFEScriptFound = true
case js.WebVitalInitScript:
webVitalInitScriptFound = true
default:
assert.Fail(t, "script is neither WebVitalIIFEScript nor WebVitalInitScript")
}
}

assert.True(t, webVitalIIFEScriptFound, "WebVitalIIFEScript was not initialized in the context")
assert.True(t, webVitalInitScriptFound, "WebVitalInitScript was not initialized in the context")
})
}

0 comments on commit e4d42e3

Please sign in to comment.