diff --git a/js/modules/k6/browser/tests/browser_context_test.go b/js/modules/k6/browser/tests/browser_context_test.go index 3779e959b00..274e4bcba9b 100644 --- a/js/modules/k6/browser/tests/browser_context_test.go +++ b/js/modules/k6/browser/tests/browser_context_test.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "net/http" - "net/http/httptest" - "os" "testing" "time" @@ -684,17 +682,7 @@ func TestK6Object(t *testing.T) { func TestNewTab(t *testing.T) { t.Parallel() - // Start a server that will return static html files. - mux := http.NewServeMux() - s := httptest.NewServer(mux) - t.Cleanup(s.Close) - - const ( - slash = string(os.PathSeparator) //nolint:forbidigo - path = slash + testBrowserStaticDir + slash - ) - fs := http.FileServer(http.Dir(testBrowserStaticDir)) - mux.Handle(path, http.StripPrefix(path, fs)) + tb := newTestBrowser(t, withFileServer()) // Start the iteration vu, _, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, "12345")) @@ -703,11 +691,11 @@ func TestNewTab(t *testing.T) { // Run the test script _, err := vu.RunAsync(t, ` const p = await browser.newPage() - await p.goto("%s/%s/ping.html") + await p.goto("%s") const p2 = await browser.context().newPage() - await p2.goto("%s/%s/ping.html") - `, s.URL, testBrowserStaticDir, s.URL, testBrowserStaticDir) + await p2.goto("%s") + `, tb.staticURL("ping.html"), tb.staticURL("ping.html")) require.NoError(t, err) } diff --git a/js/modules/k6/browser/tests/page_test.go b/js/modules/k6/browser/tests/page_test.go index 669689951ec..a4ac5f9c2c0 100644 --- a/js/modules/k6/browser/tests/page_test.go +++ b/js/modules/k6/browser/tests/page_test.go @@ -8,8 +8,6 @@ import ( "image/png" "io" "net/http" - "net/http/httptest" - "os" "runtime" "strconv" "sync/atomic" @@ -675,7 +673,7 @@ func TestPageScreenshotFullpage(t *testing.T) { const div = document.createElement('div'); div.style.width = '1280px'; div.style.height = '800px'; - div.style.background = 'linear-gradient(red, blue)'; + div.style.background = 'linear-gradient(to bottom, red, blue)'; document.body.appendChild(div); }`) @@ -690,15 +688,16 @@ func TestPageScreenshotFullpage(t *testing.T) { img, err := png.Decode(reader) assert.Nil(t, err) - assert.Equal(t, 1280, img.Bounds().Max.X, "screenshot width is not 1280px as expected, but %dpx", img.Bounds().Max.X) - assert.Equal(t, 800, img.Bounds().Max.Y, "screenshot height is not 800px as expected, but %dpx", img.Bounds().Max.Y) + assert.Equal(t, 1280, img.Bounds().Max.X, "want: screenshot width is 1280px, got: %dpx", img.Bounds().Max.X) + assert.Equal(t, 800, img.Bounds().Max.Y, "want: screenshot height is 800px, got: %dpx", img.Bounds().Max.Y) + // Allow tolerance to account for differences in rendering between + // different platforms and browsers. The goal is to ensure that the + // screenshot is mostly red at the top and mostly blue at the bottom. r, _, b, _ := img.At(0, 0).RGBA() - assert.Greater(t, r, uint32(128)) - assert.Less(t, b, uint32(128)) + assert.Truef(t, r > b*2, "want: the top pixel to be dominantly red, got R: %d, B: %d", r, b) r, _, b, _ = img.At(0, 799).RGBA() - assert.Less(t, r, uint32(128)) - assert.Greater(t, b, uint32(128)) + assert.Truef(t, b > r*2, "want: the bottom pixel to be dominantly blue, got R: %d, B: %d", r, b) } func TestPageTitle(t *testing.T) { @@ -1757,17 +1756,7 @@ func TestShadowDOMAndDocumentFragment(t *testing.T) { t.Skip() // timeouts } - // Start a server that will return static html files. - mux := http.NewServeMux() - s := httptest.NewServer(mux) - t.Cleanup(s.Close) - - const ( - slash = string(os.PathSeparator) //nolint:forbidigo - path = slash + testBrowserStaticDir + slash - ) - fs := http.FileServer(http.Dir(testBrowserStaticDir)) - mux.Handle(path, http.StripPrefix(path, fs)) + tb := newTestBrowser(t, withFileServer()) tests := []struct { name string @@ -1805,7 +1794,7 @@ func TestShadowDOMAndDocumentFragment(t *testing.T) { got := vu.RunPromise(t, ` const p = await browser.newPage() - await p.goto("%s/%s/shadow_and_doc_frag.html") + await p.goto("%s") const s = p.locator('%s') await s.waitFor({ @@ -1815,7 +1804,7 @@ func TestShadowDOMAndDocumentFragment(t *testing.T) { const text = await s.innerText(); return text; - `, s.URL, testBrowserStaticDir, tt.selector) + `, tb.staticURL("shadow_and_doc_frag.html"), tt.selector) assert.Equal(t, tt.want, got.Result().String()) }) } diff --git a/js/modules/k6/browser/tests/test_browser.go b/js/modules/k6/browser/tests/test_browser.go index ee3946d733d..d9464cd1b05 100644 --- a/js/modules/k6/browser/tests/test_browser.go +++ b/js/modules/k6/browser/tests/test_browser.go @@ -184,7 +184,7 @@ func (b *testBrowser) withFileServer() *testBrowser { fs := http.FileServer(http.Dir(testBrowserStaticDir)) - return b.withHandler(path, http.StripPrefix(path, fs).ServeHTTP) + return b.withHandler("/"+testBrowserStaticDir+"/", http.StripPrefix(path, fs).ServeHTTP) } // withHandler adds the given handler to the HTTP test server and makes it