Skip to content

Commit

Permalink
Refactor browser module test to run a fake cmd (#3261)
Browse files Browse the repository at this point in the history
* Refactor browser module test to run a fake cmd

Instead of relying on a chrome instance being present on the dev/ci
machine to verify that the correct browser options do indeed start
a browser process and run the browser tests, we will instead rely on
the browser module trying to start a fake command and ensure that the
error message verifies that it attempted to start the fake command.

* Fix expectedError for Windows test runs
  • Loading branch information
ankur22 authored Aug 9, 2023
1 parent 9598ef5 commit 8fa0e6b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2164,12 +2164,27 @@ func TestBrowserPermissions(t *testing.T) {
expectedExitCode exitcodes.ExitCode
expectedError string
}{
// When we do not supply the correct browser options,
// we expect that the test iteration will stop and not
// attempt to start a chrome instance.
{
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",
},
// When we do supply the correct browser options,
// we expect that the browser module will start
// the a chrome instance and work with the browser
// APIs defined in the test script.
//
// We do not actually want to rely on having chrome
// installed on dev/ci machines, and all we need to
// verify is that the browser module does try to exec
// a command when the correct browser options are supplied.
// This test will try to run a "fake" command and we
// expect that the test will fail when attempting to
// run that command.
{
name: "browser option set",
options: `export const options = {
Expand All @@ -2184,8 +2199,8 @@ func TestBrowserPermissions(t *testing.T) {
},
},
}`,
expectedExitCode: 0,
expectedError: "",
expectedExitCode: 108,
expectedError: "error building browser on IterStart: launching browser: exec: \"k6-browser-fake-cmd\": executable file not found",
},
}

Expand All @@ -2204,14 +2219,10 @@ func TestBrowserPermissions(t *testing.T) {
`, tt.options)

ts := getSingleFileTestState(t, script, []string{}, tt.expectedExitCode)
ts.Env["K6_BROWSER_EXECUTABLE_PATH"] = "k6-browser-fake-cmd"
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)
})
}
Expand Down

0 comments on commit 8fa0e6b

Please sign in to comment.