2017-02-09 15:01:40 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pool *Pool
|
|
|
|
|
|
|
|
var defaultContext = context.Background()
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
|
|
|
|
2017-02-14 08:41:23 +00:00
|
|
|
pool, err = NewPool(PoolLog(log.Printf, log.Printf, log.Printf))
|
2017-02-09 15:01:40 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
err = pool.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNavigate(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c, err := pool.Allocate(defaultContext)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, Navigate("https://www.google.com/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, WaitVisible(`#hplogo`, ByID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlstr string
|
|
|
|
err = c.Run(defaultContext, Location(&urlstr))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(urlstr, "https://www.google.") {
|
2017-02-12 07:08:40 +00:00
|
|
|
t.Errorf("expected to be on google domain, at: %s", urlstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(strings.ToLower(title), "google") {
|
|
|
|
t.Errorf("expected title to contain google, instead title is: %s", title)
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-12 04:59:33 +00:00
|
|
|
|
|
|
|
func TestSendKeys(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c, err := pool.Allocate(defaultContext)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Release()
|
|
|
|
}
|