Increasing unit tests coverage.

This commit is contained in:
Randy Cahyana 2017-02-24 18:24:06 +07:00 committed by Kenneth Shaw
parent 826072d0bf
commit d63d697c77
2 changed files with 265 additions and 2 deletions

238
sel_test.go Normal file
View File

@ -0,0 +1,238 @@
package chromedp
import (
"testing"
"github.com/knq/chromedp/cdp"
)
func TestWaitReady(t *testing.T) {
t.Parallel()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
var nodeIDs []cdp.NodeID
err = c.Run(defaultContext, NodeIDs("#input2", &nodeIDs, ByID))
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))
}
err = c.Run(defaultContext, WaitReady("#input2", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value(nodeIDs, &value, ByNodeID))
if err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitVisible(t *testing.T) {
t.Parallel()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
var nodeIDs []cdp.NodeID
err = c.Run(defaultContext, NodeIDs("#input2", &nodeIDs, ByID))
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))
}
err = c.Run(defaultContext, WaitVisible("#input2", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value(nodeIDs, &value, ByNodeID))
if err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitNotVisible(t *testing.T) {
t.Parallel()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
var nodeIDs []cdp.NodeID
err = c.Run(defaultContext, NodeIDs("#input2", &nodeIDs, ByID))
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))
}
err = c.Run(defaultContext, Click("#button2", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, WaitNotVisible("#input2", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value(nodeIDs, &value, ByNodeID))
if err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestWaitEnabled(t *testing.T) {
t.Parallel()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
var attr string
ok := false
err = c.Run(defaultContext, AttributeValue("#select1", "disabled", &attr, &ok, ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
if !ok {
t.Fatal("expected element to be disabled")
}
err = c.Run(defaultContext, Click("#button3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, WaitEnabled("#select1", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, AttributeValue("#select1", "disabled", &attr, &ok, ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be enabled")
}
err = c.Run(defaultContext, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
err = c.Run(defaultContext, Value("#select1", &value, ByID))
if 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()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
err = c.Run(defaultContext, Click("#button3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, WaitEnabled("#select1", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
var attr string
ok := false
err = c.Run(defaultContext, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, &ok))
if err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be not selected")
}
err = c.Run(defaultContext, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, WaitSelected(`//*[@id="select1"]/option[1]`))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, nil))
if 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()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
err = c.Run(defaultContext, WaitVisible("#input3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, Click("#button4", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
err = c.Run(defaultContext, WaitNotPresent("#input3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
}
func TestAtLeast(t *testing.T) {
t.Parallel()
c := testAllocate(t, "js.html")
defer c.Release()
var err error
var nodes []*cdp.Node
err = c.Run(defaultContext, Nodes("//input", &nodes, AtLeast(3)))
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))
}
}

27
testdata/js.html vendored
View File

@ -18,6 +18,18 @@
<body style="background-color: white"> <body style="background-color: white">
<input id="input1" type="number" value="0"/> <input id="input1" type="number" value="0"/>
<input id="button1" type="button" value="Click me" ondblclick="increment()"> <input id="button1" type="button" value="Click me" ondblclick="increment()">
<hr>
<input id="input2" type="text" value="foo"/>
<input id="button2" type="button" value="Click me" onclick="show()">
<hr>
<input id="button3" type="button" value="Click me" onclick="enable()">
<select id="select1" disabled>
<option value="foo">foo</option>
<option value="bar" selected>bar</option>
</select>
<hr>
<input id="input3" type="text" value=""/>
<input id="button4" type="button" value="Click me" onclick="remove()">
<script> <script>
document.getElementById('input1').onfocus = function() { document.getElementById('input1').onfocus = function() {
this.value = '9999' this.value = '9999'
@ -26,7 +38,20 @@
this.value = '0' this.value = '0'
}; };
function increment() { function increment() {
document.getElementById('input1').value = parseInt(document.getElementById('input1').value) + 1; document.getElementById('input1').value++;
return false;
}
function show() {
document.getElementById('input2').style.display = 'none';
return false;
}
function enable() {
document.getElementById('select1').disabled = false;
return false;
}
function remove() {
document.body.removeChild(document.getElementById('input3'));
return false;
} }
</script> </script>
</body> </body>