2019-03-21 14:52:45 +00:00
|
|
|
package chromedp_test
|
|
|
|
|
|
|
|
import (
|
2019-03-21 16:00:07 +00:00
|
|
|
"bytes"
|
2019-03-21 14:52:45 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-04-08 10:56:16 +00:00
|
|
|
"log"
|
2019-03-21 14:52:45 +00:00
|
|
|
"os"
|
2019-03-21 16:00:07 +00:00
|
|
|
"path/filepath"
|
2019-03-21 14:52:45 +00:00
|
|
|
|
|
|
|
"github.com/chromedp/chromedp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleTitle() {
|
|
|
|
ctx, cancel := chromedp.NewContext(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var title string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := chromedp.Run(ctx,
|
2019-03-21 14:52:45 +00:00
|
|
|
chromedp.Navigate("https://github.com/chromedp/chromedp/issues"),
|
|
|
|
chromedp.WaitVisible("#start-of-content", chromedp.ByID),
|
|
|
|
chromedp.Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2019-03-21 14:52:45 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(title)
|
|
|
|
|
2019-03-21 16:39:10 +00:00
|
|
|
// no expected output, to not run this test as part of 'go test'; it's
|
|
|
|
// too slow, requiring internet access.
|
2019-03-21 14:52:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 11:51:47 +00:00
|
|
|
func ExampleExecAllocator() {
|
2019-03-21 14:52:45 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:10:59 +00:00
|
|
|
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
2019-03-21 14:52:45 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-04-08 10:56:16 +00:00
|
|
|
// also set up a custom logger
|
|
|
|
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
|
2019-03-21 14:52:45 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// ensure that the browser process is started
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := chromedp.Run(taskCtx); err != nil {
|
2019-03-21 14:52:45 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:00:07 +00:00
|
|
|
path := filepath.Join(dir, "DevToolsActivePort")
|
|
|
|
bs, err := ioutil.ReadFile(path)
|
2019-03-21 14:52:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-21 16:00:07 +00:00
|
|
|
lines := bytes.Split(bs, []byte("\n"))
|
|
|
|
fmt.Printf("DevToolsActivePort has %d lines\n", len(lines))
|
2019-03-21 14:52:45 +00:00
|
|
|
|
|
|
|
// Output:
|
2019-03-21 16:00:07 +00:00
|
|
|
// DevToolsActivePort has 2 lines
|
2019-03-21 14:52:45 +00:00
|
|
|
}
|
2019-03-21 20:24:09 +00:00
|
|
|
|
2019-04-02 11:51:47 +00:00
|
|
|
func ExampleNewContext_manyTabs() {
|
2019-03-21 20:24:09 +00:00
|
|
|
// new browser, first tab
|
|
|
|
ctx1, cancel := chromedp.NewContext(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// ensure the first tab is created
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := chromedp.Run(ctx1); err != nil {
|
2019-03-21 20:24:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// same browser, second tab
|
|
|
|
ctx2, _ := chromedp.NewContext(ctx1)
|
|
|
|
|
|
|
|
// ensure the second tab is created
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := chromedp.Run(ctx2); err != nil {
|
2019-03-21 20:24:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 := chromedp.FromContext(ctx1)
|
|
|
|
c2 := chromedp.FromContext(ctx2)
|
|
|
|
|
|
|
|
fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
|
2019-04-01 13:31:11 +00:00
|
|
|
fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)
|
2019-03-21 20:24:09 +00:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Same browser: true
|
|
|
|
// Same tab: false
|
|
|
|
}
|