Repository navigation
Expand file tree
/
Copy pathassertions.go
More file actions
185 lines (166 loc) · 5.5 KB
/
Copy pathassertions.go
File metadata and controls
185 lines (166 loc) · 5.5 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package playwright
import (
"errors"
"fmt"
"reflect"
"regexp"
"strings"
)
const assertionsDefaultTimeout = 5000 // 5s
type playwrightAssertionsImpl struct {
defaultTimeout *float64
}
// NewPlaywrightAssertions creates a new instance of PlaywrightAssertions
// - timeout: default value is 5000 (ms)
func NewPlaywrightAssertions(timeout ...float64) PlaywrightAssertions {
if len(timeout) > 0 {
return &playwrightAssertionsImpl{Float(timeout[0])}
}
return &playwrightAssertionsImpl{Float(assertionsDefaultTimeout)}
}
func (pa *playwrightAssertionsImpl) APIResponse(response APIResponse) APIResponseAssertions {
return newAPIResponseAssertions(response, false)
}
func (pa *playwrightAssertionsImpl) Locator(locator Locator) LocatorAssertions {
return newLocatorAssertions(locator, false, pa.defaultTimeout)
}
func (pa *playwrightAssertionsImpl) Page(page Page) PageAssertions {
return newPageAssertions(page, false, pa.defaultTimeout)
}
type expectedTextValue struct {
Str *string `json:"string,omitempty"`
RegexSource *string `json:"regexSource,omitempty"`
RegexFlags *string `json:"regexFlags,omitempty"`
MatchSubstring *bool `json:"matchSubstring,omitempty"`
IgnoreCase *bool `json:"ignoreCase,omitempty"`
NormalizeWhiteSpace *bool `json:"normalizeWhiteSpace,omitempty"`
}
type frameExpectOptions struct {
ExpressionArg any `json:"expressionArg,omitempty"`
ExpectedText []expectedTextValue `json:"expectedText,omitempty"`
ExpectedNumber *float64 `json:"expectedNumber,omitempty"`
ExpectedValue any `json:"expectedValue,omitempty"`
UseInnerText *bool `json:"useInnerText,omitempty"`
IsNot bool `json:"isNot"`
Timeout *float64 `json:"timeout"`
Pseudo *PseudoElement `json:"pseudo,omitempty"`
}
type frameExpectResult struct {
Matches bool `json:"matches"`
Received any `json:"received,omitempty"`
TimedOut *bool `json:"timedOut,omitempty"`
Log []string `json:"log,omitempty"`
// ErrorMessage is the driver's explanation of why the assertion could not
// be evaluated, such as a locator matching elements in multiple frames.
ErrorMessage string `json:"errorMessage,omitempty"`
}
type assertionsBase struct {
actualLocator Locator
isNot bool
defaultTimeout *float64
}
func (b *assertionsBase) expect(
expression string,
options frameExpectOptions,
expected any,
message string,
) error {
options.IsNot = b.isNot
if options.Timeout == nil {
options.Timeout = b.defaultTimeout
}
if options.IsNot {
message = strings.ReplaceAll(message, "expected to", "expected not to")
}
result, err := b.actualLocator.(*locatorImpl).expect(expression, options)
if err != nil {
return err
}
if result.Matches == b.isNot {
actual := result.Received
log := strings.Join(result.Log, "\n")
if log != "" {
log = "\nCall log:\n" + log
}
if result.ErrorMessage != "" {
log = "\nError: " + result.ErrorMessage + log
}
if expected != nil {
return fmt.Errorf("%s '%v'\nActual value: %v %s", message, expected, actual, log)
}
return fmt.Errorf("%s\nActual value: %v %s", message, actual, log)
}
return nil
}
// parseExpectReceived unwraps the `received` object returned by the `expect`
// channel call. The server emits `received` as `{ value?: SerializedValue,
// ariaSnapshot?: string }` (see server frameDispatcher), so we must descend into
// the nested `value` before parsing it, falling back to `ariaSnapshot`. This
// mirrors upstream frame._expect (client/frame.ts).
func parseExpectReceived(received any) any {
recv, ok := received.(map[string]any)
if !ok {
return nil
}
if value, ok := recv["value"]; ok {
return parseResult(value)
}
if ariaSnapshot, ok := recv["ariaSnapshot"]; ok {
return ariaSnapshot
}
return nil
}
func toExpectedTextValues(
items []any,
matchSubstring bool,
normalizeWhiteSpace bool,
ignoreCase *bool,
) ([]expectedTextValue, error) {
var out []expectedTextValue
for _, item := range items {
switch item := item.(type) {
case string:
out = append(out, expectedTextValue{
Str: String(item),
MatchSubstring: Bool(matchSubstring),
NormalizeWhiteSpace: Bool(normalizeWhiteSpace),
IgnoreCase: ignoreCase,
})
case *regexp.Regexp:
pattern, flags := convertRegexp(item)
out = append(out, expectedTextValue{
RegexSource: String(pattern),
RegexFlags: String(flags),
MatchSubstring: Bool(matchSubstring),
NormalizeWhiteSpace: Bool(normalizeWhiteSpace),
IgnoreCase: ignoreCase,
})
default:
return nil, errors.New("value must be a string or regexp")
}
}
return out, nil
}
// isExpectedList reports whether the expected value is a list rather than a
// single item. It mirrors upstream's `Array.isArray(expected)` branch: the
// element types do not select the branch, they are validated per item by
// toExpectedTextValues. This is what makes a mixed list such as
// []any{"foo", regexp.MustCompile("bar")} work, matching the documented
// "string or RegExp or a list of those" (upstream Array<string|RegExp>).
func isExpectedList(expected any) bool {
if expected == nil {
return false
}
return reflect.ValueOf(expected).Kind() == reflect.Slice
}
func convertToInterfaceList(v any) []any {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice {
return []any{v}
}
list := make([]any, rv.Len())
for i := 0; i < rv.Len(); i++ {
list[i] = rv.Index(i).Interface()
}
return list
}