2017-02-24 11:24:06 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/cdp"
|
2017-02-24 11:24:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWaitReady(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
|
|
|
var nodeIDs []cdp.NodeID
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if 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))
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitReady("#input2", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitVisible(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
|
|
|
var nodeIDs []cdp.NodeID
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if 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))
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitVisible("#input2", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitNotVisible(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
|
|
|
var nodeIDs []cdp.NodeID
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if 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))
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Click("#button2", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitNotVisible("#input2", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitEnabled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
|
|
|
var attr string
|
2017-05-06 01:20:24 +00:00
|
|
|
var ok bool
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("expected element to be disabled")
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Click("#button3", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitEnabled("#select1", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-24 11:24:06 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatal("expected element to be enabled")
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true")); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Value("#select1", &value, ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-24 11:24:06 +00:00
|
|
|
if value != "foo" {
|
|
|
|
t.Fatalf("expected value to be foo, got: %s", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitSelected(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, Click("#button3", ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitEnabled("#select1", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var attr string
|
|
|
|
ok := false
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, &ok)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-24 11:24:06 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatal("expected element to be not selected")
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true")); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitSelected(`//*[@id="select1"]/option[1]`)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, nil)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-24 11:24:06 +00:00
|
|
|
if attr != "true" {
|
|
|
|
t.Fatal("expected element to be selected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitNotPresent(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, WaitVisible("#input3", ByID))
|
2017-02-24 11:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Click("#button4", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitNotPresent("#input3", ByID)); err != nil {
|
2017-02-24 11:24:06 +00:00
|
|
|
t.Fatalf("got error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtLeast(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "js.html")
|
|
|
|
defer cancel()
|
2017-02-24 11:24:06 +00:00
|
|
|
|
|
|
|
var nodes []*cdp.Node
|
2019-03-05 13:14:50 +00:00
|
|
|
err := Run(ctx, Nodes("//input", &nodes, AtLeast(3)))
|
2017-02-24 11:24:06 +00:00
|
|
|
if 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))
|
|
|
|
}
|
|
|
|
}
|