From 8fa0e6b9a8b63f430df34047e4393b281ff9ee30 Mon Sep 17 00:00:00 2001
From: Ankur <ankur.agarwal@grafana.com>
Date: Wed, 9 Aug 2023 11:42:27 +0100
Subject: [PATCH] Refactor browser module test to run a fake cmd (#3261)

* 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
---
 cmd/tests/cmd_run_test.go | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/cmd/tests/cmd_run_test.go b/cmd/tests/cmd_run_test.go
index 459afcfd3b8..a6df776bade 100644
--- a/cmd/tests/cmd_run_test.go
+++ b/cmd/tests/cmd_run_test.go
@@ -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 = {
@@ -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",
 		},
 	}
 
@@ -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)
 		})
 	}