clean up uses of Run in the tests

Many consecutive calls to Run can be collapsed into a single call. While
at it, make the error handling style more consistent. Overall removes 70
lines of repetitive code.
This commit is contained in:
Daniel Martí 2019-04-01 19:46:47 +01:00
parent 120628a01c
commit e482cdfc4d
5 changed files with 121 additions and 190 deletions

View File

@ -40,8 +40,7 @@ func MouseClickXY(x, y int64, opts ...MouseOption) Action {
me = o(me)
}
err := me.Do(ctx, h)
if err != nil {
if err := me.Do(ctx, h); err != nil {
return err
}

View File

@ -38,12 +38,11 @@ func TestMouseClickXY(t *testing.T) {
}
for i, test := range tests {
if err := Run(ctx, MouseClickXY(test.x, test.y)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
var xstr, ystr string
if err := Run(ctx, Value("#input1", &xstr, ByID)); err != nil {
if err := Run(ctx,
MouseClickXY(test.x, test.y),
Value("#input1", &xstr, ByID),
); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
@ -96,16 +95,14 @@ func TestMouseClickNode(t *testing.T) {
if err := Run(ctx, Nodes(test.sel, &nodes, test.by)); 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))
}
if err := Run(ctx, MouseClickNode(nodes[0], test.opt)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value("#input3", &value, ByID)); err != nil {
if err := Run(ctx,
MouseClickNode(nodes[0], test.opt),
Value("#input3", &value, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -204,10 +201,10 @@ func TestKeyAction(t *testing.T) {
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
if err := Run(ctx, Focus(test.sel, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, KeyAction(test.exp)); err != nil {
if err := Run(ctx,
Focus(test.sel, test.by),
KeyAction(test.exp),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -254,12 +251,11 @@ func TestKeyActionNode(t *testing.T) {
if len(nodes) != 1 {
t.Fatalf("expected nodes to have exactly 1 element, got: %d", len(nodes))
}
if err := Run(ctx, KeyActionNode(nodes[0], test.exp)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
if err := Run(ctx,
KeyActionNode(nodes[0], test.exp),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}

View File

@ -21,15 +21,13 @@ func TestNavigate(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
if err := Run(ctx, WaitVisible(`#icon-brankas`, ByID)); err != nil {
t.Fatal(err)
}
var urlstr string
if err := Run(ctx, Location(&urlstr)); err != nil {
if err := Run(ctx,
Location(&urlstr),
WaitVisible(`#icon-brankas`, ByID),
); err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(urlstr, "image.html") {
t.Errorf("expected to be on image.html, at: %s", urlstr)
}
@ -265,13 +263,10 @@ func TestCaptureScreenshot(t *testing.T) {
defer cancel()
var scriptID page.ScriptIdentifier
err = Run(ctx, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
if err != nil {
t.Fatal(err)
}
err = Run(ctx, Navigate(testdataDir+"/form.html"))
if err != nil {
if err := Run(ctx,
AddOnLoadScript(`window.alert("TEST")`, &scriptID),
Navigate(testdataDir+"/form.html"),
); err != nil {
t.Fatal(err)
}
@ -288,22 +283,17 @@ func TestRemoveOnLoadScript(t *testing.T) {
defer cancel()
var scriptID page.ScriptIdentifier
err = Run(ctx, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
if err != nil {
if err := Run(ctx, AddOnLoadScript(`window.alert("TEST")`, &scriptID)); err != nil {
t.Fatal(err)
}
if scriptID == "" {
t.Fatal("got empty script ID")
}
err = Run(ctx, RemoveOnLoadScript(scriptID))
if err != nil {
t.Fatal(err)
}
err = Run(ctx, Navigate(testdataDir+"/form.html"))
if err != nil {
if err := Run(ctx,
RemoveOnLoadScript(scriptID),
Navigate(testdataDir+"/form.html"),
); err != nil {
t.Fatal(err)
}
}*/

View File

@ -95,28 +95,26 @@ func TestFocusBlur(t *testing.T) {
{"#input1", ByID},
}
err := Run(ctx, Click("#input1", ByID))
if err != nil {
if err := Run(ctx, Click("#input1", ByID)); err != nil {
t.Fatal(err)
}
for i, test := range tests {
if err := Run(ctx, Focus(test.sel, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
var value string
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
if err := Run(ctx,
Focus(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if value != "9999" {
t.Errorf("test %d expected value is '9999', got: '%s'", i, value)
}
if err := Run(ctx, Blur(test.sel, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
if err := Run(ctx,
Blur(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
@ -218,20 +216,18 @@ func TestClear(t *testing.T) {
defer cancel()
var val string
err := Run(ctx, Value(test.sel, &val, test.by))
if err != nil {
if err := Run(ctx, Value(test.sel, &val, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if val == "" {
t.Errorf("expected `%s` to have non empty value", test.sel)
}
if err := Run(ctx, Clear(test.sel, test.by)); err != nil {
if err := Run(ctx,
Clear(test.sel, test.by),
Value(test.sel, &val, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, Value(test.sel, &val, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if val != "" {
t.Errorf("expected empty value for `%s`, got: %s", test.sel, val)
}
@ -262,16 +258,12 @@ func TestReset(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
err := Run(ctx, SetValue(test.sel, test.value, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, Reset(test.sel, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
if err := Run(ctx,
SetValue(test.sel, test.value, test.by),
Reset(test.sel, test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -331,13 +323,11 @@ func TestSetValue(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
err := Run(ctx, SetValue(test.sel, "FOOBAR", test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(test.sel, &value, test.by)); err != nil {
if err := Run(ctx,
SetValue(test.sel, "FOOBAR", test.by),
Value(test.sel, &value, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -506,8 +496,7 @@ func TestSetAttributes(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
err := Run(ctx, SetAttributes(test.sel, test.attrs, test.by))
if err != nil {
if err := Run(ctx, SetAttributes(test.sel, test.attrs, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
@ -550,11 +539,9 @@ func TestAttributeValue(t *testing.T) {
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("test %d got error: %v", i, err)
}
if !ok {
t.Fatalf("test %d failed to get attribute %s on %s", i, test.attr, test.sel)
}
if value != test.exp {
t.Errorf("test %d expected %s to be %s, got: %s", i, test.attr, test.exp, value)
}
@ -584,8 +571,7 @@ func TestSetAttributeValue(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
err := Run(ctx, SetAttributeValue(test.sel, test.attr, test.exp, test.by))
if err != nil {
if err := Run(ctx, SetAttributeValue(test.sel, test.attr, test.exp, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
@ -597,11 +583,9 @@ func TestSetAttributeValue(t *testing.T) {
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if !ok {
t.Fatalf("failed to get attribute %s on %s", test.attr, test.sel)
}
if value != test.exp {
t.Errorf("expected %s to be %s, got: %s", test.attr, test.exp, value)
}
@ -631,8 +615,7 @@ func TestRemoveAttribute(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
err := Run(ctx, RemoveAttribute(test.sel, test.attr))
if err != nil {
if err := Run(ctx, RemoveAttribute(test.sel, test.attr)); err != nil {
t.Fatalf("got error: %v", err)
}
@ -644,7 +627,6 @@ func TestRemoveAttribute(t *testing.T) {
if err := Run(ctx, AttributeValue(test.sel, test.attr, &value, &ok, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
if ok || value != "" {
t.Fatalf("expected attribute %s removed from element %s", test.attr, test.sel)
}
@ -673,16 +655,12 @@ func TestClick(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
err := Run(ctx, Click(test.sel, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitVisible("#icon-brankas", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
var title string
if err := Run(ctx, Title(&title)); err != nil {
if err := Run(ctx,
Click(test.sel, test.by),
WaitVisible("#icon-brankas", ByID),
Title(&title),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -714,13 +692,11 @@ func TestDoubleClick(t *testing.T) {
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
err := Run(ctx, DoubleClick(test.sel, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value("#input1", &value, ByID)); err != nil {
if err := Run(ctx,
DoubleClick(test.sel, test.by),
Value("#input1", &value, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -756,13 +732,11 @@ func TestSendKeys(t *testing.T) {
ctx, cancel := testAllocate(t, "visible.html")
defer cancel()
err := Run(ctx, SendKeys(test.sel, test.keys, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
var val string
if err := Run(ctx, Value(test.sel, &val, test.by)); err != nil {
if err := Run(ctx,
SendKeys(test.sel, test.keys, test.by),
Value(test.sel, &val, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -841,16 +815,12 @@ func TestSubmit(t *testing.T) {
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
err := Run(ctx, Submit(test.sel, test.by))
if err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitVisible("#icon-brankas", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
var title string
if err := Run(ctx, Title(&title)); err != nil {
if err := Run(ctx,
Submit(test.sel, test.by),
WaitVisible("#icon-brankas", ByID),
Title(&title),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -883,8 +853,7 @@ func TestComputedStyle(t *testing.T) {
defer cancel()
var styles []*css.ComputedProperty
err := Run(ctx, ComputedStyle(test.sel, &styles, test.by))
if err != nil {
if err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}
@ -895,11 +864,10 @@ func TestComputedStyle(t *testing.T) {
}
}
}
if err := Run(ctx, Click("#input1", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, ComputedStyle(test.sel, &styles, test.by)); err != nil {
if err := Run(ctx,
Click("#input1", ByID),
ComputedStyle(test.sel, &styles, test.by),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -936,8 +904,7 @@ func TestMatchedStyle(t *testing.T) {
defer cancel()
var styles *css.GetMatchedStylesForNodeReturns
err := Run(ctx, MatchedStyle(test.sel, &styles, test.by))
if err != nil {
if err := Run(ctx, MatchedStyle(test.sel, &styles, test.by)); err != nil {
t.Fatalf("got error: %v", err)
}

View File

@ -13,19 +13,17 @@ func TestWaitReady(t *testing.T) {
defer cancel()
var nodeIDs []cdp.NodeID
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
if err != nil {
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); 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))
}
if err := Run(ctx, WaitReady("#input2", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
if err := Run(ctx,
WaitReady("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
@ -37,19 +35,17 @@ func TestWaitVisible(t *testing.T) {
defer cancel()
var nodeIDs []cdp.NodeID
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
if err != nil {
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); 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))
}
if err := Run(ctx, WaitVisible("#input2", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
if err := Run(ctx,
WaitVisible("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
@ -61,22 +57,18 @@ func TestWaitNotVisible(t *testing.T) {
defer cancel()
var nodeIDs []cdp.NodeID
err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID))
if err != nil {
if err := Run(ctx, NodeIDs("#input2", &nodeIDs, ByID)); 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))
}
if err := Run(ctx, Click("#button2", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitNotVisible("#input2", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value(nodeIDs, &value, ByNodeID)); err != nil {
if err := Run(ctx,
Click("#button2", ByID),
WaitNotVisible("#input2", ByID),
Value(nodeIDs, &value, ByNodeID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
@ -89,32 +81,27 @@ func TestWaitEnabled(t *testing.T) {
var attr string
var ok bool
err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID))
if err != nil {
if err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if !ok {
t.Fatal("expected element to be disabled")
}
if err := Run(ctx, Click("#button3", ByID)); err != nil {
if err := Run(ctx,
Click("#button3", ByID),
WaitEnabled("#select1", ByID),
AttributeValue("#select1", "disabled", &attr, &ok, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitEnabled("#select1", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, AttributeValue("#select1", "disabled", &attr, &ok, ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be enabled")
}
if err := Run(ctx, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true")); err != nil {
t.Fatalf("got error: %v", err)
}
var value string
if err := Run(ctx, Value("#select1", &value, ByID)); err != nil {
if err := Run(ctx,
SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"),
Value("#select1", &value, ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -129,11 +116,10 @@ func TestWaitSelected(t *testing.T) {
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
err := Run(ctx, Click("#button3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitEnabled("#select1", ByID)); err != nil {
if err := Run(ctx,
Click("#button3", ByID),
WaitEnabled("#select1", ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -142,17 +128,14 @@ func TestWaitSelected(t *testing.T) {
if err := Run(ctx, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, &ok)); err != nil {
t.Fatalf("got error: %v", err)
}
if ok {
t.Fatal("expected element to be not selected")
}
if err := Run(ctx, SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true")); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitSelected(`//*[@id="select1"]/option[1]`)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, nil)); err != nil {
if err := Run(ctx,
SetAttributeValue(`//*[@id="select1"]/option[1]`, "selected", "true"),
WaitSelected(`//*[@id="select1"]/option[1]`),
AttributeValue(`//*[@id="select1"]/option[1]`, "selected", &attr, nil),
); err != nil {
t.Fatalf("got error: %v", err)
}
@ -167,14 +150,11 @@ func TestWaitNotPresent(t *testing.T) {
ctx, cancel := testAllocate(t, "js.html")
defer cancel()
err := Run(ctx, WaitVisible("#input3", ByID))
if err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, Click("#button4", ByID)); err != nil {
t.Fatalf("got error: %v", err)
}
if err := Run(ctx, WaitNotPresent("#input3", ByID)); err != nil {
if err := Run(ctx,
WaitVisible("#input3", ByID),
Click("#button4", ByID),
WaitNotPresent("#input3", ByID),
); err != nil {
t.Fatalf("got error: %v", err)
}
}
@ -186,8 +166,7 @@ func TestAtLeast(t *testing.T) {
defer cancel()
var nodes []*cdp.Node
err := Run(ctx, Nodes("//input", &nodes, AtLeast(3)))
if err != nil {
if err := Run(ctx, Nodes("//input", &nodes, AtLeast(3))); err != nil {
t.Fatalf("got error: %v", err)
}
if len(nodes) < 3 {