From c42b414fffbd412dddac4903e5bc9355c6bb786c Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Sat, 6 Jul 2024 15:36:26 +0800 Subject: [PATCH] fix flaky tests --- element_test.go | 2 +- hijack_test.go | 6 +++--- lib/launcher/browser.go | 1 + page_test.go | 19 ++++++++----------- setup_test.go | 4 +++- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/element_test.go b/element_test.go index ee80598b..6a1f118b 100644 --- a/element_test.go +++ b/element_test.go @@ -287,7 +287,7 @@ func TestIframeCrossDomains(t *testing.T) { `) - u := launcher.New().HeadlessNew(true).MustLaunch() + u := launcher.New().HeadlessNew(true).NoSandbox(true).MustLaunch() browser := rod.New().ControlURL(u).NoDefaultDevice().MustConnect() defer browser.MustClose() diff --git a/hijack_test.go b/hijack_test.go index 45a4bd45..a5b114ab 100644 --- a/hijack_test.go +++ b/hijack_test.go @@ -181,7 +181,7 @@ func TestHijackMockWholeResponse(t *testing.T) { go router.Run() - g.page.MustNavigate("http://test.com") + g.page.MustNavigate("http://localhost") g.Eq("ok", g.page.MustElement("body").MustText()) } @@ -289,7 +289,7 @@ func TestHijackLoadResponseErr(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) - router.MustAdd("http://test.com/a", func(ctx *rod.Hijack) { + router.MustAdd("http://localhost/a", func(ctx *rod.Hijack) { g.Err(ctx.LoadResponse(&http.Client{ Transport: &MockRoundTripper{err: errors.New("err")}, }, true)) @@ -308,7 +308,7 @@ func TestHijackLoadResponseErr(t *testing.T) { go router.Run() - _ = p.Navigate("http://test.com/a") + _ = p.Navigate("http://localhost/a") wg.Wait() } diff --git a/lib/launcher/browser.go b/lib/launcher/browser.go index 4a5e562e..733af27d 100644 --- a/lib/launcher/browser.go +++ b/lib/launcher/browser.go @@ -181,6 +181,7 @@ func (lc *Browser) Validate() error { } cmd := exec.Command(lc.BinPath(), "--headless", "--no-sandbox", + "--use-mock-keychain", "--disable-dev-shm-usage", "--disable-gpu", "--dump-dom", "about:blank") b, err := cmd.CombinedOutput() if err != nil { diff --git a/page_test.go b/page_test.go index 8241a99c..f40536b1 100644 --- a/page_test.go +++ b/page_test.go @@ -100,7 +100,7 @@ func TestSetExtraHeaders(t *testing.T) { wg := sync.WaitGroup{} var header http.Header - s.Mux.HandleFunc("/", func(_ http.ResponseWriter, r *http.Request) { + s.Mux.HandleFunc("/set-header", func(_ http.ResponseWriter, r *http.Request) { header = r.Header wg.Done() }) @@ -109,7 +109,7 @@ func TestSetExtraHeaders(t *testing.T) { cleanup := p.MustSetExtraHeaders("a", "1", "b", "2") wg.Add(1) - p.MustNavigate(s.URL()) + p.MustNavigate(s.URL("/set-header")) wg.Wait() g.Eq(header.Get("a"), "1") @@ -117,15 +117,12 @@ func TestSetExtraHeaders(t *testing.T) { cleanup() - // TODO: I don't know why it will fail randomly - if false { - wg.Add(1) - p.MustReload() - wg.Wait() + wg.Add(1) + p.MustReload() + wg.Wait() - g.Eq(header.Get("a"), "") - g.Eq(header.Get("b"), "") - } + g.Eq(header.Get("a"), "") + g.Eq(header.Get("b"), "") } func TestSetUserAgent(t *testing.T) { @@ -892,7 +889,7 @@ func TestFonts(t *testing.T) { func TestPagePDF(t *testing.T) { g := setup(t) - p := g.page.MustNavigate(g.srcFile("fixtures/click.html")) + p := g.page.MustNavigate(g.srcFile("fixtures/click.html")).MustWaitLoad() s, err := p.PDF(&proto.PagePrintToPDF{}) g.E(err) diff --git a/setup_test.go b/setup_test.go index ae29964e..11b6a022 100644 --- a/setup_test.go +++ b/setup_test.go @@ -89,7 +89,7 @@ func newTesterPool() rod.Pool[G] { } func newTester() *G { - u := launcher.New().Set("proxy-bypass-list", "<-loopback>").MustLaunch() + u := launcher.New().Set("proxy-bypass-list", "<-loopback>").NoSandbox(true).MustLaunch() mc := newMockClient(u) @@ -127,6 +127,8 @@ func setup(t *testing.T) G { tester.checkLeaking() + tester.page.MustNavigate("") + return *tester }