From 0e92de5e65542fff05985767eed344d1382cf4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 21 Mar 2019 16:00:07 +0000 Subject: [PATCH] make ExampleExecAllocatorOption less flaky The Default folder is created asynchronously to Chrome starting to listen on the debugging protocol port. So we can't expect it to exist. Instead, base the example on DevToolsActivePort, which we can rely on. --- example_test.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/example_test.go b/example_test.go index baff57b..aaaa492 100644 --- a/example_test.go +++ b/example_test.go @@ -1,10 +1,12 @@ package chromedp_test import ( + "bytes" "context" "fmt" "io/ioutil" "os" + "path/filepath" "github.com/chromedp/chromedp" ) @@ -59,21 +61,18 @@ func ExampleExecAllocatorOption() { panic(err) } - files, err := ioutil.ReadDir(dir) + path := filepath.Join(dir, "DevToolsActivePort") + bs, err := ioutil.ReadFile(path) if err != nil { panic(err) } - fmt.Println("Files in UserDataDir:") - for _, file := range files { - fmt.Println(file.Name()) - } + lines := bytes.Split(bs, []byte("\n")) + fmt.Printf("DevToolsActivePort has %d lines\n", len(lines)) // wait for the resources to be cleaned up cancel() chromedp.FromContext(allocCtx).Allocator.Wait() // Output: - // Files in UserDataDir: - // Default - // DevToolsActivePort + // DevToolsActivePort has 2 lines }