From e698c943b397708e0ec879be09eddc8a251b5f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 21 Mar 2019 14:52:45 +0000 Subject: [PATCH] make the simple and allocator examples runnable This way, not only do we ensure that they always build, they're also verified as part of 'go test'. --- _example/allocactor.go | 26 -------------- _example/simple.go | 36 ------------------- example_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 62 deletions(-) delete mode 100644 _example/allocactor.go delete mode 100644 _example/simple.go create mode 100644 example_test.go diff --git a/_example/allocactor.go b/_example/allocactor.go deleted file mode 100644 index 8a1fa5d..0000000 --- a/_example/allocactor.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "context" - "log" - - "github.com/chromedp/chromedp" -) - -func main() { - dockerAllocatorOpts := []chromedp.DockerAllocatorOption{} - - ctxt, cancel := chromedp.NewAllocator(context.Background(), chromedp.WithDockerAllocator(dockerAllocatorOpts...)) - defer cancel() - - task1Context, cancel := chromedp.NewContext(ctxt) - defer cancel() - - if err := chromedp.Run(task1Context, myTask()); err != nil { - log.Fatal(err) - } -} - -func myTask() chromedp.Tasks { - return []chromedp.Action{} -} diff --git a/_example/simple.go b/_example/simple.go deleted file mode 100644 index 6c5b403..0000000 --- a/_example/simple.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - - "github.com/chromedp/chromedp" -) - -func main() { - // create a new context - ctx, cancel := chromedp.NewContext(context.Background()) - defer cancel() - - // grab the title - var title string - if err := chromedp.Run(ctx, grabTitle(&title)); err != nil { - log.Fatal(err) - } - - // print it - fmt.Println(title) - - // ensure all resources are cleaned up - cancel() - chromedp.FromContext(ctx).Wait() -} - -func grabTitle(title *string) chromedp.Tasks { - return []chromedp.Action{ - chromedp.Navigate("https://github.com/"), - chromedp.WaitVisible("#start-of-content", chromedp.ByID), - chromedp.Title(title), - } -} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..b9516f4 --- /dev/null +++ b/example_test.go @@ -0,0 +1,79 @@ +package chromedp_test + +import ( + "context" + "fmt" + "io/ioutil" + "os" + + "github.com/chromedp/chromedp" +) + +func ExampleTitle() { + ctx, cancel := chromedp.NewContext(context.Background()) + defer cancel() + + var title string + if err := chromedp.Run(ctx, chromedp.Tasks{ + chromedp.Navigate("https://github.com/chromedp/chromedp/issues"), + chromedp.WaitVisible("#start-of-content", chromedp.ByID), + chromedp.Title(&title), + }); err != nil { + panic(err) + } + + fmt.Println(title) + + // wait for the resources to be cleaned up + cancel() + chromedp.FromContext(ctx).Wait() + + // Output: + // Issues · chromedp/chromedp · GitHub +} + +func ExampleExecAllocatorOption() { + dir, err := ioutil.TempDir("", "chromedp-example") + if err != nil { + panic(err) + } + defer os.RemoveAll(dir) + + opts := []chromedp.ExecAllocatorOption{ + chromedp.NoFirstRun, + chromedp.NoDefaultBrowserCheck, + chromedp.Headless, + chromedp.DisableGPU, + chromedp.UserDataDir(dir), + } + + allocCtx, cancel := chromedp.NewAllocator(context.Background(), + chromedp.WithExecAllocator(opts...)) + defer cancel() + + taskCtx, cancel := chromedp.NewContext(allocCtx) + defer cancel() + + // ensure that the browser process is started + if err := chromedp.Run(taskCtx, chromedp.Tasks{}); err != nil { + panic(err) + } + + files, err := ioutil.ReadDir(dir) + if err != nil { + panic(err) + } + fmt.Println("Files in UserDataDir:") + for _, file := range files { + fmt.Println(file.Name()) + } + + // wait for the resources to be cleaned up + cancel() + chromedp.FromContext(allocCtx).Wait() + + // Output: + // Files in UserDataDir: + // Default + // DevToolsActivePort +}