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

View File

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

View File

@ -43,11 +43,12 @@ func TestNavigationEntries(t *testing.T) {
ctx, cancel := testAllocate(t, "")
defer cancel()
time.Sleep(50 * time.Millisecond)
tests := []string{
"form.html",
"image.html",
tests := []struct {
file, waitID string
}{
{"form.html", "#form"},
{"image.html", "#icon-brankas"},
}
var entries []*page.NavigationEntry
@ -64,16 +65,14 @@ func TestNavigationEntries(t *testing.T) {
}
expIdx, expEntries := 1, 2
for i, url := range tests {
if err := Run(ctx, Navigate(testdataDir+"/"+url)); err != nil {
for i, test := range tests {
if err := Run(ctx, Tasks{
Navigate(testdataDir + "/" + test.file),
WaitVisible(test.waitID, ByID),
NavigationEntries(&index, &entries),
}); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil {
t.Fatal(err)
}
if len(entries) != expEntries {
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 index int64
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil {
if err := Run(ctx, Tasks{
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)
}
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
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)
}
if title != entries[index].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")
defer cancel()
time.Sleep(50 * time.Millisecond)
var exptitle string
if err := Run(ctx, Title(&exptitle)); err != nil {
t.Fatal(err)
}
var title, exptitle string
if err := Run(ctx, Tasks{
WaitVisible(`#form`, ByID), // for form.html
Title(&exptitle),
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil {
t.Fatal(err)
}
Navigate(testdataDir + "/image.html"),
WaitVisible(`#icon-brankas`, ByID), // for image.html
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigateBack()); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
var title string
if err := Run(ctx, Title(&title)); err != nil {
NavigateBack(),
WaitVisible(`#form`, ByID), // for form.html
Title(&title),
}); err != nil {
t.Fatal(err)
}
@ -158,30 +147,22 @@ func TestNavigateForward(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
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
if err := Run(ctx, Title(&exptitle)); err != nil {
t.Fatal(err)
}
if err := Run(ctx, NavigateBack()); err != nil {
t.Fatal(err)
}
Navigate(testdataDir + "/image.html"),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&exptitle),
time.Sleep(50 * time.Millisecond)
if err := Run(ctx, NavigateForward()); err != nil {
t.Fatal(err)
}
NavigateBack(),
WaitVisible(`#form`, ByID), // for form.html
time.Sleep(50 * time.Millisecond)
var title string
if err := Run(ctx, Title(&title)); err != nil {
NavigateForward(),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
t.Fatal(err)
}
@ -206,21 +187,18 @@ func TestReload(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
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
if err := Run(ctx, Title(&exptitle)); err != nil {
t.Fatal(err)
}
if err := Run(ctx, Reload()); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
var title string
if err := Run(ctx, Title(&title)); err != nil {
Reload(),
// TODO: rewrite test to change the content after a reload, so
// we can wait on a selector.
Sleep(10 * time.Millisecond),
WaitVisible(`#form`, ByID), // for form.html
Title(&title),
}); err != nil {
t.Fatal(err)
}
@ -235,10 +213,11 @@ func TestCaptureScreenshot(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
time.Sleep(50 * time.Millisecond)
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)
}
@ -265,8 +244,6 @@ func TestCaptureScreenshot(t *testing.T) {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if scriptID == "" {
t.Fatal("got empty script ID")
}
@ -298,8 +275,6 @@ func TestRemoveOnLoadScript(t *testing.T) {
if err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
}*/
func TestLocation(t *testing.T) {
@ -308,10 +283,11 @@ func TestLocation(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
time.Sleep(50 * time.Millisecond)
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)
}
@ -326,10 +302,11 @@ func TestTitle(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
time.Sleep(50 * time.Millisecond)
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)
}

View File

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