-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
59 lines (47 loc) · 1.02 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cruda
import (
"context"
"fmt"
"github.com/chromedp/chromedp"
)
func Equals(expression string, expected interface{}) chromedp.Action {
return prefixError("Equals", EqualsAction{
Expression: expression,
Expected: expected,
})
}
type EqualsAction struct {
Expression string
Expected interface{}
}
func (e EqualsAction) Do(ctx context.Context) error {
var res interface{}
err := chromedp.Evaluate(e.Expression, &res, chromedp.EvalAsValue).Do(ctx)
if err != nil {
fmt.Printf("evaluation error: %v", err)
return err
}
isEqual := false
switch v1 := e.Expected.(type) {
case int:
v2, ok := res.(int)
if !ok {
v2f, okf := res.(float64)
if okf {
v2 = int(v2f)
ok = true
}
}
isEqual = ok && v1 == v2
case string:
v2, ok := res.(string)
isEqual = ok && v1 == v2
case bool:
v2, ok := res.(bool)
isEqual = ok && v1 == v2
}
if !isEqual {
return fmt.Errorf("expected type was >>%v<< (%T), but chrome returned >>%v<< (%T)", e.Expected, e.Expected, res, res)
}
return nil
}