Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some browser Windows tests #4184

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions js/modules/k6/browser/tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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"))
Expand All @@ -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)
}

Expand Down
33 changes: 11 additions & 22 deletions js/modules/k6/browser/tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"image/png"
"io"
"net/http"
"net/http/httptest"
"os"
"runtime"
"strconv"
"sync/atomic"
Expand Down Expand Up @@ -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);
}`)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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())
})
}
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/browser/tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading