2019-03-21 14:52:45 +00:00
|
|
|
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()
|
2019-03-21 15:20:27 +00:00
|
|
|
chromedp.FromContext(ctx).Allocator.Wait()
|
2019-03-21 14:52:45 +00:00
|
|
|
|
|
|
|
// 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()
|
2019-03-21 15:20:27 +00:00
|
|
|
chromedp.FromContext(allocCtx).Allocator.Wait()
|
2019-03-21 14:52:45 +00:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Files in UserDataDir:
|
|
|
|
// Default
|
|
|
|
// DevToolsActivePort
|
|
|
|
}
|