get rid of all sleeps in tests

The navigate sleeps can be replaced by appropriate wait actions.

Some other tests don't need any sleeps at all. This might be because
work is done synchronously now; I haven't been able to get test flakes
after hundreds of test runs with flags like -parallel=32 -count=200.
This commit is contained in:
Daniel Martí 2019-03-20 17:27:57 +00:00
parent 7c1a9fbf3e
commit da4ac414ed
4 changed files with 70 additions and 112 deletions

View File

@ -3,7 +3,6 @@ package chromedp
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom" "github.com/chromedp/cdproto/dom"
@ -158,9 +157,6 @@ func KeyAction(keys string, opts ...KeyOption) Action {
return err return err
} }
} }
// TODO: move to context
time.Sleep(5 * time.Millisecond)
} }
return nil return nil

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"testing" "testing"
"time"
"github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/input" "github.com/chromedp/cdproto/input"
@ -25,10 +24,10 @@ func TestMouseClickXY(t *testing.T) {
ctx, cancel := testAllocate(t, "input.html") ctx, cancel := testAllocate(t, "input.html")
defer cancel() defer cancel()
if err := Run(ctx, Sleep(100*time.Millisecond)); err != nil {
if err := Run(ctx, WaitVisible(`#input1`, ByID)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
tests := []struct { tests := []struct {
x, y int64 x, y int64
}{ }{
@ -43,8 +42,6 @@ func TestMouseClickXY(t *testing.T) {
t.Fatalf("test %d got error: %v", i, err) t.Fatalf("test %d got error: %v", i, err)
} }
time.Sleep(50 * time.Millisecond)
var xstr, ystr string var xstr, ystr string
if err := Run(ctx, Value("#input1", &xstr, ByID)); err != nil { if err := Run(ctx, Value("#input1", &xstr, ByID)); err != nil {
t.Fatalf("test %d got error: %v", i, err) t.Fatalf("test %d got error: %v", i, err)
@ -106,8 +103,6 @@ func TestMouseClickNode(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
time.Sleep(50 * time.Millisecond)
var value string var value string
if err := Run(ctx, Value("#input3", &value, ByID)); err != nil { if err := Run(ctx, Value("#input3", &value, ByID)); err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
@ -165,8 +160,6 @@ func TestMouseClickOffscreenNode(t *testing.T) {
} }
time.Sleep(100 * time.Millisecond)
var value int var value int
if err := Run(ctx, Evaluate("window.document.test_i", &value)); err != nil { if err := Run(ctx, Evaluate("window.document.test_i", &value)); err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)

View File

@ -43,11 +43,12 @@ func TestNavigationEntries(t *testing.T) {
ctx, cancel := testAllocate(t, "") ctx, cancel := testAllocate(t, "")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
tests := []string{ tests := []struct {
"form.html", file, waitID string
"image.html", }{
{"form.html", "#form"},
{"image.html", "#icon-brankas"},
} }
var entries []*page.NavigationEntry var entries []*page.NavigationEntry
@ -64,16 +65,14 @@ func TestNavigationEntries(t *testing.T) {
} }
expIdx, expEntries := 1, 2 expIdx, expEntries := 1, 2
for i, url := range tests { for i, test := range tests {
if err := Run(ctx, Navigate(testdataDir+"/"+url)); err != nil { if err := Run(ctx, Tasks{
Navigate(testdataDir + "/" + test.file),
WaitVisible(test.waitID, ByID),
NavigationEntries(&index, &entries),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil {
t.Fatal(err)
}
if len(entries) != expEntries { if len(entries) != expEntries {
t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries)) t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries))
} }
@ -94,27 +93,24 @@ func TestNavigateToHistoryEntry(t *testing.T) {
var entries []*page.NavigationEntry var entries []*page.NavigationEntry
var index int64 var index int64
time.Sleep(50 * time.Millisecond) if err := Run(ctx, Tasks{
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil { WaitVisible(`#icon-brankas`, ByID), // for image.html
NavigationEntries(&index, &entries),
Navigate(testdataDir + "/form.html"),
WaitVisible(`#form`, ByID), // for form.html
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigateToHistoryEntry(entries[index].ID)); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
var title string var title string
if err := Run(ctx, Title(&title)); err != nil { if err := Run(ctx, Tasks{
NavigateToHistoryEntry(entries[index].ID),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if title != entries[index].Title { if title != entries[index].Title {
t.Errorf("expected title to be %s, instead title is: %s", entries[index].Title, title) t.Errorf("expected title to be %s, instead title is: %s", entries[index].Title, title)
} }
@ -125,26 +121,19 @@ func TestNavigateBack(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html") ctx, cancel := testAllocate(t, "form.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var exptitle string var title, exptitle string
if err := Run(ctx, Title(&exptitle)); err != nil { if err := Run(ctx, Tasks{
t.Fatal(err) WaitVisible(`#form`, ByID), // for form.html
} Title(&exptitle),
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil { Navigate(testdataDir + "/image.html"),
t.Fatal(err) WaitVisible(`#icon-brankas`, ByID), // for image.html
}
time.Sleep(50 * time.Millisecond) NavigateBack(),
if err := Run(ctx, NavigateBack()); err != nil { WaitVisible(`#form`, ByID), // for form.html
t.Fatal(err) Title(&title),
} }); err != nil {
time.Sleep(50 * time.Millisecond)
var title string
if err := Run(ctx, Title(&title)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -158,30 +147,22 @@ func TestNavigateForward(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html") ctx, cancel := testAllocate(t, "form.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond) var title, exptitle string
if err := Run(ctx, Tasks{
WaitVisible(`#form`, ByID), // for form.html
var exptitle string Navigate(testdataDir + "/image.html"),
if err := Run(ctx, Title(&exptitle)); err != nil { WaitVisible(`#icon-brankas`, ByID), // for image.html
t.Fatal(err) Title(&exptitle),
}
if err := Run(ctx, NavigateBack()); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond) NavigateBack(),
if err := Run(ctx, NavigateForward()); err != nil { WaitVisible(`#form`, ByID), // for form.html
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond) NavigateForward(),
WaitVisible(`#icon-brankas`, ByID), // for image.html
var title string Title(&title),
if err := Run(ctx, Title(&title)); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -206,21 +187,18 @@ func TestReload(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html") ctx, cancel := testAllocate(t, "form.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond) var title, exptitle string
if err := Run(ctx, Tasks{
WaitVisible(`#form`, ByID), // for form.html
Title(&exptitle),
var exptitle string Reload(),
if err := Run(ctx, Title(&exptitle)); err != nil { // TODO: rewrite test to change the content after a reload, so
t.Fatal(err) // we can wait on a selector.
} Sleep(10 * time.Millisecond),
WaitVisible(`#form`, ByID), // for form.html
if err := Run(ctx, Reload()); err != nil { Title(&title),
t.Fatal(err) }); err != nil {
}
time.Sleep(50 * time.Millisecond)
var title string
if err := Run(ctx, Title(&title)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -235,10 +213,11 @@ func TestCaptureScreenshot(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html") ctx, cancel := testAllocate(t, "image.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var buf []byte var buf []byte
if err := Run(ctx, CaptureScreenshot(&buf)); err != nil { if err := Run(ctx, Tasks{
WaitVisible(`#icon-brankas`, ByID), // for image.html
CaptureScreenshot(&buf),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -265,8 +244,6 @@ func TestCaptureScreenshot(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(50 * time.Millisecond)
if scriptID == "" { if scriptID == "" {
t.Fatal("got empty script ID") t.Fatal("got empty script ID")
} }
@ -298,8 +275,6 @@ func TestRemoveOnLoadScript(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(50 * time.Millisecond)
}*/ }*/
func TestLocation(t *testing.T) { func TestLocation(t *testing.T) {
@ -308,10 +283,11 @@ func TestLocation(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html") ctx, cancel := testAllocate(t, "form.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var urlstr string var urlstr string
if err := Run(ctx, Location(&urlstr)); err != nil { if err := Run(ctx, Tasks{
WaitVisible(`#form`, ByID), // for form.html
Location(&urlstr),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -326,10 +302,11 @@ func TestTitle(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html") ctx, cancel := testAllocate(t, "image.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var title string var title string
if err := Run(ctx, Title(&title)); err != nil { if err := Run(ctx, Tasks{
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/css" "github.com/chromedp/cdproto/css"
@ -680,8 +679,6 @@ func TestDoubleClick(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
time.Sleep(50 * time.Millisecond)
var value string var value string
if err := Run(ctx, Value("#input1", &value, ByID)); err != nil { if err := Run(ctx, Value("#input1", &value, ByID)); err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
@ -824,8 +821,6 @@ func TestComputedStyle(t *testing.T) {
ctx, cancel := testAllocate(t, "js.html") ctx, cancel := testAllocate(t, "js.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var styles []*css.ComputedProperty var styles []*css.ComputedProperty
err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)) err := Run(ctx, ComputedStyle(test.sel, &styles, test.by))
if err != nil { if err != nil {
@ -843,7 +838,6 @@ func TestComputedStyle(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)); err != nil { if err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)); err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
@ -879,8 +873,6 @@ func TestMatchedStyle(t *testing.T) {
ctx, cancel := testAllocate(t, "js.html") ctx, cancel := testAllocate(t, "js.html")
defer cancel() defer cancel()
time.Sleep(50 * time.Millisecond)
var styles *css.GetMatchedStylesForNodeReturns var styles *css.GetMatchedStylesForNodeReturns
err := Run(ctx, MatchedStyle(test.sel, &styles, test.by)) err := Run(ctx, MatchedStyle(test.sel, &styles, test.by))
if err != nil { if err != nil {