-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to check web vital scripts added
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
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |