Ai
1 Star 0 Fork 0

nuokwan/chromedp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
query_test.go 34.09 KB
一键复制 编辑 原始数据 按行查看 历史
Kenneth Shaw 提交于 2020-01-17 07:45 +08:00 . Updating cdproto dependency
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
package chromedp
import (
"bytes"
"fmt"
"image/png"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/css"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/chromedp/kb"
)
func TestWaitReady(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var nodeIDs []cdp.NodeID
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodeIDs) != 1 {
t.Errorf("expected to have exactly 1 node id: got %d", len(nodeIDs))
}
var value string
if err := Run(ctx,
WaitReady("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitVisible(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var nodeIDs []cdp.NodeID
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodeIDs) != 1 {
t.Errorf("expected to have exactly 1 node id: got %d", len(nodeIDs))
}
var value string
if err := Run(ctx,
WaitVisible("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitNotVisible(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var nodeIDs []cdp.NodeID
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodeIDs) != 1 {
t.Errorf("expected to have exactly 1 node id: got %d", len(nodeIDs))
}
var value string
if err := Run(ctx,
Click("#button2", ByID),
WaitNotVisible("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitEnabled(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var attr string
var ok bool
if err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if !ok {
t.Fatal("expected element to be disabled")
}
if err := Run(ctx,
Click("#button3", ByID),
WaitEnabled("#select1", ByID),
AttributeValue("#select1", "disabled", &attr, &ok, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be enabled")
}
var value string
if err := Run(ctx,
SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"),
Value("#select1", &value, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
if value != "foo" {
t.Fatalf("expected value to be foo, got: %s", value)
}
}
func TestWaitSelected(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
if err := Run(ctx,
Click("#button3", ByID),
WaitEnabled("#select1", ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
var attr string
ok := false
if err := Run(ctx, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, &ok)); err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be not selected")
}
if err := Run(ctx,
SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"),
WaitSelected(`//*[@id="select1"]/option[1]`),
AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, nil),
); err != nil {
t.Fatalf("got error: %v", err)
}
if attr != "true" {
t.Fatal("expected element to be selected")
}
}
func TestWaitNotPresent(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
if err := Run(ctx,
WaitVisible("#input3", ByID),
Click("#button4", ByID),
WaitNotPresent("#input3", ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestAtLeast(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var nodes []*cdp.Node
if err := Run(ctx, Nodes("//input", &nodes, AtLeast(3))); err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) < 3 {
t.Errorf("expected to have at least 3 nodes: got %d", len(nodes))
}
}
func TestByJSPath(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image2.html")
defer cancel()
// check nodes == 1
var nodes []*cdp.Node
if err := Run(ctx,
Nodes(`document.querySelector('#imagething').shadowRoot.querySelector('.container')`, &nodes, ByJSPath),
); err != nil {
t.Fatal(err)
}
if len(nodes) != 1 {
t.Errorf("expected nodes to have len 1, got: %d", len(nodes))
}
// check class
class := nodes[0].AttributeValue("class")
if class != "container" {
t.Errorf("expected class to be 'container', got: %q", class)
}
}
func TestNodes(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "table.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
n int
}{
{`/html/body/table/tbody[1]/tr[2]/td`, BySearch, 3},
{`body > table > tbody:nth-child(2) > tr:nth-child(2) > td:not(:last-child)`, ByQueryAll, 2},
{`body > table > tbody:nth-child(2) > tr:nth-child(2) > td`, ByQuery, 1},
{`#footer`, ByID, 1},
{`document.querySelector("body > table > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(1)")`, ByJSPath, 1},
}
for i, test := range tests {
var nodes []*cdp.Node
if err := Run(ctx, Nodes(test.sel, &nodes, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if len(nodes) != test.n {
t.Errorf("test %d expected to have %d nodes: got %d", i, test.n, len(nodes))
}
}
}
func TestNodeIDs(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "table.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
n int
}{
{`/html/body/table/tbody[1]/tr[2]/td`, BySearch, 3},
{`body > table > tbody:nth-child(2) > tr:nth-child(2) > td:not(:last-child)`, ByQueryAll, 2},
{`body > table > tbody:nth-child(2) > tr:nth-child(2) > td`, ByQuery, 1},
{`#footer`, ByID, 1},
{`document.querySelector("body > table > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(1)")`, ByJSPath, 1},
}
for i, test := range tests {
var ids []cdp.NodeID
if err := Run(ctx, NodeIDs(test.sel, &ids, test.by)); err != nil {
t.Fatal(err)
}
if len(ids) != test.n {
t.Errorf("test %d expected to have %d node id's: got %d", i, test.n, len(ids))
}
}
}
func TestFocusBlur(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="input1"]`, BySearch},
{`body > input[type="number"]:nth-child(1)`, ByQueryAll},
{`body > input[type="number"]:nth-child(1)`, ByQuery},
{`#input1`, ByID},
{`document.querySelector("#input1")`, ByJSPath},
}
if err := Run(ctx, Click("#input1", ByID)); err != nil {
t.Fatal(err)
}
for i, test := range tests {
var value string
if err := Run(ctx,
Focus(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if value != "9999" {
t.Errorf("test %d expected value is '9999', got: %q", i, value)
}
if err := Run(ctx,
Blur(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if value != "0" {
t.Errorf("test %d expected value is '0', got: %q", i, value)
}
}
}
func TestDimensions(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
width int64
height int64
}{
{`/html/body/img`, BySearch, 239, 239},
{`img`, ByQueryAll, 239, 239},
{`img`, ByQuery, 239, 239},
{`#icon-github`, ByID, 120, 120},
{`document.querySelector("#icon-github")`, ByJSPath, 120, 120},
}
for i, test := range tests {
var model *dom.BoxModel
if err := Run(ctx, Dimensions(test.sel, &model, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if model.Height != test.height || model.Width != test.width {
t.Errorf("test %d expected %dx%d, got: %dx%d", i, test.width, test.height, model.Height, model.Width)
}
}
}
func TestText(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
exp string
}{
{"#foo", ByID, "insert"},
{"body > form > span", ByQueryAll, "insert"},
{"body > form > span:nth-child(2)", ByQuery, "keyword"},
{"/html/body/form/span[2]", BySearch, "keyword"},
{`document.querySelector("#form > span:nth-child(2)")`, ByJSPath, "keyword"},
{"#inner-hidden", ByID, "this is"},
{"#hidden", ByID, ""},
}
for i, test := range tests {
var text string
if err := Run(ctx, Text(test.sel, &text, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if text != test.exp {
t.Errorf("test %d expected %q, got: %s", i, test.exp, text)
}
}
}
func TestTextContent(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
exp string
}{
{"#inner-hidden", ByID, "this is hidden"},
{"#hidden", ByID, "hidden"},
}
for i, test := range tests {
var text string
if err := Run(ctx, TextContent(test.sel, &text, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if text != test.exp {
t.Errorf("test %d expected %q, got: %s", i, test.exp, text)
}
}
}
func TestClear(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
// input fields
{`//*[@id="form"]/input[1]`, BySearch},
{`#form > input[type="text"]:nth-child(4)`, ByQuery},
{`#form > input[type="text"]`, ByQueryAll},
{`#keyword`, ByID},
{`document.querySelector("#keyword")`, ByJSPath},
// textarea fields
{`//*[@id="bar"]`, BySearch},
{`#form > textarea`, ByQuery},
{`#form > textarea`, ByQueryAll},
{`#bar`, ByID},
// input + textarea fields
{`//*[@id="form"]/input`, BySearch},
{`#form > input[type="text"]`, ByQueryAll},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var val string
if err := Run(ctx, Value(test.sel, &val, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if val == "" {
t.Errorf("expected %q to have non empty value", test.sel)
}
if err := Run(ctx,
Clear(test.sel, test.by),
Value(test.sel, &val, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
if val != "" {
t.Errorf("expected empty value for %q, got: %s", test.sel, val)
}
})
}
}
func TestReset(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
value string
exp string
}{
{`//*[@id="keyword"]`, BySearch, "foobar", "chromedp"},
{`#form > input[type="text"]:nth-child(6)`, ByQuery, "foobar", "foo"},
{`#form > input[type="text"]`, ByQueryAll, "foobar", "chromedp"},
{"#bar", ByID, "foobar", "bar"},
{`document.querySelector("#bar")`, ByJSPath, "foobar", "bar"},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var value string
if err := Run(ctx,
SetValue(test.sel, test.value, test.by),
Reset(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
if value != test.exp {
t.Errorf("expected value after reset is %s, got: %q", test.exp, value)
}
})
}
}
func TestValue(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="form"]/input[1]`, BySearch},
{`#form > input[type="text"]:nth-child(4)`, ByQuery},
{`#form > input[type="text"]`, ByQueryAll},
{`#keyword`, ByID},
{`document.querySelector("#keyword")`, ByJSPath},
}
for i, test := range tests {
var value string
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if value != "chromedp" {
t.Errorf("test %d expected `chromedp`, got: %s", i, value)
}
}
}
func TestValueUndefined(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var value string
err := Run(ctx, Value("foo", &value, ByID))
want := `could not retrieve attribute "value": encountered an undefined value`
got := fmt.Sprint(err)
if !strings.Contains(got, want) {
t.Fatalf("want error %q, got %q", want, got)
}
}
func TestSetValue(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="form"]/input[1]`, BySearch},
{`#form > input[type="text"]:nth-child(4)`, ByQuery},
{`#form > input[type="text"]`, ByQueryAll},
{`#bar`, ByID},
{`document.querySelector("#bar")`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var value string
if err := Run(ctx,
SetValue(test.sel, "FOOBAR", test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
if value != "FOOBAR" {
t.Errorf("expected `FOOBAR`, got: %s", value)
}
})
}
}
func TestAttributes(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
exp map[string]string
}{
{
`//*[@id="icon-brankas"]`, BySearch,
map[string]string{
"alt": "Brankas - Easy Money Management",
"id": "icon-brankas",
"src": "images/brankas.png",
},
},
{
`body > img:first-child`, ByQuery,
map[string]string{
"alt": "Brankas - Easy Money Management",
"id": "icon-brankas",
"src": "images/brankas.png",
},
},
{
`body > img:nth-child(2)`, ByQueryAll,
map[string]string{
"alt": `How people build software`,
"id": "icon-github",
"src": "images/github.png",
},
},
{
`#icon-github`, ByID,
map[string]string{
"alt": "How people build software",
"id": "icon-github",
"src": "images/github.png",
},
},
{
`document.querySelector("#icon-github")`, ByJSPath,
map[string]string{
"alt": "How people build software",
"id": "icon-github",
"src": "images/github.png",
},
},
}
for i, test := range tests {
var attrs map[string]string
if err := Run(ctx, Attributes(test.sel, &attrs, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if !reflect.DeepEqual(test.exp, attrs) {
t.Errorf("test %d expected %v, got: %v", i, test.exp, attrs)
}
}
}
func TestAttributesAll(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
exp []map[string]string
}{
{
"img", ByQueryAll,
[]map[string]string{
{
"alt": "Brankas - Easy Money Management",
"id": "icon-brankas",
"src": "images/brankas.png",
},
{
"alt": "How people build software",
"id": "icon-github",
"src": "images/github.png",
},
},
},
}
for i, test := range tests {
var attrs []map[string]string
if err := Run(ctx, AttributesAll(test.sel, &attrs, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if !reflect.DeepEqual(test.exp, attrs) {
t.Errorf("test %d expected %v, got: %v", i, test.exp, attrs)
}
}
}
func TestSetAttributes(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
attrs map[string]string
exp map[string]string
}{
{
`//*[@id="icon-brankas"]`, BySearch,
map[string]string{"data-url": "brankas"},
map[string]string{
"alt": "Brankas - Easy Money Management",
"id": "icon-brankas",
"src": "images/brankas.png",
"data-url": "brankas",
},
},
{
`body > img:first-child`, ByQuery,
map[string]string{"data-url": "brankas"},
map[string]string{
"alt": "Brankas - Easy Money Management",
"id": "icon-brankas",
"src": "images/brankas.png",
"data-url": "brankas",
},
},
{
`body > img:nth-child(2)`, ByQueryAll,
map[string]string{"width": "100", "height": "200"},
map[string]string{
"alt": `How people build software`,
"id": "icon-github",
"src": "images/github.png",
"width": "100",
"height": "200",
},
},
{
`#icon-github`, ByID,
map[string]string{"width": "100", "height": "200"},
map[string]string{
"alt": "How people build software",
"id": "icon-github",
"src": "images/github.png",
"width": "100",
"height": "200",
},
},
{
`document.querySelector("#icon-github")`, ByJSPath,
map[string]string{"width": "100", "height": "200"},
map[string]string{
"alt": "How people build software",
"id": "icon-github",
"src": "images/github.png",
"width": "100",
"height": "200",
},
},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
if err := Run(ctx, SetAttributes(test.sel, test.attrs, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
// TODO: figure why this test is flaky without this
time.Sleep(10 * time.Millisecond)
var attrs map[string]string
if err := Run(ctx, Attributes(test.sel, &attrs, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if !reflect.DeepEqual(test.exp, attrs) {
t.Errorf("expected %v, got: %v", test.exp, attrs)
}
})
}
}
func TestAttributeValue(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
attr string
exp string
}{
{`//*[@id="icon-brankas"]`, BySearch, "alt", "Brankas - Easy Money Management"},
{`body > img:first-child`, ByQuery, "alt", "Brankas - Easy Money Management"},
{`body > img:nth-child(2)`, ByQueryAll, "alt", "How people build software"},
{`#icon-github`, ByID, "alt", "How people build software"},
{`document.querySelector('#icon-github')`, ByJSPath, "alt", "How people build software"},
}
for i, test := range tests {
var value string
var ok bool
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if !ok {
t.Fatalf("test %d failed to get attribute %s on %s", i, test.attr, test.sel)
}
if value != test.exp {
t.Errorf("test %d expected %s to be %s, got: %s", i, test.attr, test.exp, value)
}
}
}
func TestSetAttributeValue(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
attr string
exp string
}{
{`//*[@id="keyword"]`, BySearch, "foo", "bar"},
{`#form > input[type="text"]:nth-child(6)`, ByQuery, "foo", "bar"},
{`#form > input[type="text"]`, ByQueryAll, "foo", "bar"},
{`#bar`, ByID, "foo", "bar"},
{`document.querySelector('#bar')`, ByJSPath, "foo", "bar"},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
if err := Run(ctx, SetAttributeValue(test.sel, test.attr, test.exp, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
// TODO: figure why this test is flaky without this
time.Sleep(10 * time.Millisecond)
var value string
var ok bool
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if !ok {
t.Fatalf("failed to get attribute %s on %s", test.attr, test.sel)
}
if value != test.exp {
t.Errorf("expected %s to be %s, got: %s", test.attr, test.exp, value)
}
})
}
}
func TestRemoveAttribute(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
attr string
}{
{`/html/body/img`, BySearch, "alt"},
{`img`, ByQueryAll, "alt"},
{`img`, ByQuery, "alt"},
{`#icon-github`, ByID, "alt"},
{`document.querySelector('#icon-github')`, ByJSPath, "alt"},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
if err := Run(ctx, RemoveAttribute(test.sel, test.attr, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
// TODO: figure why this test is flaky without this
time.Sleep(10 * time.Millisecond)
var value string
var ok bool
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if ok || value != "" {
t.Fatalf("expected attribute %s removed from element %s", test.attr, test.sel)
}
})
}
}
func TestClick(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="form"]/input[4]`, BySearch},
{`#form > input[type="submit"]:nth-child(11)`, ByQuery},
{`#form > input[type="submit"]:nth-child(11)`, ByQueryAll},
{`#btn2`, ByID},
{`document.querySelector('#btn2')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var title string
if err := Run(ctx,
Click(test.sel, test.by),
WaitVisible("#icon-brankas", ByID),
Title(&title),
); err != nil {
t.Fatalf("got error: %v", err)
}
if title != "this is title" {
t.Errorf("expected title to be 'chromedp - Google Search', got: %q", title)
}
})
}
}
func TestDoubleClick(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`/html/body/input[2]`, BySearch},
{`body > input[type="button"]:nth-child(2)`, ByQueryAll},
{`body > input[type="button"]:nth-child(2)`, ByQuery},
{`#button1`, ByID},
{`document.querySelector('#button1')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var value string
if err := Run(ctx,
DoubleClick(test.sel, test.by),
Value("#input1", &value, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
if value != "1" {
t.Errorf("expected value to be '1', got: %q", value)
}
})
}
}
func TestSendKeys(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
keys string
exp string
}{
{`//*[@id="input1"]`, BySearch, "INSERT ", "INSERT some value"}, // 0
{`#box4 > input:nth-child(1)`, ByQuery, "insert ", "insert some value"},
{`#box4 > textarea`, ByQueryAll, "prefix " + kb.End + "\b\b SUFFIX\n", "prefix textar SUFFIX\n"},
{`#textarea1`, ByID, "insert ", "insert textarea"},
{`#textarea1`, ByID, kb.End + "\b\b\n\naoeu\n\nfoo\n\nbar\n\n", "textar\n\naoeu\n\nfoo\n\nbar\n\n"},
{`#select1`, ByID, kb.ArrowDown + kb.ArrowDown, "three"}, // 5
{`document.querySelector('#textarea1')`, ByJSPath, "insert ", "insert textarea"},
}
for i, test := range tests {
i, test := i, test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
if runtime.GOOS == "darwin" && i == 5 {
t.Skipf("skipping test %d on darwin -- FIXME!", i)
}
t.Parallel()
ctx, cancel := testAllocate(t, "visible.html")
defer cancel()
var val string
if err := Run(ctx,
SendKeys(test.sel, test.keys, test.by),
Value(test.sel, &val, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
if val != test.exp {
t.Errorf("expected value %s, got: %s", test.exp, val)
}
})
}
}
func TestScreenshot(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image2.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
size int
}{
{`/html/body/img`, BySearch, 239},
{`img`, ByQueryAll, 239},
{`#icon-github`, ByID, 120},
{`document.querySelector('#imagething').shadowRoot.querySelector('.container')`, ByJSPath, 190},
}
// a smaller viewport speeds up this test
if err := Run(ctx, EmulateViewport(600, 400)); err != nil {
t.Fatal(err)
}
for i, test := range tests {
var buf []byte
if err := Run(ctx, Screenshot(test.sel, &buf, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if len(buf) == 0 {
t.Fatalf("test %d failed to capture screenshot", i)
}
img, err := png.Decode(bytes.NewReader(buf))
if err != nil {
t.Fatal(err)
}
size := img.Bounds().Size()
if size.X != test.size || size.Y != test.size {
t.Errorf("expected dimensions to be %d*%d, got %d*%d",
test.size, test.size, size.X, size.Y)
}
}
}
func TestScreenshotHighDPI(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
// Use a weird screen dimension with a 1.5 scale factor, so that
// cropping the screenshot is forced to use floating point arithmetic
// and keep the high DPI in mind.
// We also want the dimensions to be large enough to see the element we
// want, since we're not scrolling to ensure it's in view.
if err := Run(ctx, EmulateViewport(905, 705, EmulateScale(1.5))); err != nil {
t.Fatal(err)
}
var buf []byte
if err := Run(ctx, Screenshot("#half-color", &buf, ByID)); err != nil {
t.Fatal(err)
}
img, err := png.Decode(bytes.NewReader(buf))
if err != nil {
t.Fatal(err)
}
size := img.Bounds().Size()
wantSize := 300 // 200px at 1.5 scaling factor
if size.X != wantSize || size.Y != wantSize {
t.Fatalf("expected dimensions to be %d*%d, got %d*%d",
wantSize, wantSize, size.X, size.Y)
}
wantColor := func(x, y int, r, g, b, a uint32) {
color := img.At(x, y)
r_, g_, b_, a_ := color.RGBA()
if r_ != r || g_ != g || b_ != b || a_ != a {
t.Errorf("got 0x%04x%04x%04x%04x at (%d,%d), want 0x%04x%04x%04x%04x",
r_, g_, b_, a_, x, y, r, g, b, a)
}
}
// The left half is blue.
wantColor(5, 5, 0x0, 0x0, 0xffff, 0xffff)
wantColor(5, 295, 0x0, 0x0, 0xffff, 0xffff)
// The right half is red.
wantColor(295, 5, 0xffff, 0x0, 0x0, 0xffff)
wantColor(295, 295, 0xffff, 0x0, 0x0, 0xffff)
}
func TestSubmit(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="keyword"]`, BySearch},
{`#form > input[type="text"]:nth-child(4)`, ByQuery},
{`#form > input[type="text"]`, ByQueryAll},
{`#form`, ByID},
{`document.querySelector('#form')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var title string
if err := Run(ctx,
Submit(test.sel, test.by),
WaitVisible("#icon-brankas", ByID),
Title(&title),
); err != nil {
t.Fatalf("got error: %v", err)
}
if title != "this is title" {
t.Errorf("expected title to be 'this is title', got: %q", title)
}
})
}
}
func TestComputedStyle(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="input1"]`, BySearch},
{`body > input[type="number"]:nth-child(1)`, ByQueryAll},
{`body > input[type="number"]:nth-child(1)`, ByQuery},
{`#input1`, ByID},
{`document.querySelector('#input1')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var styles []*css.ComputedStyleProperty
if err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
for _, style := range styles {
if style.Name == "background-color" {
if style.Value != "rgb(255, 0, 0)" {
t.Logf("expected style 'rgb(255, 0, 0)' got: %s", style.Value)
}
}
}
if err := Run(ctx,
Click("#input1", ByID),
ComputedStyle(test.sel, &styles, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
for _, style := range styles {
if style.Name == "background-color" {
if style.Value != "rgb(255, 255, 0)" {
t.Fatalf("expected style 'rgb(255, 255, 0)' got: %s", style.Value)
}
}
}
})
}
}
func TestMatchedStyle(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`//*[@id="input1"]`, BySearch},
{`body > input[type="number"]:nth-child(1)`, ByQueryAll},
{`body > input[type="number"]:nth-child(1)`, ByQuery},
{`#input1`, ByID},
{`document.querySelector('#input1')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
var styles *css.GetMatchedStylesForNodeReturns
if err := Run(ctx, MatchedStyle(test.sel, &styles, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
// TODO: Add logic to check if the style returned is true and valid.
})
}
}
func TestFileUpload(t *testing.T) {
t.Parallel()
// create test server
mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "%s", uploadHTML)
})
mux.HandleFunc("/upload", func(res http.ResponseWriter, req *http.Request) {
f, _, err := req.FormFile("upload")
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(res, resultHTML, len(buf))
})
s := httptest.NewServer(mux)
defer s.Close()
// create temporary file on disk
tmpfile, err := ioutil.TempFile("", "chromedp-upload-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
if _, err := tmpfile.WriteString(uploadHTML); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
tests := []struct {
a Action
}{
{SendKeys(`input[name="upload"]`, tmpfile.Name(), NodeVisible)},
{SetUploadFiles(`input[name="upload"]`, []string{tmpfile.Name()}, NodeVisible)},
}
// Don't run these tests in parallel. The only way to do so would be to
// fire a separate httptest server and tmpfile for each. There's no way
// to share these resources easily among parallel subtests, as the
// parent must finish for the children to run, made impossible by the
// defers above.
for i, test := range tests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
ctx, cancel := testAllocate(t, "")
defer cancel()
var result string
if err := Run(ctx,
Navigate(s.URL),
test.a,
Click(`input[name="submit"]`),
Text(`#result`, &result, ByID, NodeVisible),
); err != nil {
t.Fatalf("test %d expected no error, got: %v", i, err)
}
if result != fmt.Sprintf("%d", len(uploadHTML)) {
t.Errorf("test %d expected result to be %d, got: %s", i, len(uploadHTML), result)
}
})
}
}
func TestInnerHTML(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "table.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
}{
{`/html/body/table/thead`, BySearch},
{`thead`, ByQueryAll},
{`thead`, ByQuery},
{`document.querySelector("#footer > td:nth-child(2)")`, ByJSPath},
}
for i, test := range tests {
var html string
if err := Run(ctx, InnerHTML(test.sel, &html, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if html == "" {
t.Fatalf("test %d: InnerHTML is empty", i)
}
}
}
func TestOuterHTML(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "table.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
}{
{`/html/body/table/thead/tr`, BySearch},
{`thead tr`, ByQueryAll},
{`thead tr`, ByQuery},
{`document.querySelector("#footer > td:nth-child(2)")`, ByJSPath},
}
for i, test := range tests {
var html string
if err := Run(ctx, OuterHTML(test.sel, &html, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if html == "" {
t.Fatalf("test %d: OuterHTML is empty", i)
}
}
}
func TestScrollIntoView(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
tests := []struct {
sel string
by QueryOption
}{
{`/html/body/img`, BySearch},
{`img`, ByQueryAll},
{`img`, ByQuery},
{`#icon-github`, ByID},
{`document.querySelector('#icon-github')`, ByJSPath},
}
for i, test := range tests {
if err := Run(ctx, ScrollIntoView(test.sel, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
// TODO test scroll event
}
}
func TestSVGFullXPath(t *testing.T) {
t.Parallel()
tests := []struct {
sel string
by QueryOption
}{
{`#brankas`, ByQuery},
{`text`, ByQueryAll},
{`brankas`, ByID},
{`//*[local-name()='text']`, BySearch},
{`document.querySelector('#brankas')`, ByJSPath},
}
for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "svg.html")
defer cancel()
var nodes []*cdp.Node
if err := Run(ctx, Nodes(test.sel, &nodes, test.by)); err != nil {
t.Fatal(err)
}
if len(nodes) < 1 {
t.Fatalf("expected at least 1 node, got: %d", len(nodes))
}
const exp = `/html[1]/body[1]/*[local-name()='svg'][1]/*[local-name()='text'][1]`
xpath := nodes[0].FullXPath()
if exp != xpath {
t.Errorf("expected %q, got: %q", exp, xpath)
}
var text1, text2 string
if err := Run(ctx, TextContent(test.sel, &text1, test.by)); err != nil {
t.Fatal(err)
}
if strings.TrimSpace(text1) != "Brankas" {
t.Errorf("expected %q, got: %q", "Brankas", text1)
}
if err := Run(ctx, TextContent(xpath, &text2)); err != nil {
t.Fatal(err)
}
if strings.TrimSpace(text2) != "Brankas" {
t.Errorf("expected %q, got: %q", "Brankas", text2)
}
})
}
}
const (
uploadHTML = `<!doctype html>
<html>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input name="upload" type="file"/>
<input name="submit" type="submit"/>
</form>
</body>
</html>`
resultHTML = `<!doctype html>
<html>
<body>
<div id="result">%d</div>
</body>
</html>`
)
func TestWaitReadyReuseAction(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
// Reusing a single WaitReady action used to panic.
action := WaitReady("#input2", ByID)
for i := 0; i < 3; i++ {
if err := Run(ctx, action); err != nil {
t.Fatalf("got error: %v", err)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nuokwan/chromedp.git
git@gitee.com:nuokwan/chromedp.git
nuokwan
chromedp
chromedp
master

搜索帮助