f742f327a7
Using a smaller viewport speeds up both tests, and lets us know what dimensions to expect in TestCaptureScreenshot. For TestScreenshot, we can know what dimensions to expect in advance, as we have the images in testdata. 'go test -run Screenshot' goes from ~0.9s to ~0.5s on my machine. Finally, don't run ExampleTitle as part of 'go test', as it's slow.
79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package chromedp_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"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).Allocator.Wait()
|
|
|
|
// no expected output, to not run this test as part of 'go test'; it's
|
|
// too slow, requiring internet access.
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
path := filepath.Join(dir, "DevToolsActivePort")
|
|
bs, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
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:
|
|
// DevToolsActivePort has 2 lines
|
|
}
|