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'.
This commit is contained in:
Daniel Martí 2019-03-21 14:52:45 +00:00
parent 5fb1c07412
commit e698c943b3
3 changed files with 79 additions and 62 deletions

View File

@ -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{}
}

View File

@ -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),
}
}

79
example_test.go Normal file
View File

@ -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
}