Skip to content

Commit

Permalink
Add e2e test for browser module in k6
Browse files Browse the repository at this point in the history
This test ensures that an error occurs and the test ends when the
browser module is imported but the options are missing, otherwise
when setup correctly the browser test runs.
  • Loading branch information
ankur22 committed Aug 7, 2023
1 parent b1fbe53 commit 5eee08d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2154,3 +2154,65 @@ func BenchmarkRunEvents(b *testing.B) {
}
}
}

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

tests := []struct {
name string
options string
expectedExitCode exitcodes.ExitCode
expectedError string
}{
{
name: "browser option not set",
options: "",
expectedExitCode: 0,
expectedError: "GoError: browser not found in registry. make sure to set browser type option in scenario definition in order to use the browser module",
},
{
name: "browser option set",
options: `export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
}`,
expectedExitCode: 0,
expectedError: "",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
script := fmt.Sprintf(`
import { browser } from 'k6/experimental/browser';
%s
export default function() {
browser.isConnected();
};
`, tt.options)

ts := getSingleFileTestState(t, script, []string{}, tt.expectedExitCode)
cmd.ExecuteWithGlobalState(ts.GlobalState)
loglines := ts.LoggerHook.Drain()

if tt.expectedError == "" {
require.Len(t, loglines, 0)
return
}

assert.Contains(t, loglines[0].Message, tt.expectedError)
})
}
}

0 comments on commit 5eee08d

Please sign in to comment.