Adding unit test for navigation actions and few minor changes.

This commit is contained in:
Randy Cahyana 2017-02-23 15:23:37 +07:00
parent 320373de1a
commit 3a264ed3ff
4 changed files with 322 additions and 81 deletions

262
input_test.go Normal file
View File

@ -0,0 +1,262 @@
package chromedp
import (
"fmt"
"strconv"
"testing"
"time"
"github.com/knq/chromedp/cdp"
"github.com/knq/chromedp/cdp/input"
)
func TestMouseClickXY(t *testing.T) {
t.Parallel()
c := testAllocate(t, "input.html")
defer c.Release()
var err error
err = c.Run(defaultContext, Sleep(time.Millisecond*50))
if err != nil {
t.Fatal(err)
}
tests := []struct {
x, y int64
}{
{100, 100},
{0, 0},
{9999, 100},
{100, 9999},
}
for i, test := range tests {
err = c.Run(defaultContext, MouseClickXY(test.x, test.y))
if err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
var value string
err = c.Run(defaultContext, Value("#input1", &value, ByID))
if err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
x, err := strconv.ParseInt(value, 10, 64)
if err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if x != test.x {
t.Fatalf("test %d expected x to be: %d, got: %d", i, test.x, x)
}
err = c.Run(defaultContext, Value("#input2", &value, ByID))
if err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
y, err := strconv.ParseInt(value, 10, 64)
if err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if y != test.y {
t.Fatalf("test %d expected y to be: %d, got: %d", i, test.y, y)
}
}
}
func TestMouseClickNode(t *testing.T) {
tests := []struct {
exp string
opt MouseOption
by QueryOption
}{
{"foo", ButtonType(input.ButtonNone), ByID},
{"bar", ButtonType(input.ButtonLeft), ByID},
{"bar-middle", ButtonType(input.ButtonMiddle), ByID},
{"bar-right", ButtonType(input.ButtonRight), ByID},
{"bar2", ClickCount(2), ByID},
}
var err error
for i, test := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
t.Parallel()
c := testAllocate(t, "input.html")
defer c.Release()
var nodes []*cdp.Node
err = c.Run(defaultContext, Nodes("#button2", &nodes, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
err = c.Run(defaultContext, MouseClickNode(nodes[0], test.opt))
if err != nil {
t.Fatalf("got error: %v", err)
}
time.Sleep(time.Millisecond * 50)
var value string
err = c.Run(defaultContext, Value("#input3", &value, ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
if value != test.exp {
t.Fatalf("expected to have value %s, got: %s", test.exp, value)
}
})
}
}
func TestMouseClickOffscreenNode(t *testing.T) {
tests := []struct {
sel string
exp int
by QueryOption
}{
{"#button3", 0, ByID},
{"#button3", 2, ByID},
{"#button3", 10, ByID},
}
var err error
for i, test := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
t.Parallel()
c := testAllocate(t, "input.html")
defer c.Release()
var nodes []*cdp.Node
err = c.Run(defaultContext, Nodes(test.sel, &nodes, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
for i := test.exp; i > 0; i-- {
err = c.Run(defaultContext, MouseClickNode(nodes[0]))
if err != nil {
t.Fatalf("got error: %v", err)
}
}
time.Sleep(time.Millisecond * 50)
var value int
err = c.Run(defaultContext, Evaluate("window.document.test_i", &value))
if err != nil {
t.Fatalf("got error: %v", err)
}
if value != test.exp {
t.Fatalf("expected to have value %d, got: %d", test.exp, value)
}
})
}
}
func TestKeyAction(t *testing.T) {
tests := []struct {
sel, exp string
by QueryOption
}{
{"#input4", "foo", ByID},
{"#input4", "foo and bar", ByID},
{"#input4", "1234567890", ByID},
{"#input4", "~!@#$%^&*()_+=[];'", ByID},
{"#input4", "你", ByID},
{"#input4", "\n\nfoo\n\nbar\n\n", ByID},
}
var err error
for i, test := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
t.Parallel()
c := testAllocate(t, "input.html")
defer c.Release()
var nodes []*cdp.Node
err = c.Run(defaultContext, Nodes(test.sel, &nodes, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
err = c.Run(defaultContext, Focus(test.sel, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, KeyAction(test.exp))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value(test.sel, &value, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if value != test.exp {
t.Fatalf("expected to have value %s, got: %s", test.exp, value)
}
})
}
}
func TestKeyActionNode(t *testing.T) {
tests := []struct {
sel, exp string
by QueryOption
}{
{"#input4", "foo", ByID},
{"#input4", "foo and bar", ByID},
{"#input4", "1234567890", ByID},
{"#input4", "~!@#$%^&*()_+=[];'", ByID},
{"#input4", "你", ByID},
{"#input4", "\n\nfoo\n\nbar\n\n", ByID},
}
var err error
for i, test := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
t.Parallel()
c := testAllocate(t, "input.html")
defer c.Release()
var nodes []*cdp.Node
err = c.Run(defaultContext, Nodes(test.sel, &nodes, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
err = c.Run(defaultContext, KeyActionNode(nodes[0], test.exp))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value(test.sel, &value, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if value != test.exp {
t.Fatalf("expected to have value %s, got: %s", test.exp, value)
}
})
}
}

View File

@ -82,10 +82,7 @@ func TestNavigationEntries(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, NavigationEntries(&index, &entries))
if err != nil {
@ -119,10 +116,7 @@ func TestNavigateToHistoryEntry(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, NavigationEntries(&index, &entries))
if err != nil {
@ -134,20 +128,14 @@ func TestNavigateToHistoryEntry(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, NavigateToHistoryEntry(entries[index].ID))
if err != nil {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var title string
err = c.Run(defaultContext, Title(&title))
@ -172,10 +160,7 @@ func TestNavigateBack(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var exptitle string
err = c.Run(defaultContext, Title(&exptitle))
@ -188,20 +173,14 @@ func TestNavigateBack(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, NavigateBack())
if err != nil {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var title string
err = c.Run(defaultContext, Title(&title))
@ -226,20 +205,14 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html"))
if err != nil {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var exptitle string
err = c.Run(defaultContext, Title(&exptitle))
@ -252,20 +225,14 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, NavigateForward())
if err != nil {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var title string
err = c.Run(defaultContext, Title(&title))
@ -309,10 +276,7 @@ func TestReload(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var exptitle string
err = c.Run(defaultContext, Title(&exptitle))
@ -325,10 +289,7 @@ func TestReload(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var title string
err = c.Run(defaultContext, Title(&title))
@ -353,10 +314,7 @@ func TestCaptureScreenshot(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var buf []byte
err = c.Run(defaultContext, CaptureScreenshot(&buf))
@ -389,10 +347,7 @@ func TestAddOnLoadScript(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
if scriptID == "" {
t.Fatal("got empty script ID")
@ -428,10 +383,7 @@ func TestRemoveOnLoadScript(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
}
func TestLocation(t *testing.T) {
@ -448,10 +400,7 @@ func TestLocation(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var urlstr string
err = c.Run(defaultContext, Location(&urlstr))
@ -478,10 +427,7 @@ func TestTitle(t *testing.T) {
t.Fatal(err)
}
err = c.Run(defaultContext, Sleep(time.Second*1))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
var title string
err = c.Run(defaultContext, Title(&title))

View File

@ -782,13 +782,10 @@ func TestComputedStyle(t *testing.T) {
c := testAllocate(t, "js.html")
defer c.Release()
err := c.Run(defaultContext, Sleep(time.Millisecond*50))
if err != nil {
t.Fatalf("got error: %v", err)
}
time.Sleep(time.Millisecond * 50)
var styles []*css.ComputedProperty
err = c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by))
err := c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
@ -806,10 +803,7 @@ func TestComputedStyle(t *testing.T) {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, Sleep(time.Millisecond*50))
if err != nil {
t.Fatalf("got error: %v", err)
}
time.Sleep(time.Millisecond * 50)
err = c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by))
if err != nil {

39
testdata/input.html vendored Normal file
View File

@ -0,0 +1,39 @@
<!doctype html>
<html>
<body style="background-color: white">
<script>
window.document.test_i = 0
</script>
<input id="input1" type="number" value="0"/>
<input id="input2" type="number" value="0"/>
<input id="button1" type="button" value="button 1">
<hr>
<input id="input3" type="text" value="foo"/>
<textarea id="input4"/></textarea>
<input id="button2" type="button" value="button 2"/>
<input id="button3" type="button" style="float: right;margin-left: 500px;" value="offscreen button" onclick="javascript:window.document.test_i++;return false;"/>
<script>
document.addEventListener("click", function(){
document.getElementById('input1').value = event.clientX;
document.getElementById('input2').value = event.clientY;
});
document.getElementById('button2').addEventListener('click', function(e) {
e.preventDefault();
document.getElementById('input3').value = 'bar';return false;
}, false);
document.getElementById('button2').addEventListener('dblclick', function(e) {
e.preventDefault();
document.getElementById('input3').value = 'bar2';return false;
}, false);
document.getElementById('button2').addEventListener('contextmenu', function(e) {
e.preventDefault();
document.getElementById('input3').value = 'bar-right';return false;
}, false);
document.getElementById('button2').addEventListener('auxclick', function(e) {
e.preventDefault();
document.getElementById('input3').value = 'bar-middle';return false;
}, false);
</script>
</body>
</html>