2017-02-09 15:01:40 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-05 13:14:50 +00:00
|
|
|
"fmt"
|
2017-02-09 15:01:40 +00:00
|
|
|
"os"
|
2017-12-27 02:30:28 +00:00
|
|
|
"path"
|
2017-02-09 15:01:40 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-12-18 00:11:42 +00:00
|
|
|
var (
|
|
|
|
testdataDir string
|
|
|
|
|
2019-04-01 13:20:41 +00:00
|
|
|
browserCtx context.Context
|
2017-12-18 00:11:42 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
allocOpts = []ExecAllocatorOption{
|
|
|
|
NoFirstRun,
|
|
|
|
NoDefaultBrowserCheck,
|
|
|
|
Headless,
|
2019-03-20 17:41:47 +00:00
|
|
|
DisableGPU,
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
)
|
2017-02-09 15:01:40 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) {
|
2019-04-01 13:20:41 +00:00
|
|
|
// Same browser, new tab; not needing to start new chrome browsers for
|
|
|
|
// each test gives a huge speed-up.
|
|
|
|
ctx, cancel := NewContext(browserCtx)
|
2017-02-09 15:01:40 +00:00
|
|
|
|
2019-03-20 16:56:03 +00:00
|
|
|
// Only navigate if we want a path, otherwise leave the blank page.
|
|
|
|
if path != "" {
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/"+path)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-12 07:08:40 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
return ctx, cancel
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
2017-02-12 04:59:33 +00:00
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2017-12-27 02:30:28 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
panic(fmt.Sprintf("could not get working directory: %v", err))
|
2017-12-27 02:30:28 +00:00
|
|
|
}
|
|
|
|
testdataDir = "file://" + path.Join(wd, "testdata")
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
// it's worth noting that newer versions of chrome (64+) run much faster
|
2017-12-18 00:11:42 +00:00
|
|
|
// than older ones -- same for headless_shell ...
|
2019-03-05 13:14:50 +00:00
|
|
|
if execPath := os.Getenv("CHROMEDP_TEST_RUNNER"); execPath != "" {
|
|
|
|
allocOpts = append(allocOpts, ExecPath(execPath))
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
// not explicitly needed to be set, as this vastly speeds up unit tests
|
|
|
|
if noSandbox := os.Getenv("CHROMEDP_NO_SANDBOX"); noSandbox != "false" {
|
2019-03-05 13:14:50 +00:00
|
|
|
allocOpts = append(allocOpts, NoSandbox)
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:20:41 +00:00
|
|
|
allocCtx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
|
|
|
|
|
|
|
|
// start the browser
|
|
|
|
browserCtx, _ = NewContext(allocCtx)
|
2019-04-01 16:12:17 +00:00
|
|
|
if err := Run(browserCtx); err != nil {
|
2019-04-01 13:20:41 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-15 14:46:07 +00:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
cancel()
|
2017-02-15 14:46:07 +00:00
|
|
|
os.Exit(code)
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|